创建一个既支持WSDL又支持REST的WCFWebService

 

创建一个既支持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的时候,请求和应答的截屏如下:

完整的服务端及客户端调用程序,在win7+ vs2015环境运行通过. 一、说明 1、创建winfrom应用程序;(或者是控制台项目) 2、在项目中添加一个WCF服务,并实现服务; 3、在需要启动WebService服务的地方启动该服务即可; 二、代码如下: 1、新建一个WCF服务——定义服务接口    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]     public interface ICalculator     {         [OperationContract]         double Add(double n1, double n2);     } 2、新建一个WCF服务——实现服务 public class CalculatorService : ICalculator     {         public double Add(double n1, double n2)         {             return n1 + n2;         }     } 3、添加完WcF服务后会在应用程序配置文件中有入下节点                             <!--TestServer.ICalculator服务定义的接口,根据自己定义进行修改-->                                                                   <baseAddresses> <!--这个是要发布的服务地址,可以进行修改-->                                   </baseAddresses>                   4、在要启动服务的地方启动服务监听   public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { try { //打开服务创建监听,并开始监听消息 ServiceHost serviceHost = new ServiceHost(typeof(Service1));//需要using System.ServiceModel; serviceHost.Open(); label1.Text = "服务启动正常"; } catch (Exception ex) { label1.Text = ex.Message; } } 5、下面可以在客户端通过上面的服务地址”http://xxx.xxx.xxx.xx:8733/test/Service1/“对服务进行调用 到这步就实现在控制台中实现webService的发布。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值