2009-11-01 81 views
11

我创建了一个WCF Serice,在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. 
+0

什么是你的代码创建代理? – Dani 2009-11-01 21:51:31

+0

productsServiceHost = new ServiceHost(typeof(Products.ProductsService)); productsServiceHost.Open(); stop.IsEnabled = true; start.IsEnabled = false; status.Text =“服务正在运行...”; – Attilah 2009-11-01 22:00:44

回答

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> 

现在,“HttpGetEnabled”有基址http://localhost.8080/YourService去摆脱的元数据。

或者,如果你不喜欢这一点,再次,该错误信息是你选择的很清楚:定义一个绝对URL在您ServiceMetadata的HttpGetUrl:

<serviceBehaviors> 
    <behavior name="Default"> 
     <serviceMetadata 
      httpGetEnabled="true" 
      httpGetUrl="http://localhost:8282/YourService/mex" /> 
    </behavior> 
    </serviceBehaviors> 

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

如果你从IIS来了,还没有适应任何东西,你既没有基址,也不是为你的元数据交换终结的明确,绝对URL,所以这就是为什么你得到你所看到的错误。

马克

+1

我在这里结束了同样的问题,但不是因为我不知道该怎么做。相反,我不知道要在我的配置中添加“基址”。您使用了哪些资源来确定“”和“”属性的存在和语法? – Matt 2016-01-03 00:12:46

0

,当我试图使用net.pipe binding.In我来说,我遇到这个错误,默认服务行为发布的服务元数据,这是我的错误的原因。我的解决方案是为您的服务使用不同的行为。 ,然后我根据@marc_s回答改变了我的配置文件,使不同的服务行为如下:

<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> 
0

检查服务类是正确的。

它解决了我的问题

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