学习前言
这次WCF的学习是通过一个小Demo进行的。实例创建是在DOS 窗口发布的服务和调试,我想不管形式怎样WCF的实
质精髓是没有变动的,所以暂时就使用DOS吧。
通过在ITOO项目中的接触的实现WCF Demo 我对它的认识目前是这样的:WCF是一种面向服务的编程方式,它将服
务托管(Hosting)到到宿主或(包含在宿主进程),通过终结点(endpoint)的配置可以使Client 端拿到发布在宿
主(IIS、或本地发布)中的服务。
在实现Demo的时候它的实现过程是这样的:
首先在Service端创建一个对外接口,该接口中的所有方法被Service端的类实现;
其次在创建一个托管类(0.0),在该类中构造Service服务对象,并且具有服务(功能、方法)。此外在该类中
还需要为服务对象配置终结点信息(A+B+C)。
最后创建Client端,首先使用代理方法,通过System.ServiceModel的Channels属性拿到托管的服务,然后实例
化代理,在客户端调用Servcie发布的服务。
代码实现:
需要说明的是无论是服务、托管、客户端都引用了System.ServiceModel,此外托管类和Clint端引用了
System.ServiceModel的channels属性。
Service:
实现类:
<span style="font-family:SimSun;font-size:18px;">namespace HelloService
{
public class HelloService :IHelloService
{
/// <summary>
/// 打招呼服务。sayHello 去实现对应的接口
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string SayHello(string name)
{
return name + "说:我有千百种方法逗笑你!";
}
}
}</span>
接口类:
<span style="font-family:SimSun;font-size:18px;">namespace HelloService
{
/// <summary>
/// 服务契约
/// </summary>
[ServiceContract]
public interface IHelloService
{
/// <summary>
/// 服务操作,【operationcontract】作用: 保证SayHello写人服务中。
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[OperationContract]
string SayHello(string name);
}
}
</span>
ServiceHost:
<span style="font-family:SimSun;font-size:18px;">namespace HelloServiceHost
{
class Program
{
static void Main(string[] args)
{
using (MyHelloHost Host = new MyHelloHost())
{
Host.open();
Console.Read();
}
}
}
//IDisposable 进程结束可释放占用的资源。
public class MyHelloHost : IDisposable
{
/// <summary>
/// 定义一个服务对象
/// </summary>
private ServiceHost _myhost;
/// <summary>
/// 定义一个基地址
/// </summary>
public const string BaseAddress = "net.pipe://localhost";
/// <summary>
/// 可选地址
/// </summary>
public const string HelloServiceAddress = "Hello";
//服务契约实现类型
public static readonly Type ServiceType= typeof(HelloService.HelloService);
//服务契约接口;和服务端的服务绑定
public static readonly Type ContractType = typeof(HelloService.IHelloService);
/// <summary>
/// 服务定义一个绑定
/// </summary>
public static readonly Binding hellobingding = new NetNamedPipeBinding();
/// <summary>
/// 构造服务对象
/// </summary>
protected void CreateHelloServiceHost()
{
//创建服务对象
_myhost = new ServiceHost(ServiceType, new Uri[] { new Uri(BaseAddress) });
//添加终结点
_myhost.AddServiceEndpoint(ContractType,hellobingding,HelloServiceAddress );
}
public ServiceHost Myhost
{
get { return _myhost; }
}
/// <summary>
/// 打开服务
/// </summary>
public void open ()
{
Console.WriteLine("开始启动服务");
_myhost.Open();
Console.WriteLine("服务已经启动....");
}
/// <summary>
/// 构造方法
/// </summary>
public MyHelloHost()
{
CreateHelloServiceHost();
}
/// <summary>
/// 关闭服务后,销毁服务宿主实例对象
/// </summary>
public void Dispose()
{
if (_myhost!=null)
{
(_myhost as IDisposable).Dispose();
}
}
}
}</span>
Client:
<span style="font-family:SimSun;font-size:18px;">namespace HelloClient
{
class Program
{
static void Main(string[] args)
{
using (HelloProxy proxy = new HelloProxy())
{
//利用代理调用服务
Console.WriteLine(proxy.Say("良辰"));
Console.Read();
}
}
//硬编码定义服务契约
[ServiceContract]
interface IService
{
//服务操作
[OperationContract]
String Say(String name);
}
/// <summary>
/// 客户端代理类;ClientBase 是用于创建可调用服务的客户端对象,是一种泛型,里面要包括服务端的接口;IService 接口要实现客户端创建的硬编码定义服务企业的接口。
/// </summary>
class HelloProxy : ClientBase<IHelloService>,IService
{
/// <summary>
/// 硬编码定义绑定
/// </summary>
public static readonly Binding HelloBinding = new NetNamedPipeBinding();
//硬编码定义地址
public static readonly EndpointAddress HelloAddress= new EndpointAddress(new Uri("net.pipe://localhost/Hello"));
/// <summary>
/// 构造方法
/// </summary>
public HelloProxy():base(HelloBinding,HelloAddress){}
//调用服务端方法
public String Say(String name)
{
//使用Channel属性对服务进行调用,获取
return Channel.SayHello(name);
}
}
}
}
</span>
效果:
服务运行:
Client效果
KEY WORD:
1.托管:通过服务契约的绑定,拿到Service端的服务。
<span style="font-family:SimSun;font-size:18px;"> //服务契约接口;和服务端的服务绑定
public static readonly Type ContractType = typeof(HelloService.IHelloService);
</span>
2.托管:创建服务并为服务添加元数据(终结点信息)。
<span style="font-family:SimSun;font-size:18px;">protected void CreateHelloServiceHost()
{
//创建服务对象
_myhost = new ServiceHost(ServiceType, new Uri[] { new Uri(BaseAddress) });
//添加终结点
_myhost.AddServiceEndpoint(ContractType,hellobingding,HelloServiceAddress );
}
</span>
3.Client:通过channels属性拿到服务。
<span style="font-family:SimSun;font-size:18px;"> public String Say(String name)
{
//使用Chaanel属性对服务进行调用,获取
return Channel.SayHello(name);
}
</span>
4.Client:通过代理服务调用。
<span style="font-family:SimSun;font-size:18px;"> static void Main(string[] args)
{
using (HelloProxy proxy = new HelloProxy())
{
//利用代理调用服务
Console.WriteLine(proxy.Say("良辰"));
Console.Read();
}
}
</span>
总结:
在高校云平台的项目中就接触到了WCF,对其中使用的分布式服务以及用到的工厂、代理很是向往,但是自己一
直都没有去搭过ITOO的框架,这次在demo中看到了很多ITOO的框架身影,再去接触ITOO相信感触会更多。下一步尝试
去搭建高校的框架。