我最近将 WCF SOAP 服务转换为 REST/JSON 服务。正如回复中详细说明的here https://stackoverflow.com/questions/10232509/can-i-generate-a-service-reference-automatically-for-a-rest-wcf-service,Visual Studio 的“添加服务引用”功能无法为 JSON 服务生成代码。该问题的答案中的链接文章暗示了将 WCF 公开为 REST 和 SOAP 来解决该问题的可能性,但它没有提供任何详细信息。
有谁知道这是否可行,如果可以,如何设置?
总是可以选择自己编写一个代码生成器,它读取 WCF REST 服务的 WSDL 并生成 C# 类,但似乎应该有比这更简单的解决方案。
以供参考,
我的网络配置:
<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)]
有可能,您需要创建两个端点:一个用于soap,另一个用于json。我已经在我的项目中做到了这一点,并且可以通过 SoapUI 或 Fiddler (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”访问SAP,并通过JSON访问
“本地主机:7689/MyService.svc/MyMethod”
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)