JSON和SOAP WCF服务? [英] JSON and SOAP WCF Service?

查看:113
本文介绍了JSON和SOAP WCF服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近转换一个WCF SOAP服务为REST / JSON服务。正如在响应<一href="http://stackoverflow.com/questions/10232509/can-i-generate-a-service-reference-automatically-for-a-rest-wcf-service">here, Visual Studio中的添加服务引用功能是无能的发电$ C $下一个JSON服务。在这个问题的答案链接的文章暗示其公开为REST和SOAP来解决这一问题的WCF的可能性,但它并没有透露任何细节如何做到这一点。

有谁知道这是可能的,如果是,如何设置?

总会有写一个code生成自己,它读取的WSDL WCF REST服务并生成C#类的另类,但看起来应该有比这更容易的解决方案。

有关参考, 我的web.config:

 &LT; system.serviceModel&GT;
&LT;行为&GT;
  &LT; endpointBehaviors&GT;
    &LT;行为NAME =RestBehavior&GT;
      &LT; webHttp /&GT;
    &LT; /行为&GT;
  &LT; / endpointBehaviors&GT;
  &LT; serviceBehaviors&GT;
    &LT;行为NAME =GetBehavior&GT;
      &LT; serviceMetadata httpGetEnabled =真/&GT;
      &LT; serviceDebug
         httpHelpPageEnabled =真
         includeExceptionDetailInFaults =真
      /&GT;
    &LT; /行为&GT;
  &LT; / serviceBehaviors&GT;
&LT; /行为&GT;
&LT;绑定&GT;
  &LT;的WebHttpBinding&GT;
    &LT;绑定名称=的WebHttpBinding&GT;
      &LT;安全模式=TransportCredentialOnly&GT;
        &LT;交通运输clientCredentialType =无/&GT;
      &LT; /安全&GT;
    &LT; /装订&GT;
  &LT; /的WebHttpBinding&GT;
&LT; /绑定&GT;
&LT;服务&GT;
  &LT;服务名称=服务behaviorConfiguration =GetBehavior&GT;
    &LT;端点地址=
      绑定=的WebHttpBinding
      behaviorConfiguration =RestBehavior
      合同=IService
      bindingConfiguration =的WebHttpBinding&GT;
    &LT; /端点&GT;
  &LT; /服务&GT;
&LT; /服务&GT;
&LT; /system.serviceModel>
 

我的服务方法都具有以下属性:

  [WebInvoke(RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped)
 

解决方案

据我知道你有三个选择从WCF结合JSON和XML响应,他们都不是可取的:

  1. 在写具体的方法(和enpoints)对XML和JSON,并相应地装饰一番
  2. 勾入序列化引擎并结合这与的检查接受的http请求头。
  3. 让你的方法接受并返回一个 <一个href="http://stackoverflow.com/questions/8308449/making-of-json-webservice-using-c-sharp-net/8308582#8308582">which按照惯例告诉WCF给你直接访问请求和响应流。

每个方法都有它自身的缺陷:  1.这是丑陋的,违反了休息的想法,因为序列化应该使用HTTP头被确定,而不是URL(应标识资源)。  2.它是复杂的附加到序列化引擎,你仍然需要看一下头,以确定序列化。  3.您得到映射请求参数和序列化杂波你身体的方法没有任何帮助。

我一直在使用方法3在很多场合,因为它是简单和容易上手,但方法2似乎是更正确。

I recently converted a WCF SOAP service into REST/JSON service. As detailed in the response here, the Add Service Reference feature of Visual Studio is incapable of generating code for a JSON service. The linked article in that question's answer alludes to the possibility of having a WCF exposed as both REST and SOAP to solve the issue, but it doesn't give any details how to do so.

Does anyone know if this is possible and if so, how to set it up?

There's always the alternative of writing a code generator myself, which reads the WSDL for the WCF REST service and generates C# classes, but it seems like there should be an easier solution than this.

For reference, My web.config:

<system.serviceModel>
<behaviors>
  <endpointBehaviors>
    <behavior name="RestBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="GetBehavior" >
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug
         httpHelpPageEnabled="true"
         includeExceptionDetailInFaults="true"
      />
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <webHttpBinding>
    <binding name="WebHttpBinding">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="None" />
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<services>
  <service name="Service" behaviorConfiguration="GetBehavior">
    <endpoint address=""
      binding="webHttpBinding"
      behaviorConfiguration="RestBehavior"
      contract="IService"
      bindingConfiguration="WebHttpBinding">
    </endpoint>
  </service>
</services>
</system.serviceModel>

My service methods all have the following attribute:

[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]

解决方案

As far as I know you have three options for combining json and xml responses from WCF and neither of them are desirable:

  1. Write specific methods (and enpoints) for XML and JSON and decorate them accordingly
  2. Hook into the serialization engine and combine this with an inspection of the accept header from the http request.
  3. Let your method accept and return a Stream which by convention tells WCF to give you direct access to the request and response streams.

Each method has it's own flaws: 1. This is ugly and contrary to the ideas of Rest, since the serialization should be determined using http headers, not the URL (which should identify the resource). 2. It is complicated to attach to the serialization engine and you still need to look at the headers to determine the serialization. 3. You get no help in mapping request parameters and the serialization clutters you method body.

I have been using method 3 on many occasions because it is simple and easy to get started with, but method 2 seems to be more correct.

这篇关于JSON和SOAP WCF服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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