代码改变世界

01 WCF初识:WCF组成结构

2017-03-01 01:37  张三李四隔壁老王  阅读(818)  评论(1编辑  收藏  举报

在项目中总用到WCF,但是未曾去详细而深入得去了解。现重新阅读WCF全面解析上下册。谢谢博文以记心得。

望坚持。

使用WCF需要引用 System.ServiceModel.dll,而.NetFramwork版本需要3.0以上。

WCF组成结构:服务契约(Service Contract)、服务实体(Services)、宿主(Hosting)、客户端(Client)。

Contract:服务契约:定义WCF服务端与客户端之间的协定,通常以接口的形式实现,并使用[ServiceContract]进行服务协定定义,使用[OperationContract]进行服务公开协定;ServiceContract中还可以进行CallbackContractNameNamespace等特性赋值。OperationContract中还可以进行ActionIsOneWayName等特性赋值。

 1  [ServiceContract]
 2     public interface IDataProvider
 3     {
 4         /// <summary>
 5         /// 保存数据接口
 6         /// </summary>
 7         /// <param name="type">数据类型描述</param>
 8         /// <param name="data">数据的json格式</param>
 9         /// <returns>DataResult对象</returns>
10         [OperationContract]
11         string SaveData(string type, string data);
12     }

 

Services:继承Service Contract,并实现服务契约中所定义的方法。可以使用 ServiceBehavior(ServiceBehaviorAttribute)来指定服务协定实现的内部执行行为。

可进行特性赋值如:AddressFilterModeIncludeExceptionDetailInFaultsNameNamespace

1 [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
2     public partial class DataProvider : IDataProvider
3     {
4         public string SaveData(string type, string data)
5         {
6               //XXXX
7         }
8 
9     }

 

Hosting:宿主:WCF的运行是依赖于宿主的,是WCF服务运行的寄宿体,在System.ServiceModel.dll 中有System.ServiceModel.ServiceHost中可以实现该宿主功能。宿主主要有三部分构成:地址(Address)、绑定(Binding)、契约(Contract)。

    • 地址(Address):地址决定了服务的位置,解决了服务寻址的问题,《WCF技术剖析(卷1)》第2章提供了对地址和寻址机制的详细介绍;
    • 绑定(Binding):绑定实现了通信的所有细节,包括网络传输、消息编码,以及其他为实现某种功能(比如安全、可靠传输、事务等)对消息进行的相应处理。WCF中具有一系列的系统定义绑定,比如BasicHttpBinding、WsHttpBinding、NetTcpBinding等,《WCF技术剖析(卷1)》第3章提供对绑定的详细介绍;
    • 契约(Contract):契约是对服务操作的抽象,也是对消息交换模式以及消息结构的定义。《WCF技术剖析(卷1)》第4章提供对服务契约的详细介绍。
 1             //WCF 使用Http实现WCF宿主
 2             Uri _uris = new Uri("http://127.0.0.1:3800");//WCF的地址
 3             ServiceHost _host = new ServiceHost(typeof(DataProvider), _uris);//初始化Host
 4             ContractDescription_contractDesc = ContractDescription.GetContract(typeof(IDataProvider));
 5             BasicHttpBinding httpBinding = new BasicHttpBinding();
 6 
 7             httpBinding.Name = "_HTTP_BINDING";
 8             httpBinding.MaxReceivedMessageSize = int.MaxValue;
 9             httpBinding.MaxBufferSize = int.MaxValue;
10             httpBinding.ReaderQuotas.MaxArrayLength = int.MaxValue;
11             httpBinding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
12             httpBinding.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
13             httpBinding.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
14             httpBinding.SendTimeout = new TimeSpan(10, 10, 10);
15             httpBinding.CloseTimeout = new TimeSpan(10, 10, 10);
16             httpBinding.ReceiveTimeout = new TimeSpan(10, 10, 10);
17             httpBinding.OpenTimeout = new TimeSpan(10, 10, 10);
18             httpBinding.Security.Mode = BasicHttpSecurityMode.None;
19             ServiceEndpoint _endpoint_http = new ServiceEndpoint(_contractDesc,
20                                             httpBinding, new EndpointAddress(url));
21             _host.Description.Endpoints.Add(_endpoint_http);
22 
23             ServiceMetadataBehavior _behavior = new ServiceMetadataBehavior();
24             _behavior.HttpGetEnabled = true;
25             _host.Description.Behaviors.Add(_behavior);
26             _host.Open();//可使用host.Opened += delegate{LogHelper.Error("")}进行日志输出或其他操作。

Client:客户端:当WCF服务成功寄宿到宿主后,可以便可通过代码生成工具(SvcUtil.exe)进行代理类生成,我们便可以通过这个代理类来实现对WCF服务的调用。

通常情况调用契约的方法有两种:Client和Channel。(详细使用方法会在后续的文章中给出)

 1 //Client 访问方式
 2                 BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
 3                 binding.MaxReceivedMessageSize = 5000000;
 4                 EndpointAddress address = new EndpointAddress(_hostUrl);
 5                 DataProviderClient client = new DataProviderClient(binding, address);//生成Client后即可使用定义的方法
 6                 //Channel访问方式
 7                 Uri _uris = new Uri(url);
 8                 EndpointAddress endpoint = new EndpointAddress(uri);
 9                 ContractDescription _contractDesc = ContractDescription.GetContract(Contract);
10                 ChannelFactory<T> channelFactory = new ChannelFactory<T>(endpoint);
11                 T instance = channelFactory.CreateChannel();

 

 暂时写到这里,各位大神,有任何问题 欢迎留言批评,指点。