【问题标题】:WCF discovery finds the endpoint but the address is localhostWCF 发现找到端点,但地址是 localhost
【发布时间】:2010-09-17 03:26:55
【问题描述】:

我有一个名为“GetNameService”的 wcf 可发现服务,它托管在 PC @ 10.0.0.5:8732 上。它由 wsHttpBinding 托管,可通过 UdpEndpoint 发现。我还有一个客户端 @ 10.0.0.9,它在同一网络中发现此类服务。当我运行客户端时,我能够发现该服务,但发现的服务的终点是其中包含 localhost。怎么会这样,请帮帮我。

更多信息,

使用了 App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

 <system.web>
  <compilation debug="true" />
 </system.web>
 <!-- When deploying the service library project, the content of the config file must be added to the host's
 app.config file. System.Configuration does not support config files for libraries. -->
 <system.serviceModel>
  <services>
   <service name="NameService.GetNameService">
    <host>
     <baseAddresses>
      <add baseAddress = "http://10.0.0.5:8732/Design_Time_Addresses/NameService/GetNameService/" />
     </baseAddresses>
    </host>
    <!-- Service Endpoints -->
    <!-- Unless fully qualified, address is relative to base address supplied above -->
    <endpoint address ="" binding="wsHttpBinding" contract="NameService.IGetNameService">
     <!--
       Upon deployment, the following identity element should be removed or replaced to reflect the
       identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
       automatically.
     -->
     <identity>
      <dns value="localhost"/>
     </identity>
    </endpoint>
    <!-- Metadata Endpoints -->
    <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
    <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
   </service>
  </services>
  <behaviors>
   <serviceBehaviors>
    <behavior>
     <!-- To avoid disclosing metadata information,
     set the value below to false and remove the metadata endpoint above before deployment -->
     <serviceMetadata httpGetEnabled="True"/>
     <!-- To receive exception details in faults for debugging purposes,
     set the value below to true. Set to false before deployment
     to avoid disclosing exception information -->
     <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
   </serviceBehaviors>
  </behaviors>
 </system.serviceModel>

</configuration>

我在客户端机器中打开浏览器并输入服务地址,我可以看到服务详细信息页面,但 wsdl 链接仍然指向 localhost(如下所示)

svcutil.exe http://localhost:8732/Design_Time_Addresses/NameService/GetNameService/?wsdl

而不是

svcutil.exe http://10.0.0.5:8732/Design_Time_Addresses/NameService/GetNameService/?wsdl

此外,同样的应用程序似乎在我的工作场所工作,那里有一个可用的 dns,系统通过一个交换机连接,因为它在我家里的电脑不工作通过路由器连接。这会影响什么吗?!?

更新 它不在 IIS 中运行,它是通过普通控制台应用程序托管的

【问题讨论】:

    标签: c# network-programming wcf service-discovery


    【解决方案1】:

    如果您在 IIS 上运行,则必须为您的站点设置默认主机名规则:

    1. 打开 IIS
    2. 右键单击您的网站,然后单击“属性”
    3. 单击“网站”选项卡,然后单击“网站标识”标题下的“高级”按钮
    4. 您应该有网站的默认身份(TCP 端口 8732)。编辑它并确保主机头值是您网站的域名(所以它应该是 www.yourdomain.com)

    更多详情请参阅http://forums.asp.net/p/1096811/1659596.aspx

    【讨论】:

    • 它不是托管在 IIS 上,而是通过控制台应用程序托管
    【解决方案2】:

    通过使用在代码中解决它(用于 tcp.net 连接)。

    // Add discovery feature.
    m_Host.AddServiceEndpoint(new Discovery.UdpDiscoveryEndpoint());
    m_Host.Description.Behaviors.Add(new Discovery.ServiceDiscoveryBehavior());
    // Add ordinary service feature
    var binding = new NetTcpBinding();
    binding.Security.Message.ClientCredentialType = MessageCredentialType.None;
    binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
    m_Host.AddServiceEndpoint(
            typeof(IMyServiceContract),
            new NetTcpBinding(SecurityMode.None),
            new UriBuilder { Scheme = Uri.UriSchemeNetTcp, Port = 8732, Host = "10.0.0.5" , Path = "MyServiceContractPath" }.Uri
            );
    

    剩下的唯一(也许更小)问题是将哪台计算机 IP 设置为主机...输入的 Uri 是在 DiscoveryClient.Find(...) 响应中返回的那个。

    【讨论】:

      【解决方案3】:

      如果你仍然需要这个,事实证明,你可以在 baseAddress 中使用通配符:

      <add baseAddress = "http://*:8732/Design_Time_Addresses/NameService/GetNameService/" />
      

      【讨论】:

      • 在执行new ServiceHost(typeof(MyService), new Uri('http://*:8080/MyService')); 时,我无法在代码中使用它。它说无法解析主机名。有什么建议吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 2011-12-18
      • 1970-01-01
      • 2013-09-14
      • 2010-09-21
      相关资源
      最近更新 更多