WCF 启用multipleSiteBindingsEnabled 情况下报终结点地址错误

报错信息如下:

Server Error in '/MyWcfService' Application.

When 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' is set to true in configuration, the endpoints are required to specify a relative address. If you are specifying a relative listen URI on the endpoint, then the address can be absolute. To fix this problem, specify a relative uri for endpoint 'http://127.0.0.1/MyWcfService/Calculatorservice.svc'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: When 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' is set to true in configuration, the endpoints are required to specify a relative address. If you are specifying a relative listen URI on the endpoint, then the address can be absolute. To fix this problem, specify a relative uri for endpoint 'http://127.0.0.1/MyWcfService/Calculatorservice.svc'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidOperationException: When 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' is set to true in configuration, the endpoints are required to specify a relative address. If you are specifying a relative listen URI on the endpoint, then the address can be absolute. To fix this problem, specify a relative uri for endpoint 'http://127.0.0.1/MyWcfService/Calculatorservice.svc'.]
System.ServiceModel.Activation.ApplyHostConfigurationBehavior.ThrowIfAbsolute(Uri uri) +143946
System.ServiceModel.Activation.ApplyHostConfigurationBehavior.FailActivationIfEndpointsHaveAbsoluteAddress(ServiceHostBase service) +153
System.ServiceModel.Description.DispatcherBuilder.ValidateDescription(ServiceDescription description, ServiceHostBase serviceHost) +391
System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +334
System.ServiceModel.ServiceHostBase.InitializeRuntime() +82
System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +64
System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +789
System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +255
System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +1172

[ServiceActivationException: The service '/MyWcfService/Calculatorservice.svc' cannot be activated due to an exception during compilation. The exception message is: When 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' is set to true in configuration, the endpoints are required to specify a relative address. If you are specifying a relative listen URI on the endpoint, then the address can be absolute. To fix this problem, specify a relative uri for endpoint 'http://127.0.0.1/MyWcfService/Calculatorservice.svc'..]
System.Runtime.AsyncResult.End(IAsyncResult result) +900576
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +193150
System.Web.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar) +107

配置文件:

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="metadataBehavior" name="MyWcf.MyWcfService.CalculatorService">
        <endpoint address="http://localhost:2821/Calculatorservice.svc" binding="wsHttpBinding" contract="MyWcf.MyWcfContracts.ICalculator"/>
        
      </service>
      <service behaviorConfiguration="metadataBehavior" name="MyWcf.MyWcfService.DealWithFault">
        <endpoint address="http://localhost:2821/DealWithFault.svc" binding="wsHttpBinding" contract="MyWcf.MyWcfContracts.IDealWithFault"/>
        
      </service>
    </services>

根据报错提示,在启用multipleSiteBindingsEnabled="true"的情况下,需要为终结点指定相对uri。

给定终结点地址一个base address:

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="metadataBehavior" name="MyWcf.MyWcfService.CalculatorService">
        <endpoint binding="wsHttpBinding" contract="MyWcf.MyWcfContracts.ICalculator"/>
        <host>
          <baseAddresses>
            <!--将service发布到IIS上的配置,需配置IIS虚拟目录,例如:MyWcfService-->
            <!--<add baseAddress="http://localhost/MyWcfService/Calculatorservice.svc"/>-->
            <!--将service发布到VS web deployment上的配置,项目-Property设置了虚拟目录“/”,如果发布在http://localhost/MyWcfService下,需要设置虚拟目录为“/MyWcfService”-->
            <add baseAddress="http://localhost:2821/Calculatorservice.svc"/>
          </baseAddresses>
        </host>
      </service>
      <service behaviorConfiguration="metadataBehavior" name="MyWcf.MyWcfService.DealWithFault">
        <endpoint binding="wsHttpBinding" contract="MyWcf.MyWcfContracts.IDealWithFault"/>
        <host>
          <baseAddresses>
            <!--将service发布到VS web deployment上的配置-->
            <add baseAddress="http://localhost:2821/DealWithFault.svc"/>
          </baseAddresses>
        </host>
      </service>
    
    </services>

 具体WCF的地址拼接方法相关原因可参照:http://social.msdn.microsoft.com/Forums/en-US/ef113ca3-4636-4031-b9de-f651dce07b76/wcfiis

如下:

“address在终结点里,简单理解就是地址,终结点地址。

你可以配置address为相对,也可以是全部完整格式的地址。当你有基地址时,这里可以使用相对地址,终结点的地址就是 基地址+相对地址。

当你使用完整的地址格式,那终结点地址就不会使用 基地址+相对地址。

IIS部署的时候,默认会有一个基地址Baseaddress,这个是根据你WCF服务程序的配置生成的。

如果你打算提供完成的地址格式,但是这个完成的地址格式 和Baseaddress 不匹配,比如端口不一样,就会出错。

address换成“”,目的就是使用默认的Baseaddress+“”。避免了你自己设置的和Baseaddress 不匹配的问题。”

原文地址:https://www.cnblogs.com/iwaner/p/3447852.html