无法连接到工作者角色中托管的WCF服务

时间:2011-08-18 12:45:47

标签: wcf service azure role worker

我已经创建了一个WCF服务,并通过辅助角色在云中托管它。不幸的是,当我尝试连接到worker角色服务时,我收到一条消息的异常: “主机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");
            }
        }
    } 

使用端口8001和9001配置tcppoint和mexinput。还使用辅助角色部署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时,会发生连接超时,但远程计算机没有响应。

2 个答案:

答案 0 :(得分:0)

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

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

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

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

如果您在角色准备好之前尝试过早地点击端点,我也会看到DNS错误。它可能需要一点点传播DNS,你应该尽量不解决它,直到它准备好,以免你缓存虚假的DNS条目。如果您可以将该DNS名称解析为您的VIP地址,则不是问题。

答案 1 :(得分:-2)

    public void CallWebService(string data)
    {

        try
        {
            string uri = "url"+data;

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream str = response.GetResponseStream();

            StreamReader sr = new StreamReader(str);

            String IResponse = sr.ReadToEnd();


        }
        catch (Exception ex)
        {
            System.Console.WriteLine("Message: "+ex.Message);
        }

    }

希望它对你有所帮助。