0

I'm developing a WCF service using Net.tcp Binding. The service is hosted on the Run method of a worker role.

When deployed to my Azure Account, it works fine, but in runtime it throws an exception:

No connection could be made because the target machine actively refused it

Sometimes when I change the port number, it works fine for a couple of times, but then it refuses the connection again and I have to change the port number again...

I made the exceptions in windows firewall, and also shut down the firewall but it doesn't work.

Could it be some constraint on Windows 7? Any help appreciated. Thanks

Edit: I'm adding client and Server code for clarification.

Service configuration:

using (ServiceHost host = new ServiceHost(typeof(XMPPService)))
{
    string ip = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["tcpinput"].IPEndpoint.Address.ToString();
    int tcpport = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["tcpinput"].IPEndpoint.Port;
    int mexport = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["mexinput"].IPEndpoint.Port;
    ServiceMetadataBehavior metadatabehavior = new ServiceMetadataBehavior();
    host.Description.Behaviors.Add(metadatabehavior);

    ServiceDebugBehavior behavior = host.Description.Behaviors.Find<ServiceDebugBehavior>();
    ServiceThrottlingBehavior tho = new ServiceThrottlingBehavior();
    tho.MaxConcurrentCalls = 10000;
    tho.MaxConcurrentInstances = 1000;
    tho.MaxConcurrentSessions = 1000;
    host.Description.Behaviors.Add(tho);

    if (behavior == null)
    {
        host.Description.Behaviors.Add(new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
    }
    else
    {
        if (!behavior.IncludeExceptionDetailInFaults)
        {
            behavior.IncludeExceptionDetailInFaults = true;
        }
    }

    Binding mexBinding = MetadataExchangeBindings.CreateMexTcpBinding();

    string mexlistenurl = string.Format("net.tcp://{0}:{1}/XMPPServiceMetaDataEndpoint", ip, mexport);
    string mexendpointurl = string.Format("net.tcp://{0}:{1}/XMPPServiceMetaDataEndpoint", ip, mexport);
    host.AddServiceEndpoint(typeof(IMetadataExchange), mexBinding, mexendpointurl, new Uri(mexlistenurl));
    NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);
    tcpBinding.CloseTimeout = TimeSpan.FromMinutes(2);

    tcpBinding.ReceiveTimeout = TimeSpan.FromDays(23);
    tcpBinding.OpenTimeout = TimeSpan.FromMinutes(3);
    tcpBinding.SendTimeout = TimeSpan.FromMinutes(1);
    tcpBinding.PortSharingEnabled = true;
    tcpBinding.MaxConnections = 10000;
    tcpBinding.MaxConnections = 100;
    // tcpBinding.ListenBacklog = 1000000;
    tcpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromSeconds(90);
    tcpBinding.ReliableSession.Enabled = true;

    // Add the endpoint for MyService
    string listenurl = string.Format("net.tcp://{0}:{1}/ServiceEndpoint", ip, tcpport);
    string endpointurl = string.Format("net.tcp://{0}:{1}/ServiceEndpoint", ip, tcpport);
    host.AddServiceEndpoint(typeof(IXMPPService), tcpBinding, endpointurl, new Uri(listenurl));

    host.Open();

    Thread.Sleep(Timeout.Infinite);
}

Client:

AppService()  //private constructor
{
    client = new ServiceRef.ServiceClient();
}

Service call:

bool isAvailable = false;

try
{
    isAvailable=client.IsAvailable(_ixo.IMBot.IMEmail, _ixo.Operator.IMClients.First().IMEmail);
}
catch
{
    if (client.InnerChannel.State == System.ServiceModel.CommunicationState.Faulted)
    {
        client.InnerChannel.Abort();
        client = new ServiceRef.ServiceClient();
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Sebastián Odena
  • 195
  • 1
  • 4
  • 13
  • 1
    Add some server and client code examples, please – Tommy Grovnes Aug 30 '12 at 16:40
  • are you binding to 127.0.0.1:XXXX ? You shall never do that with Windows Azure. Instead you shall take the IPEndpoint from the InstanceEndpoints - http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleinstance.instanceendpoints take from RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["yourEndpointName"] – astaykov Aug 30 '12 at 19:31
  • Yes astaykov, I'm taking endpoint from the InstanceEndpoints. It points to 127.255.0.1. That's the IP I'm using from the client. It works from time to time. – Sebastián Odena Aug 31 '12 at 03:55
  • Give your question **meaningful** title, not just a list of tags. – abatishchev Aug 31 '12 at 11:47

2 Answers2

0

As your problem is sporadic and changing port works for a while, it is possible that the net.tcp port sharing is conflicting the service. So because you are using net.tcp based WCF application (in Web or Worker Role) locally then be sure to have net.tcp port sharing services enabled to run effectively.

Also in your service start code you can setup the binding properly to the base IP address and port something as below:

NetTcpBinding binding = new NetTcpBinding();
binding.PortSharingEnabled = true;
// Setup other binding properties here.

// Service_NAME is the Serice Name in this project
ServiceHost host = new ServiceHost(typeof(Service_NAME)); 

//Endpoint1 is the End point name you have setup in your Windows Azure Role property
string serviceIP = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint.Address.ToString();
string servicePort = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint.Port.ToString();
// 
string address = String.Format("net.tcp://{0}:{1}/SERVICE_METHOD*", serviceIP, servicePort);
host.AddServiceEndpoint(typeof(YOUR_SERVICE_TYPE), binding, address);
host.Open();
AvkashChauhan
  • 20,495
  • 3
  • 34
  • 65
0

"I found out what's happening: The IP assigned to the worker role is dynamic. Sometimes it's 127.255.0.0 and other times is 127.255.0.1"

I think you're making the same mistake I was. See here:

Azure Compute Emulator: Is it possible to control the IP of individual instances?

Community
  • 1
  • 1
Jude Fisher
  • 11,138
  • 7
  • 48
  • 91