zoukankan      html  css  js  c++  java
  • (转)REST下的WCF的寄宿方式

    原文地址:

    http://www.cnblogs.com/tyb1222/archive/2011/10/28/2227795.html

      如同SOA下的WCF,REST架构下的WCF也有多种多样的寄宿方式,如IIS寄宿,自寄宿等等,即使它只有一种协议。
    由于REST基于HTTP协议的特点,所以这种架构下的WCF寄宿时,需要有Web服务器的支持。那么很显然,微软肯定会使用
    自己的Web服务器IIS了。
    本节目录:
    1、IIS寄宿
    2、控制台程序寄宿(暂且将它称为自寄宿)
    当然,REST WCF还有其他的寄宿方式,我这里只挑出典型的两种给大家介绍。有兴趣的朋友不妨试试其他的寄宿方式。
    本节中所使用的实例还是上节所使用的例子。Demo结构图如下:

    结构说明:Client为服务消费者,Contracts定义服务契约、数据契约,Services定义服务的实现,SelfHost、WebHost:自寄宿、IIS寄宿程序宿主程序
    在IIS或者自寄宿中使用同样的服务契约。服务契约的定义如下:

    01[ServiceContract]
    02public interface ILog
    03{
    04[OperationContract]
    05[WebGet(UriTemplate = "/")]
    06List<LogEntity> GetAll();
    07
    08[OperationContract]
    09[WebGet(UriTemplate = "/Log/{year}/{month}")]
    10List<LogEntity> GetMonthLog(string year, string month);
    11
    12}

      

    数据契约定义:

    01[DataContract]
    02public class LogEntity
    03{
    04[DataMember]
    05public int ID { get; set; }
    06
    07[DataMember]
    08public string EventName { get; set; }
    09
    10[DataMember]
    11public string Level { get; set; }
    12
    13[DataMember]
    14public DateTime Time { get; set; }
    15
    16}

      

    服务的实现代码:

    01#region ILog 成员
    02
    03public List<LogEntity> GetAll()
    04{
    05return GetLogEntityList();
    06}
    07
    08public List<LogEntity> GetMonthLog(string year, string month)
    09{
    10List<LogEntity> logEntities = GetLogEntityList();
    11List<LogEntity> logList = new List<LogEntity>();
    12logEntities.ForEach(log =>
    13{
    14if (log.Time.Year.ToString() == year && log.Time.Month.ToString() == month)
    15{
    16logList.Add(log);
    17}
    18});
    19return logList;
    20
    21}
    22
    23#endregion

      


    1、IIS寄宿。
    众所周知Web程序基于HTTP协议,REST也基于HTTP。REST架构认为:WEB很成功、很简洁,不用那么复杂。所以基于
    IIS的寄宿就很容易理解了。在IIS的寄宿中,我直接建了一个Web项目,然后在WCF 服务文件里面里提供对服务的调用。
    代码如下:

    01public List<LogEntity> GetAll()
    02{
    03LogServices services = new LogServices();
    04return services.GetAll();
    05}
    06
    07public List<LogEntity> GetMonthLog(string year, string month)
    08{
    09LogServices services = new LogServices();
    10return services.GetMonthLog(year, month);
    11}

      

    这样就实现了接口规范中定义的对外提供服务。
    2、控制台程序寄宿。
    2.1通过编码实现REST WCF 寄宿。
    寄宿代码如下:

    01static void HostViaCode()
    02{
    03using (WebServiceHost host = new WebServiceHost(typeof(LogServices)))
    04{
    05ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ILog), new WebHttpBinding(),
    07endpoint.Behaviors.Add(new WebHttpBehavior());
    08Console.WriteLine("服务开启...");
    09host.Open();
    10Console.ReadLine();
    11}
    12}

      


    2.2、通配置的方式实现REST WCF 寄宿。
    首先在配置文件中进行相关的终结点等等配置。配置文件如下:

    01<system.serviceModel>
    02<behaviors>
    03<serviceBehaviors >
    04<behavior name="RESTServiceBehavior"></behavior>
    05</serviceBehaviors>
    06<endpointBehaviors>
    07<behavior name="servierBehavior"></behavior>
    08</endpointBehaviors>
    09</behaviors>
    10
    11<services>
    12<service name="Services.LogServices" behaviorConfiguration="RESTServiceBehavior">
    13<endpoint address="Http://127.0.0.1:8866/LogService" contract="Contracts.ILog" binding="webHttpBinding" behaviorConfiguration="servierBehavior"></endpoint>
    14</service>
    15</services>
    16</system.serviceModel>

      

    对WCF SOA 架构有一点了解的同学,一看就明白了,是的,REST架构下采用自寄宿的方式进行寄宿时和SOA中没有什么不同。
    在这种配置寄宿时的代码如下:

    1using(WebServiceHost host=new WebServiceHost(typeof(LogServices)))
    2{
    3Console.WriteLine("服务开启...");
    4host.Open();
    5Console.ReadLine();
    6}

      

    这样就实现了在控制台程序中对REST WCF服务的寄宿。
    在IE中调用GetAll接口,获取到的数据如下图:


    在IE中调用GetMonthLog接口,获取到的数据如下图:

    最后,在Client中,调用服务,代码如下:

    01private const string address = "Http://127.0.0.1:8866/LogService";
    02
    03static void Main(string[] args)
    04{
    05InvokeRESTService();
    06Console.ReadLine();
    07}
    08
    09static void InvokeRESTService()
    10{
    11HttpWebRequest webRequest = WebRequest.Create(address) as HttpWebRequest;
    12webRequest.Method = "GET";
    13HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
    14using (StreamReader reader=new StreamReader(webResponse.GetResponseStream()) )
    15{
    16Console.WriteLine(string.Format("获取的调用结果为:{0}",reader.ReadToEnd()));
    17}
    18}

      


    不管是IIS寄宿,还是自寄宿,不同的寄宿方式下,客户单调用结果如下图:

    提示,通过配置的方式寄宿REST WCF 服务,虽然在服务端程序启动时,也会有一个Asp.Net Development Server
    启动,但是它对客户端对服务的调用没有影响。我们可以看到,服务端在启动后,如果关闭Asp.Net Development Server,
    客户端对服务的调用依然是成功的。

    以上分别是 REST WCF在IIS与控制台程序下的寄宿方式。同样地它一定也能在其他程序:如Windows Service程序,Winform程序中实现寄宿。
    另外,我对前三节中契约的定义有个小的变动,就是在[WebGet(UriTemplate = "/Log/{year}/{month}")]中用Log替换了Get,这样能更清晰地
    表述REST思想。

  • 相关阅读:
    FZU 2112 并查集、欧拉通路
    HDU 5686 斐波那契数列、Java求大数
    Codeforces 675C Money Transfers 思维题
    HDU 5687 字典树插入查找删除
    HDU 1532 最大流模板题
    HDU 5384 字典树、AC自动机
    山科第三届校赛总结
    HDU 2222 AC自动机模板题
    HDU 3911 线段树区间合并、异或取反操作
    CodeForces 615B Longtail Hedgehog
  • 原文地址:https://www.cnblogs.com/fcsh820/p/2228599.html
Copyright © 2011-2022 走看看