我创建了一个 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>
现在,“HttpGetEnabled”有一个基地址 http://localhost.8080/YourService
可以从中获取元数据。
或者,如果您不喜欢这样,您的替代方法中的错误消息会非常清楚:在您的 ServiceMetadata 中为 HttpGetUrl 定义一个绝对 URL:
<serviceBehaviors>
<behavior name="Default">
<serviceMetadata
httpGetEnabled="true"
httpGetUrl="http://localhost:8282/YourService/mex" />
</behavior>
</serviceBehaviors>
客户端可以从您的“mex”端点获取您的元数据,或者通过第二个示例中定义的固定 URL,或者他们将转到元数据服务的基地址(如果有的话)。
如果您来自 IIS 并且没有进行任何调整,那么您既没有基地址,也没有元数据交换端点的显式绝对 URL,因此这就是您看到错误的原因。
马克
关于c# - 无效操作异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1658455/