C# 无效的操作异常

C# 无效的操作异常,c#,.net,wcf,C#,.net,Wcf,我创建了一个WCF服务,它在IIS上托管时运行良好 现在,我接受了相同的服务,并在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

我创建了一个WCF服务,它在IIS上托管时运行良好

现在,我接受了相同的服务,并在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.

错误很明显-您使用的是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>
客户机可以从“mex”端点获取元数据,可以是在第二个示例中定义的固定URL,也可以是元数据的服务基址(如果有)

如果您来自IIS,并且没有修改任何内容,那么您的元数据交换端点既没有基址,也没有显式的绝对URL,所以这就是您看到错误的原因


Marc

我在尝试使用net.pipe绑定时遇到了这个错误。在我的情况下,默认服务行为发布了服务元数据,这就是我的错误原因。我的解决方案是为您的服务使用不同的行为,然后我根据@marc_的答案更改了配置文件,并做出如下不同的服务行为:

<serviceBehaviors>
        <!--Default behavior for all services (in my case net pipe binding)-->
        <behavior >

          <serviceMetadata   httpGetEnabled="false" httpsGetEnabled="false" />

          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <!--for my http services -->
        <behavior name="MyOtherServiceBehavior">

          <serviceMetadata   httpGetEnabled="true" httpsGetEnabled="true" />

          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>

检查服务类别是否正确

它解决了我的问题

// Create a ServiceHost for the CalculatorService type and 
// provide the base address.
serviceHost = new ServiceHost(typeof(ServiceClass));

创建代理的代码是什么?productsServiceHost=newServiceHost(typeof(Products.ProductsService));productsServiceHost.Open();stop.IsEnabled=true;start.IsEnabled=false;status.Text=“服务正在运行…”;我在这里遇到了同样的问题,但不是因为我不知道该怎么办。相反,我不知道在配置中添加“基址”需要寻找什么。您使用了什么资源来确定
属性的存在和语法?
// Create a ServiceHost for the CalculatorService type and 
// provide the base address.
serviceHost = new ServiceHost(typeof(ServiceClass));