无效的操作异常

Att*_*lah 11 .net c# wcf

我创建了一个在IIS上托管时运行良好的WCF Serice.

现在,我采用相同的服务,并在WPF中创建了一个主机应用程序,当尝试从该应用程序启动服务时,我得到了以下异常:

The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the   
HttpGetUrl property is a relative address, but there is no http base address.  
Either     supply an http base address or set HttpGetUrl to an absolute address.
Run Code Online (Sandbox Code Playgroud)

mar*_*c_s 22

错误很明显 - 您正在使用HTTP,您已在ServiceMetadata行为上启用了HttpGetEnabled,但您尚未在配置中提供基地址.

在IIS中,既不需要也不使用基址,因为*.svc文件的位置定义了您的服务地址.当您进行自托管时,您可以并且应该使用基址.

将配置更改为如下所示:

<system.serviceModel>
  <services>
    <service name="YourService">
      <host>
        <baseAddresses>
           <add baseAddress="http://localhost:8080/YourService" />
        </baseAddresses>
      </host>
      <endpoint address="mex" binding="mexHttpBinding"
                contract="IMetadataExchange" />
      ..... (your own other endpoints) ...........
    </service>
  </services>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

现在,"HttpGetEnabled"有一个基地址http://localhost.8080/YourService来获取元数据.

或者,如果你不喜欢这个,那么你的备选方案的错误信息非常明确:在ServiceMetadata中定义HttpGetUrl的绝对URL:

  <serviceBehaviors>
    <behavior name="Default">
      <serviceMetadata 
           httpGetEnabled="true" 
           httpGetUrl="http://localhost:8282/YourService/mex" />
    </behavior>
  </serviceBehaviors>
Run Code Online (Sandbox Code Playgroud)

客户端可以从"mex"端点获取元数据,或者在第二个示例中定义的固定URL,或者它们将转到元数据服务的基址(如果有的话).

如果您来自IIS并且没有进行任何调整,那么您将既没有基本地址,也没有元数据交换端点的明确绝对URL,因此这就是您收到错误的原因.