c# – WCF配置地狱?

前端之家收集整理的这篇文章主要介绍了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.

编辑:由于您没有为服务指定地址,请尝试点击:@L_403_2@(使用.svc).

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

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

<service 
  name="HighOnCodingWebApps.ZombieService"
  behaviorConfiguration="MyServiceTypeBehaviors">

  <endpoint 
    address="" 
    binding="webHttpBinding"
    behaviorConfiguration="SomeBehavior"
    bindingConfiguration="default"
    contract="HighOnCodingWebApps.IZombieService" />

</service>
原文链接:https://www.f2er.com/csharp/94025.html

猜你在找的C#相关文章