【问题标题】:WCF net.TCP self hosted on azure emulator - Windows 7WCF net.TCP 自托管在 azure 模拟器 - Windows 7
【发布时间】:2012-08-30 15:45:11
【问题描述】:

我正在使用 Net.tcp Binding 开发 WCF 服务。该服务托管在辅助角色的 Run 方法上。

部署到我的 Azure 帐户后,它可以正常工作,但在运行时会引发异常:

由于目标机器主动拒绝,无法建立连接

有时当我更改端口号时,它可以正常工作几次,但随后它再次拒绝连接,我必须再次更改端口号......

我在windows防火墙中做了例外,也关闭了防火墙,但它不起作用。

这可能是对 Windows 7 的一些限制吗?任何帮助表示赞赏。谢谢

编辑:我正在添加客户端和服务器代码以进行澄清。

服务配置:

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);
}

客户:

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

服务调用:

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();
    }
}

【问题讨论】:

  • 请添加一些服务器和客户端代码示例
  • 您是否绑定到 127.0.0.1:XXXX ?对于 Windows Azure,您永远不会这样做。相反,您应该从 InstanceEndpoints 获取 IPEndpoint - msdn.microsoft.com/en-us/library/… 从 RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["yourEndpointName"] 获取
  • 是的 astaykov,我正在从 InstanceEndpoints 获取端点。它指向 127.255.0.1。那是我从客户端使用的IP。它不时起作用。
  • 给你的问题有意义标题,而不仅仅是标签列表。

标签: .net wcf azure net.tcp self-hosting


【解决方案1】:

由于您的问题是零星的,并且更改端口可以工作一段时间,因此 net.tcp 端口共享可能与服务发生冲突。因此,由于您在本地使用基于 net.tcp 的 WCF 应用程序(在 Web 或 Worker 角色中),因此请确保启用 net.tcp 端口共享服务以有效运行。

此外,在您的服务启动代码中,您可以正确设置绑定到基本 IP 地址和端口,如下所示:

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();

【讨论】:

  • 我启用了端口共享,是的,我正在使用 CurrentRoleInstance 获取 IP 和端口。我正在编辑我的帖子以添加一些代码
【解决方案2】:

“我发现发生了什么:分配给工作角色的 IP 是动态的。有时是 127.255.0.0,有时是 127.255.0.1”

我认为你犯了和我一样的错误。见这里:

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

【讨论】:

    猜你喜欢
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2015-01-28
    • 2011-06-20
    • 1970-01-01
    • 2012-02-29
    相关资源
    最近更新 更多