加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WCF配置地狱?

发布时间:2020-12-15 03:42:09 所属栏目:百科 来源:网络整理
导读:我讨厌WCF设置端点,行为等等.我相信所有这些都应该自动执行.所有我想做的是从我的WCF服务返回 JSON结果.这是我的配置: system.serviceModelbindings webHttpBinding binding name="default"/ /webHttpBinding/bindingsservices service name="HighOnCodingW
我讨厌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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读