c# - WCF 客户端不考虑主机地址

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

我有以下 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="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abfe9a9d92929bebded8dfd985c8c4c6" rel="noreferrer noopener nofollow">[email protected]</a>" />
    </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" />

最佳答案

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

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

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

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

关于c# - WCF 客户端不考虑主机地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8401196/

相关文章:

.net - 如何让 LinqToSql 将 “index hints” 传递给 sql server?

c# - 将对象转换为特定类型 C#

c# - 他们有什么好的方法可以通过.NET 中的 C# 生成 XML 文件吗?

c# - efcore 2.2 中的 Multi-Tenancy 配置

c# - 在 visual studio 2015 中停止正在进行的调试

.net - Object.GetHashCode() 的默认实现

c# - 文本到 Excel 中的列

c# - 在 C# 中创建自定义 TabPage 控件

javascript - 使用 javascript,客户端打开文件

在 ASP.NET 中动态表中的 HTML 固定 header