1、WCF 简单介绍 |
Windows Communication Foundation(WCF)是由微软发展的一组数据通讯的应用程序开发接口,是一套通信接口。如今比较流行的SOA就能够经过WCF实现。他的功能若是非要用一个词来形容恐怕只能用“强大”,“完美”来形容。融合了remoting和webservices的强大功能,还推出了WCF配置的小工具,更加方便入手和进阶。 |
WCF 体系结构 |
2、今天想说的 |
一、Wcf常常须要添加新功能,须要发布新功能与修改配置文件 |
二、须要配置的信息颇有特色(基本就是service,endpoint) |
三、配置多了,真的很烦,并且一不当心要是写错了就,糟糕了 |
进入主题啦,咱们要说的是WCF自承载 |
3、WCF自承载 |
一、简单实现
1 ServiceHost host = new ServiceHost(typeof(Service1)); 2 WSHttpBinding nb = new WSHttpBinding(SecurityMode.None); 3 host.AddServiceEndpoint(iType, nb, "http://127.0.0.1:8888/IService1"); 4 host.Open(); 这样就能够简单实现自承载了,现实中除了Http咱们还常常用到Http程序员 |
1 //添加支持TCP协议服务 2 private static void AddTcpServer(Type type, Type iType) 3 { 4 ServiceHost host = new ServiceHost(type); 5 NetTcpBinding nb = new NetTcpBinding(SecurityMode.None); 6 host.AddServiceEndpoint(iType, nb, string.Concat("net.tcp://", Ip, ":", TcpPort, "/", iType.Name)); 7 OpenServer(host, type.Name + "Tcp服务"); 8 } 9 //添加支持HTTP协议服务 10 private static void AddHttpServer(Type type, Type iType) 11 { 12 ServiceHost host = new ServiceHost(type); 13 WSHttpBinding nb = new WSHttpBinding(SecurityMode.None); 14 host.AddServiceEndpoint(iType, nb, string.Concat("http://", Ip, ":", HttpPort, "/", iType.Name)); 15 OpenServer(host, type.Name + "Http服务"); 16 } |
二、日常用较经常使用的有能够经过防火墙的HTTP协议,和效率很高的TCP |
1 private static void AddServer(Type type, Type iType) 2 { 3 ServiceHost host = new ServiceHost(type); 4 NetTcpBinding nb = new NetTcpBinding(SecurityMode.None); 5 host.AddServiceEndpoint(iType, nb, string.Concat("net.tcp://", Ip, ":", TcpPort, "/", iType.Name)); 6 host.Opening += delegate { Console.WriteLine(DateTime.Now.ToString() + " Http服务启动中" + "\r\n"); }; 7 host.Opening += delegate { Console.WriteLine(DateTime.Now.ToString() + " Http服务已经启动" + "\r\n"); }; 8 9 host.Open(); 10 ServiceHost host1 = new ServiceHost(type); 11 WSHttpBinding nb1 = new WSHttpBinding(SecurityMode.None); 12 host1.AddServiceEndpoint(iType, nb1, string.Concat("http://", Ip, ":", HttpPort, "/", iType.Name)); 13 host1.Opening += delegate { Console.WriteLine(DateTime.Now.ToString() + " Http服务启动中" + "\r\n"); }; 14 host1.Opening += delegate { Console.WriteLine(DateTime.Now.ToString() + " Http服务已经启动" + "\r\n"); }; 15 host1.Open(); 16 } 这样已经强大了,传入服务和协定的Type,就能够实现服务在Http和Tcp上的承载了。当有一天咱们须要msmq,当有一天咱们想把服务运行状况的记录到数据库中,当...。一般需求是多变的,程序员要多给本身留点后路web |
三、后路 |
1 private static void AddServer(Type type, Type iType, bool useTcpHost, bool useHttpHost) 2 { 3 if (useTcpHost) 4 AddTcpServer(type, iType); 5 6 if (useHttpHost) 7 AddHttpServer(type, iType); 8 } 9 //添加支持TCP协议服务 10 private static void AddTcpServer(Type type, Type iType) 11 { 12 ServiceHost host = new ServiceHost(type); 13 NetTcpBinding nb = new NetTcpBinding(SecurityMode.None); 14 host.AddServiceEndpoint(iType, nb, string.Concat("net.tcp://", Ip, ":", TcpPort, "/", iType.Name)); 15 OpenServer(host, type.Name + "Tcp服务"); 16 } 17 //添加支持TCP协议服务 18 private static void AddHttpServer(Type type, Type iType) 19 { 20 ServiceHost host = new ServiceHost(type); 21 WSHttpBinding nb = new WSHttpBinding(SecurityMode.None); 22 host.AddServiceEndpoint(iType, nb, string.Concat("http://", Ip, ":", HttpPort, "/", iType.Name)); 23 OpenServer(host, type.Name + "Http服务"); 24 } 25 26 private static void OpenServer(ServiceHost host, string name) 27 { 28 try 29 { 30 host.Opening += delegate { ShowMessage(name + "启动中"); }; 31 host.Opened += delegate { ShowMessage(name + "已经启动"); }; 32 33 host.Open(); 34 } 35 catch (Exception ex) 36 { 37 ShowMessage(host.ToString() + ex.Message); 38 } 39 } 40 41 private static void ShowMessage(string outMessage) 42 { 43 Console.WriteLine(DateTime.Now.ToString() + " " + outMessage + "\r\n"); 44 } 第一眼相同的功能代码多多了。是否是很麻烦呢?要是你以为是,那我认可我太失败了,这样以前说的问题就不用担忧了,很简单就解决了。当时新的服务老是有,老是要我改代码,从新发布也麻烦啊。下面说不改代码的办法数据库 |
四、不改代码的办法 |
1 static void Main(string[] args) 2 { 3 Setup(); 4 Console.ReadLine(); 5 } 6 7 private static void Setup() 8 { 9 string docPath = path + "\\Assembly.xml"; 10 if (File.Exists(docPath)) 11 { 12 XDocument xdoc = XDocument.Load(docPath); 13 foreach (XElement element in xdoc.Root.Element("BLLFiles").Elements()) 14 { 15 LoadType(element.Attribute("FileName").Value); 16 } 17 } 18 } 19 20 private static void LoadType(string bllPath) 21 { 22 if (!File.Exists(path + "\\" + bllPath + ".dll")) 23 return; 24 25 Assembly bllAssembly = Assembly.LoadFile(path + "\\" + bllPath + ".dll"); 26 27 Type[] types = bllAssembly.GetTypes(); 28 29 foreach (Type type in types) 30 { 31 Type t1 = bllAssembly.GetType(bllPath + ".I" + type.Name); 32 if (type == null || t1 == null) 33 continue; 34 AddServer(type, t1, true, true); 35 } 36 } 1 Assembly.xml文件 2 <?xml version="1.0" encoding="UTF-8"?> 3 <AssemblyInfos> 4 <BLLFiles> 5 <BLLFile FileName="WcfService"/> 6 </BLLFiles> 7 </AssemblyInfos> 认可你丫的事情多,我用一个X妹儿没你要加载的Dll文件记录下。tcp 而后反射去你全部的类。而后自动加载全部有协定的类。之后有新的服务在其余dll中,我加行BLLFil,哈哈哈,在此笑过。。。。工具 |
我用这样的方式实现了,一套本身的管理系统,下载 用户名1 密码 111 |
本实例源码 |
感谢你们拍砖,偶也是摸着石头过河 |