在配置文件中应用[WebInvoke(ResponseFormat = WebMessageFormat.Json)] [英] Applying [WebInvoke(ResponseFormat = WebMessageFormat.Json)] in config file

查看:155
本文介绍了在配置文件中应用[WebInvoke(ResponseFormat = WebMessageFormat.Json)]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写我所谓的POJ(普通JSON)WCF Web服务-一种接受和发出标准JSON的服务,没有ASP.NET Ajax喜欢添加的废话.

I'm writing what I'm referring to as a POJ (Plain Old JSON) WCF web service - one that takes and emits standard JSON with none of the crap that ASP.NET Ajax likes to add to it.

看来要完成此操作需要三个步骤:

It seems that there are three steps to accomplish this:

  1. 在端点的标记中将"enableWebScript"更改为"webHttp"
  2. 使用[WebInvoke(ResponseFormat = WebMessageFormat.Json)]装饰方法
  3. 添加[AspNetCompatibilityRequirements(RequirementsMode =服务协议的AspNetCompatibilityRequirementsMode.Allowed)]
  1. Change "enableWebScript" to "webHttp" in the endpoint's tag
  2. Decorate the method with [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
  3. Add an incantation of [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] to the service contract

这对我来说一切正常-我可以通过并返回漂亮的纯JSON.

This is all working OK for me - I can pass in and am being returned nice plain JSON.

如果删除WebInvoke属性,那么将返回XML,因此它确实在执行应做的事情.但是让我感到奇怪的是,指定JSON输出的选项出现在此处而不是出现在配置文件中.假设我也想将我的方法公开为XML端点-我该怎么做?目前,我只能看到的第二种方法是执行完全相同的操作,但未指定WebMethodFormat.Json.然后冲洗并为我的服务中的每种方法重复一次吗? uck.

If I remove the WebInvoke attribute, then I get XML returned instead, so it is certainly doing what it is supposed to do. But it strikes me as odd that the option to specify JSON output appears here and not in the configuration file. Say I wanted to expose my method as an XML endpoint too - how would I do this? Currently the only way I can see would be to have a second method that does exactly the same thing but does not have WebMethodFormat.Json specified. Then rinse and repeat for every method in my service? Yuck.

在属性中指定将输出序列化为JSON似乎完全与WCF的原理相反,在WCF中,服务的实现是一种传输和编码不可知的方式,从而留下了如何移动数据的令人讨厌的细节到配置文件.

Specifying that the output should be serialized to JSON in the attribute seems to be completely contrary to the philosophy of WCF, where the service is implemented is a transport and encoding agnostic manner, leaving the nasty details of how the data will be moved around to the configuration file.

是否有更好的方式来做我想做的事?还是我们坚持使用这个笨拙的属性?还是我对WCF不够深入?

Is there a better way of doing what I want to do? Or are we stuck with this awkward attribute? Or do I not understand WCF deeply enough?

推荐答案

我还没有对它进行完整的测试,但是,我研究了反射器中的WebMessageFormat及其在代码中的使用位置.

I haven't fully tested this out yet, BUT, I took a look at WebMessageFormat in reflector and where it was used in code.

webHttp元素的一个名为defaultOutgoingResponseFormat的属性可以设置为"Json"或"Xml".

There is a attribute of the webHttp element called defaultOutgoingResponseFormat that can be set to "Json" or "Xml".

    <behaviors>
      <endpointBehaviors>
        <behavior name="ServicesJSONEndpointBehavior">
          <webHttp defaultOutgoingResponseFormat="Json"/>
        </behavior>
</behaviors>

在没有太多信息的情况下进行在线搜索后,我遇到了同样的问题,并且通常无法解决问题.

I've run into the same issue and typically crufted work-arounds after searching online without much info.

我将为它提供多个已配置的endpointBehaviors的快照,并进行报告.

I'll give it a shot with multiple configured endpointBehaviors and report back.

更新6/5/2011

仅供参考-我已经放弃了香草WCF及其所有类似的拔毛方案,而这些方案应该简单,而采用了ServiceStack(

FYI -- I've ditched vanilla WCF with all its hair-pulling scenarios like this that should be simple, in favor of ServiceStack ( http://servicestack.net/ ). If you're looking to put together a standard REST style / document-oriented service over HTTP that out-of-the-box supports JSON / XML / CSV (and in the future protocol buffers) and that allows you to put together clean MVC-style routes with ease, give ServiceStack a hard look. There are a number of things that ServiceStack will handle rather easily and cleanly that always turn out to be a major PITA due to some wonky configuration issue or similar in standard WCF. ServiceStack uses it's own JSON serializer, which as an added bonus, outperforms DataContractJsonSerializer and JSON.NET as Demis mentions below.

这篇关于在配置文件中应用[WebInvoke(ResponseFormat = WebMessageFormat.Json)]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆