如何将 WCF 服务从 SOAP 转换为 REST? [英] How to convert a WCF service from SOAP to REST?

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

问题描述

我对 WCF 很陌生,我对此有疑问.

I am very new to WCF and I have questions about it.

阅读一些文章后,我发现在 web.config 文件中,如果我将端点绑定从 basicHttpBinding 更改为 webHttpBinding,并将 httpGetEnabled 从 true 更改为 false,则它使用 REST.

After going through some articles I found that in the web.config file if I change the endpoint binding to webHttpBinding from basicHttpBinding and httpGetEnabled from true to false it uses REST.

我的问题是,要制作服务 SOAP 或 REST,我只需要更改这两件事吗?或者我需要更改/添加任何其他内容吗?

My question is are these the only two things that I need to change to make a service SOAP or REST? Or do I need to change/add any other things?

推荐答案

您可以在两个不同的端点公开服务.SOAP 可以使用支持 SOAP 的绑定,例如basicHttpBindingRESTful 可以使用webHttpBinding.我假设您的 REST 服务将在 JSON 中,在这种情况下,您需要使用以下行为配置配置两个端点

You can expose the service in two different endpoints. the SOAP one can use the binding that support SOAP e.g. basicHttpBinding, the RESTful one can use the webHttpBinding. I assume your REST service will be in JSON, in that case, you need to configure the two endpoints with the following behaviour configuration

<endpointBehaviors>
  <behavior name="jsonBehavior">
    <enableWebScript/>
  </behavior>
</endpointBehaviors>

您方案中的端点配置示例是

An example of endpoint configuration in your scenario is

<services>
  <service name="TestService">
    <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
    <endpoint address="json" binding="webHttpBinding"  behaviorConfiguration="jsonBehavior" contract="ITestService"/>
  </service>
</services>

[WebGet] 应用到操作合约中,使其成为 RESTful.例如

Apply [WebGet] to the operation contract to make it RESTful. e.g.

public interface ITestService
{
   [OperationContract]
   [WebGet]
   string HelloWorld(string text)
}

添加服务引用后SOAP服务的SOAP请求客户端端点配置,

SOAP request client endpoint configuration for SOAP service after adding the service reference,

<client>
    <endpoint address="http://www.example.com/soap" binding="basicHttpBinding"
      contract="ITestService" name="BasicHttpBinding_ITestService" />
  </client>

在 C# 中

TestServiceClient client = new TestServiceClient();
client.GetAccount("A123");

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

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