2013-07-21 97 views
2

我最近将WCF SOAP服务转换为REST/JSON服务。如响应here中所述,Visual Studio的“添加服务引用”功能无法为JSON服务生成代码。在这个问题的答案中的链接文章暗示有一个WCF作为REST和SOAP公开的可能性来解决这个问题,但它没有给出任何细节如何这样做。JSON和SOAP WCF服务?

有谁知道这是否可能,如果是这样,如何设置它?

自己编写代码生成器总是有其他选择,它读取WCF REST服务的WSDL并生成C#类,但似乎应该有比这更容易的解决方案。

仅供参考, 我的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> 

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

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

客户端和服务解决方案是否可以共享相同的“Models”项目? (Models == DataContracts) – lcryder

回答

0

据我知道你有合并JSON和XML的三种选择来自WCF的响应并且它们都不可取:

  1. 编写特定的方法(和enpo ints),并相应地装饰它们
  2. 钩入serialization engine并将其与来自http请求的accept标头的检查结合起来。
  3. 让你的方法接受并返回一个Streamwhich by convention tells WCF to give you direct access to the request and response streams

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

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

2

这是可能的,你需要创建两个端点:一个用于肥皂,另一个用于json。我这样做,是在我的项目,并且可以在通过了SoapUI或小提琴手(JSON)同时访问的服务,这里是例子配置:

<system.serviceModel> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="BehaviourService"> 
    <serviceMetadata httpGetEnabled="true" /> 
    <serviceDebug includeExceptionDetailInFaults="false" /> 
    </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
     <behavior name="BehaviourWebHttp"> 
     <webHttp defaultBodyStyle="Wrapped" automaticFormatSelectionEnabled="True" faultExceptionEnabled="True" /> 
     </behavior> 
    </endpointBehaviors> 
</behaviors> 

<services> 
    <service name="NameSpace.MyService" behaviorConfiguration="BehaviourService" > 
    <endpoint address ="soap" binding="basicHttpBinding" contract="NameSpace.IMyService"></endpoint> 
    <endpoint binding="webHttpBinding" behaviorConfiguration="BehaviourWebHttp" contract="NameSpace.IMyService" ></endpoint> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
    <host> 
     <baseAddresses> 
     <add baseAddress="http://localhost:7689/MyService.svc"/> 
     </baseAddresses> 
    </host> 
    </service> 
</services> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 

也会使你的合同是这样的:

[OperationContract] 
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "MyMethod", Method = "POST")] 
bool MyMethod(); 

然后您可以通过“localhost:7689/MyService.svc?wsdl”和JSON通过 “localhost:7689/MyService.svc/MyMethod”来加入SOAP。