【问题标题】:WCF Client does not consider Host AddressWCF 客户端不考虑主机地址
【发布时间】:2012-01-14 03:08:27
【问题描述】:

我有以下 WCF 服务的配置文件。配置中定义了一个主机。尽管如此,当我从客户端打印服务地址时,它并不知道主机。打印结果是:

http://localhost:3187/Service1.svc/MyFolder

为什么不考虑主机名?我们需要对其进行哪些修改?

注意:我从 VS 2010 运行服务和客户端网站。

        Service1Client myClientService = new Service1Client();
        Response.Write(myClientService.Endpoint.Address);

客户端配置(由 Visual Studio 自动生成)

    <client>
  <endpoint address="http://localhost:3187/Service1.svc/MyFolder"
    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
    contract="MyWCFReference.IService1" name="WSHttpBinding_IService1">
    <identity>
      <userPrincipalName value="U16990@ustr.com" />
    </identity>
  </endpoint>
</client>

服务器端配置为:

<services>

  <!--MyService-->
  <service name="MyWCFServiceApplication.MyService"
           behaviorConfiguration="WeatherServiceBehavior">

    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:80/ServiceModelSamples/FreeServiceWorld"/>
      </baseAddresses>
    </host>

    <endpoint address="MyFolder"
              binding="wsHttpBinding"
              contract="MyWCFServiceApplication.IService1" />

    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>

</services>


  <behaviors>

  <serviceBehaviors>

    <behavior name="WeatherServiceBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="False"/>
    </behavior>

  </serviceBehaviors>

</behaviors>

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

【问题讨论】:

标签: c# asp.net .net wcf visual-studio-2010


【解决方案1】:

您说的是 WCF 客户端 - 然而,您发布的配置仅包含 service(服务器端)的配置...(&lt;services&gt; 部分)。

我在您发布的内容中看不到任何 client 配置 - 您的配置中应该有一个 &lt;client&gt; 部分

【讨论】:

  • 更新了帖子(由 Visual Studio 自动生成)
  • @Lijo:所以该服务在 IIS 中托管在同一台机器上运行?那么是的——WCF 将使用localhost——我认为你无法改变这一点。为什么这对你来说是个问题??
  • 我正在使用 Visual Studio 来托管服务。之后我需要将它移到另一台服务器上并需要在 IIS 7 中安装它。>>> 这不是问题..我想了解行为。我需要这些知识才能在不同服务器的 IIS 中托管。
  • @Lijo 在 IIS 中托管 WCF 服务时,baseAddresses 配置设置将被忽略。详情请参阅我的回答。
  • @Lijo:如果您在 VS Web 服务器 (Cassini) 中托管,您将始终看到 localhost 已使用 - Cassini 不支持其他任何东西(仅适用于本地连接)。如果您将服务移动到“真实”服务器,那么如果您定义客户端,则需要使用“真实”服务器的名称(或 IP 地址)来定义服务的位置
【解决方案2】:

当 WCF 服务托管在 ASP.NET 进程中时,通过 IIS 或 ASP.NET 开发服务器(又名 Cassini),服务配置文件中的baseAddresses setting 被忽略,因为该服务将始终可以通过 SVC 文件的 URL 访问

因此,您在客户端上看到的 URL 是正确的:

http://localhost:3187/Service1.svc/MyFolder

如您所见,服务的基地址变成了 Web 服务器上 SVC 文件的 URL

【讨论】:

    猜你喜欢
    • 2015-09-12
    • 2023-01-10
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 2014-11-29
    • 1970-01-01
    相关资源
    最近更新 更多