REST下的WCF的寄宿方式

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

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

1
2
3
4
5
6
7
8
9
10
11
12
[ServiceContract]
public interface ILog
{
[OperationContract]
[WebGet(UriTemplate = "/")]
List<LogEntity> GetAll();
 
[OperationContract]
[WebGet(UriTemplate = "/Log/{year}/{month}")]
List<LogEntity> GetMonthLog(string year, string month);
 
}

  

数据契约定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[DataContract]
public class LogEntity
{
[DataMember]
public int ID { getset; }
 
[DataMember]
public string EventName { getset; }
 
[DataMember]
public string Level { getset; }
 
[DataMember]
public DateTime Time { getset; }
 
}

  

服务的实现代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#region ILog 成员
 
public List<LogEntity> GetAll()
{
return GetLogEntityList();
}
 
public List<LogEntity> GetMonthLog(string year, string month)
{
List<LogEntity> logEntities = GetLogEntityList();
List<LogEntity> logList = new List<LogEntity>();
logEntities.ForEach(log =>
{
if (log.Time.Year.ToString() == year && log.Time.Month.ToString() == month)
{
logList.Add(log);
}
});
return logList;
 
}
 
#endregion

  


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

1
2
3
4
5
6
7
8
9
10
11
public List<LogEntity> GetAll()
{
LogServices services = new LogServices();
return services.GetAll();
}
 
public List<LogEntity> GetMonthLog(string year, string month)
{
LogServices services = new LogServices();
return services.GetMonthLog(year, month);
}

  

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

1
2
3
4
5
6
7
8
9
10
11
12
static void HostViaCode()
{
using (WebServiceHost host = new WebServiceHost(typeof(LogServices)))
{
ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ILog), new WebHttpBinding(),
"http://127.0.0.1:6688/LogService");
endpoint.Behaviors.Add(new WebHttpBehavior());
Console.WriteLine("服务开启...");
host.Open();
Console.ReadLine();
}
}

  


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<system.serviceModel>
<behaviors>
<serviceBehaviors >
<behavior name="RESTServiceBehavior"></behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="servierBehavior"></behavior>
</endpointBehaviors>
</behaviors>
 
<services>
<service name="Services.LogServices" behaviorConfiguration="RESTServiceBehavior">
<endpoint address="Http://127.0.0.1:8866/LogService" contract="Contracts.ILog" binding="webHttpBinding" behaviorConfiguration="servierBehavior"></endpoint>
</service>
</services>
</system.serviceModel>

  

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

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

  

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


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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private const string address = "Http://127.0.0.1:8866/LogService";
 
static void Main(string[] args)
{
InvokeRESTService();
Console.ReadLine();
}
 
static void InvokeRESTService()
{
HttpWebRequest webRequest = WebRequest.Create(address) as HttpWebRequest;
webRequest.Method = "GET";
HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
using (StreamReader reader=new StreamReader(webResponse.GetResponseStream()) )
{
Console.WriteLine(string.Format("获取的调用结果为:{0}",reader.ReadToEnd()));
}
}

  


不管是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思想。

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

原文地址:https://www.cnblogs.com/cmblogs/p/6812774.html