前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WCF 添加 RESTful 支持,适用于 IIS、Winform、cmd 宿主

WCF 添加 RESTful 支持,适用于 IIS、Winform、cmd 宿主

作者头像
庞小明
发布2018-03-08 13:43:58
1K0
发布2018-03-08 13:43:58
举报
文章被收录于专栏:pangguoming

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

代码语言:javascript
复制
<endpointBehaviors>
  <behavior name="jsonBehavior">
    <enableWebScript/>
  </behavior>
</endpointBehaviors>

  An example of endpoint configuration in your scenario is

代码语言:javascript
复制
<services>
  <service name="TestService">
    <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
    <endpoint address="json" binding="webHttpBinding"  behaviorConfiguration="jsonBehavior" contract="ITestService"/>
  </service>
</services>

so, the service will be available at

  • http://www.example.com/soap
  • http://www.example.com/json

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

代码语言:javascript
复制
public interface ITestService
{
   [OperationContract]
   [WebGet]
   string HelloWorld(string text)
}

Note, if the REST service is not in JSON, parameters of the operations can not contain complex type.

Reply to the post for SOAP and RESTful POX(XML)

For plain old XML as return format, this is an example that would work both for SOAP and XML

代码语言:javascript
复制
[ServiceContract(Namespace = "http://test")]
public interface ITestService
{
    [OperationContract]
    [WebGet(UriTemplate = "accounts/{id}")]
    Account[] GetAccount(string id);
}

POX behavior for REST Plain Old XML

代码语言:javascript
复制
<behavior name="poxBehavior">
  <webHttp/>
</behavior>

Endpoints

代码语言:javascript
复制
<services>
  <service name="TestService">
    <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
    <endpoint address="xml" binding="webHttpBinding"  behaviorConfiguration="poxBehavior" contract="ITestService"/>
  </service>
</services>

Service will be available at

  • http://www.example.com/soap
  • http://www.example.com/xml

REST request try it in browser,

http://www.example.com/xml/accounts/A123

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

代码语言:javascript
复制
<client>
    <endpoint address="http://www.example.com/soap" binding="basicHttpBinding"
      contract="ITestService" name="BasicHttpBinding_ITestService" />
  </client>

  in C#

代码语言:javascript
复制
TestServiceClient client = new TestServiceClient();
client.GetAccount("A123");

  Another way of doing it is to expose two different service contract and each one with specific configuration. This may generate some duplicates at code level, however at the end of the day, you want to make it working.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reply to the post for SOAP and RESTful POX(XML)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档