【发布时间】:2011-10-25 04:21:04
【问题描述】:
我讨厌带有端点、行为等的 WCF 设置。我相信所有这些事情都应该自动执行。我想做的就是从我的 WCF 服务返回 JSON 结果。这是我的配置:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="default"/>
</webHttpBinding>
</bindings>
<services>
<service name="HighOnCodingWebApps.ZombieService"
behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="webHttpBinding"
bindingConfiguration="default"
contract="HighOnCodingWebApps.IZombieService"
behaviorConfiguration="webScriptEnablingBehavior"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webScriptEnablingBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false"/>
我有以下服务实现:
public class ZombieService : IZombieService
{
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "KnownZombies")]
public Zombie GetZombie()
{
return new Zombie() { Name = "Mohammad Azam"};
}
}
public class Zombie
{
public string Name { get; set; }
}
当我访问http://localhost:22059/ZombieService/KnownZombies 时,会显示以下消息:
使用“UriTemplate”的端点不能与“System.ServiceModel.Description.WebScriptEnablingBehavior”一起使用。
如果我从 web.config 中删除 WebScriptEnablingBehavior,我会收到以下错误:
带有To的消息 'http://localhost:22059/ZombieService.svc/KnownZombies' 不能 由于 AddressFilter 不匹配,在接收方处理 端点调度程序。检查发送方和接收方的 EndpointAddresses 同意。
更新1:
我将配置更新为:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="default"/>
</webHttpBinding>
</bindings>
<services>
<service name="HighOnCodingWebApps.ZombieService"
behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="http://localhost:22059/ZombieService.svc" binding="webHttpBinding"
bindingConfiguration="default"
contract="HighOnCodingWebApps.IZombieService"
/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="SomeBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
现在当我访问http://localhost:22059/ZombieService.svc/KnownZombies 时,我在浏览器中收到以下消息:
带有To的消息 'http://localhost:22059/ZombieService.svc/KnownZombies' 不能 由于 AddressFilter 不匹配,在接收方处理 端点调度程序。检查发送方和接收方的 EndpointAddresses 同意。
【问题讨论】:
-
什么都没拉?您能否尝试发布一些有用的内容,或者包括一个问题?
-
查看 WCF 4.0 - 它解决了很多的配置难题!阅读A Developer's Intro to WCF 4了解更多详情。