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

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>

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.

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

[ServiceContract(Namespace = "http://test")]
public interface ITestService
{
    [OperationContract]
    [WebGet(UriTemplate = "accounts/{id}")]
    Account[] GetAccount(string id);
}

POX behavior for REST Plain Old XML

<behavior name="poxBehavior">
  <webHttp/>
</behavior>

Endpoints

<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,

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

  in C#

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.

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏技术沉淀

Rails里应用Friendly Id

1093
来自专栏逢魔安全实验室

Some Linux Hacking Tricks

3675
来自专栏一个会写诗的程序员的博客

React.js 集成 Kotlin Spring Boot 开发 Web 应用实例详解工程源代码参考文章

React.js 集成 Kotlin Spring Boot 开发 Web 应用实例详解

1242
来自专栏数据库新发现

Oracle诊断案例-SGA与Swap之一

案例描述: 用户报告,服务器启动一段时间以后,无法建立数据库连接 重新启动几分钟以后,再次无法连接 系统无法正常使用.

1372
来自专栏我爱编程

conda update

DESCRIPTION usage: conda update [-h] [-y] [--dry-run] [-f] [--file F...

1192
来自专栏张善友的专栏

MS 的IOC容器(ObjectBuilder)?

MS 的 Net Framework 2.0的Enterprise Library - November 2005 CTP 和Composite User I...

1907
来自专栏一个会写诗的程序员的博客

《Spring Boot极简教程》第7章 Spring Boot集成模板引擎

其实,没有任何一个模板引擎(jsp,velocity,thymeleaf,freemarker,etc)可以完全实现MVC绝对的分层,只有“自由度”上的界定罢了...

983
来自专栏平凡文摘

Spring Boot 内嵌容器 Tomcat / Undertow / Jetty 优雅停机实现

3172
来自专栏三木的博客

基于Jenkins和Koji的代码管理及构建系统

<h1>Summary</h1> Currently, we KVM team are maintaining the code repository of t...

2217
来自专栏李成熙heyli

Deploy Using Travis-CI And Github Webhook — webpack doc as an example

Overview Some friends and I have been running webpack-china for a few months. A...

2298

扫码关注云+社区