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 条评论
登录 后参与评论

相关文章

来自专栏Netkiller

CentOS 7.3 + Server JRE 1.8 + Hadoop-2.8.0

本文节选自《Netkiller Linux 手札》由于工作的关系最近几年没有使用hadoop,今天看了一下版本已经到了2.8, 跟数年前用的版本差异还是很大的。...

4656
来自专栏三木的博客

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

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

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

《Spring Boot极简教程》第3章 一键启动应用程序本章源代码

首先安装Spring Boot CLI,参考文档:http://docs.spring.io/spring-boot/docs/current-SNAPSHOT...

811
来自专栏我爱编程

conda update

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

1052
来自专栏潇涧技术专栏

Android NDK and OpenCV development with Android Studio

Android NDK and OpenCV development with Android Studio

732
来自专栏pangguoming

IIS 安装 pydio

Introduction In this how-to, we’ll see the installation of Pydio (Put Your Data ...

39311
来自专栏Netkiller

Spring RestFul Example (实例参考)

以下节选择《Netkiller Java 手札》 11.8. Spring4 Restful @RestController 首先我要禁告各位,Spring发展...

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

第3章 Spring Boot快速开始Hello World本章源代码

首先安装Spring Boot CLI,参考文档:http://docs.spring.io/spring-boot/docs/current-SNAPSHOT...

974
来自专栏我是攻城师

Spring Boot开发之明月千城(一)

3313
来自专栏专业duilib使用+业余界面开发

Error code of Media server (MediaPlayer内核异常码说明)

3764

扫码关注云+社区