我创建了一个 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,因此这就是您看到错误的原因。
Marc
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)