创建一个既支持WSDL又支持REST的WCFWebService
首先回顾一下REST,WCF和WebService这三个基本概念。
REST:REST最核心的概念是“资源”,一个uri代表一个特定的资源。使用它的一个好处就是客户端只需要发送一个简单的http包即可操作服务器的资源,没有了SOAP协议包的复杂,以至于使用Javascript就可以轻松的调用REST的服务。
WCF:它是一个分布式应用的开发框架,它整合了.Net平台下所有的和分布式系统有关的技术,如Enterprise Sevices(COM+).Net Remoting、Web Service(ASMX)、WSE3.0和MSMQ消息队列。
WebService:它是行业标准,也就是Web Service 规范,它不是某一个框架或者技术。微软的WebService框架就是ASP.NET WebService。它是使用SOAP协议进行数据交互的,需要提供wsdl文件来描述该WebService的接口。
当前有很多的开发环境提供了对WebService调用的支持,比如,在VS.net环境只需要添加个Web引用,就可以在本地生成一个和WebService对应的代理类,本地只需要使用该代理类就可以与WebService进行交互,完全感觉不到SOAP协议的存在。但是,如果想在JavaScript中使用WebService就会比较费事了,因为需要封装一个SOAP XML的数据包。那么,有没有一个既提供WSDL文件供开发环境生成代理类,又可以在JavaScript中通过传递一个简单的Json字符串就可以轻松调用的WebService呢?当然,那就是本文需要做。
1、在VS2010中创建一个默认的WcfService项目,项目文件的一览如下。
这就是一个简单的WebService了。现在把该WebService发布到IIS中,发布后WcfService的URL为http://192.168.2.249:8084/Service1.svc。然后新建一个WinForm程序引用该WebService并调用其中的GetData方法,如下图
下面需要做的就是让该WcfService支持RESTful的格式对外提供服务。
2、为Service1类型添加AspNetCompatibilityRequirementsAtribute声明
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(string value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += " Suffix";
}
return composite;
}
}
3、修改IService1接口,使其支持JSON格式
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetData/?value={value}",
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] //使用JSON格式请求和应答
string GetData(string value);
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "GetDataUsingDataContract",
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] //使用JSON格式请求和应答
CompositeType GetDataUsingDataContract(CompositeType composite);
}
4、为WcfService添加一个Global.asax文件,并注册一个路由转换器
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
// Edit the base address of Service1 by replacing the "Service1" string below
RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
}
}
5、修改web.config文件,添加URL路由模块,打开AspNet兼容模式和添加一个standardEndpoint节点。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/> <!--打开aspnet兼容模式-->
<!--添加一个standardEndpoints节点-->
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<!--添加一个URL路由模块-->
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
</configuration>
到此,一个既支持WSDL和RESTful的WebService已经完成。
下面分别适用Soap调用和Rest调用进行测试。
测试一:在新建的WinForm工程中调用该WebService的GetDataUsingDataContract
接口(通过抓包分析,这种调用方式采用的是SOAP方式,详细分析请参考上篇文章http://blog.csdn.net/zztfj/article/details/7012252 )。
测试二:在RESTClient客户端工具中使用REST请求方式调用该WebService的GetDataUsingDataContract接口
把Content-Type设置为text/json; charset=utf-8的时候,请求和应答的截屏如下:
把Content-Type设置为text/xml; charset=utf-8的时候,请求和应答的截屏如下: