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 不匹配的問題。”


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM