C# · 12月 29, 2021

c# – WCF配置地狱?

我讨厌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我得到以下错误:

The message with To
‘http://localhost:22059/ZombieService.svc/KNownZombies’ cannot be
processed at the receiver,due to an AddressFilter mismatch at the
EndpointDispatcher. Check that the sender and receiver’s
EndpointAddresses agree.

更新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我在浏览器中收到以下消息:

The message with To
‘http://localhost:22059/ZombieService.svc/KNownZombies’ cannot be
processed at the receiver,due to an AddressFilter mismatch at the
EndpointDispatcher. Check that the sender and receiver’s
EndpointAddresses agree.

解决方法 看看 this SO question.

编辑:由于您没有为服务指定地址,请尝试点击:http://localhost:22059/ZombieService.svc/KnownZombies(使用.svc).

我也认为你需要< webHttp />行为添加到指定的端点行为.

编辑:尝试将端点定义更改为:

<service name=”HighOnCodingWebApps.ZombieService” behaviorConfiguration=”MyServiceTypeBehaviors”> <endpoint address=”” binding=”webHttpBinding” behaviorConfiguration=”SomeBehavior” bindingConfiguration=”default” contract=”HighOnCodingWebApps.IZombieService” /></service>