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

发布于 2024-11-30 06:15:53 字数 4292 浏览 2 评论 0原文

我创建了一个 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 时,会发生连接超时,但远程计算机没有响应。

I've created a WCF service and hosted it in cloud through a worker role. Unfortunately when I try to connect to the worker role service I get an exception with the message:
"No DNS entries exist for host 3a5c0cdffcf04d069dbced5e590bca70.cloudapp.net."
3a5c0cdffcf04d069dbced5e590bca70.cloudapp.net is the address for the worker role deployed in azure staging environment.
The workerrole.cs has the following code to expose the WCF service:

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

The tcppoint and mexinput are configured with the ports 8001 and 9001. Also Domain is configured with worker role deployment url:3a5c0cdffcf04d069dbced5e590bca70.cloudapp.net

On the client part(a console app), we are using the following configuration in 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>

The following code is built using the sample code available in msdn as background. Locally it is working fine. Unfortunately when i deploy it to cloud, the exception occurs. Moreover, when i use the virtual ip instead of the url, a connection time out occurs with the exception the remote machine did not respond.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅过去的我 2024-12-07 06:15:53

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

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

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

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

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

Looks like you have your service setup to listen on net.tcp (TCP) and your client using http bindings. I would not expect that to work even locally. I am assuming you have actually opened port 9000 in the ServiceDefinition. Remember that will be a load-balanced endpoint. Are you trying to communicate to this instance from within the deployment (inter-role) or from outside the cloud?

I have found it is a lot easier to setup the host and client (when communicating within a role) through code. Try this:

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

If you are trying to hit the service from a client outside the deployment, this still applies, but for the client building part. You will need to use the external DNS name and port defined in ServiceDefinition.

I have also seen DNS errors if you try to hit the endpoint too soon before the role was ready. It can take a bit to propogate the DNS and you should try not to resolve it until it is ready, lest you cache a bogus DNS entry. If you can resolve that DNS name however to your VIP address, that is not the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文