我有以下 WCF 服务配置文件。配置中定义了一个主机。尽管如此,当我从客户端打印服务地址时,它并不知道主机。打印结果为:
为什么不考虑主机名?我们需要对其进行哪些修改?
注意:我从 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 是正确的:
如您所见,服务的基址成为 Web 服务器上 SVC 文件的 URL。
关于c# - WCF 客户端不考虑主机地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8401196/