wcf - 无法连接到辅助角色中托管的 WCF 服务

标签 wcf service azure role worker

我创建了一个 WCF 服务并通过辅助角色将其托管在云中。不幸的是,当我尝试连接到辅助角色服务时,我收到一条异常消息: “主机 3a5c0cdffcf04d069dbced5e590bca70.cloudapp.net 不存在 DNS 条目。” 3a5c0cdffcf04d069dbced5e590bca70.cloudapp.net 是部署在 azure 临时环境中的辅助角色的地址。 workerrole.cs 有以下代码来公开 WCF 服务:

    public override void Run()
    {
        using (ServiceHost host = new ServiceHost(typeof(MyService)))
        {
            string ip = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["tcppoint"].IPEndpoint.Address.ToString();
            int tcpport = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["tcppoint"].IPEndpoint.Port;
            int mexport = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["mexinput"].IPEndpoint.Port;

            // Add a metadatabehavior for client proxy generation
            // The metadata is exposed via net.tcp
            ServiceMetadataBehavior metadatabehavior = new ServiceMetadataBehavior();
            host.Description.Behaviors.Add(metadatabehavior);
            Binding mexBinding = MetadataExchangeBindings.CreateMexTcpBinding();
            string mexlistenurl = string.Format("net.tcp://{0}:{1}/MyServiceMetaDataEndpoint", ip, mexport);
            string mexendpointurl = string.Format("net.tcp://{0}:{1}/MyServiceMetaDataEndpoint", RoleEnvironment.GetConfigurationSettingValue("Domain"), 8001);
            host.AddServiceEndpoint(typeof(IMetadataExchange), mexBinding, mexendpointurl, new Uri(mexlistenurl));

            // Add the endpoint for MyService
            string listenurl = string.Format("net.tcp://{0}:{1}/MyServiceEndpoint", ip, tcpport);
            string endpointurl = string.Format("net.tcp://{0}:{1}/MyServiceEndpoint", RoleEnvironment.GetConfigurationSettingValue("Domain"), 9001);
            host.AddServiceEndpoint(typeof(IMyService), new NetTcpBinding(SecurityMode.None), endpointurl, new Uri(listenurl));
            host.Open();

            while (true)
            {
                Thread.Sleep(100000);
                Trace.WriteLine("Working", "Information");
            }
        }
    } 

tcppoint 和 mexinput 配置了端口 8001 和 9001。此外,域还配置了辅助角色部署 url:3a5c0cdffcf04d069dbced5e590bca70.cloudapp.net

在客户端部分(控制台应用程序),我们在 app.config 中使用以下配置::

    <bindings>
        <netTcpBinding>
            <binding name="NetTcpBinding_IMyService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
                maxReceivedMessageSize="65536">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:50:00"
                    enabled="false" />
                <security mode="None">
                    <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                    <message clientCredentialType="Windows" />
                </security>
            </binding>
        </netTcpBinding>

    </bindings>
    <client>
      <endpoint address="httpp:\\3a5c0cdffcf04d069dbced5e590bca70.cloudapp.net:9001/MyServiceEndpoint" binding="netTcpBinding"
         bindingConfiguration="NetTcpBinding_IMyService" contract="ServiceReference1.IMyService"
         name="NetTcpBinding_IMyService" />
    </client>

  <behaviors>
    <serviceBehaviors>
      <behavior name="behave">
        <serviceMetadata httpGetEnabled="false"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>   

</system.serviceModel>
<system.net>
  <defaultProxy useDefaultCredentials="true">
  <proxy autoDetect="False" usesystemdefault="False" bypassonlocal="True" />
</defaultProxy>

以下代码是使用 msdn 中提供的示例代码作为背景构建的。在本地它运行良好。不幸的是,当我将其部署到云端时,发生了异常。此外,当我使用虚拟 ip 而不是 url 时,会发生连接超时,但远程计算机没有响应。

最佳答案

看起来您的服务设置是使用 http 绑定(bind)来监听 net.tcp (TCP) 和客户端。我不希望这甚至在本地也能起作用。我假设您实际上已经在 ServiceDefinition 中打开了端口 9000。请记住,这将是一个负载平衡的端点。您是否尝试从部署内部(角色间)或从云外部与此实例进行通信?

我发现通过代码设置主机和客户端(在角色内通信时)要容易得多。试试这个:

http://dunnry.com/blog/2010/05/28/HostingWCFInWindowsAzure.aspx

如果您尝试从部署外部的客户端访问服务,这仍然适用,但适用于客户端构建部分。您将需要使用 ServiceDefinition 中定义的外部 DNS 名称和端口。

如果您在角色准备就绪之前过早尝试访问端点,我还会看到 DNS 错误。传播 DNS 可能需要一些时间,您应该在准备好之前尝试不要解析它,以免缓存虚假的 DNS 条目。但是,如果您可以将该 DNS 名称解析为您的 VIP 地址,那就不是问题了。

关于wcf - 无法连接到辅助角色中托管的 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7107621/

相关文章:

linux - 无法停止 mpd 服务(没有发现/usr/bin/mpd 正在运行;没有被杀死)

java - 如何从 Java Functions Worker 获取 Java 堆?

python - 如何将文件夹从 AzureML 笔记本文件夹下载到本地或 Blob 存储?

azure - 将 Azure 策略定义部署到管理组权限

没有 IIS 的 c# rest webservice

c# - 可以通过 SSL 通过 WCF 使用 Kerberos token 配置文件吗?

javascript - sessionStorage 中的 Json 与对象数组

java - EJB - 如何将复杂对象传输到表示层?

java - java servicewrapper 的替代品?

c# - 添加路由以消除 REST 服务中的 .SVC 不起作用