WCF 自承載 提供源碼

1、WCF 簡單介紹
Windows Communication Foundation(WCF)是由微軟發展的一組數據通訊的應用程序開發接口,是一套通信接口。如今比較流行的SOA就能夠經過WCF實現。他的功能若是非要用一個詞來形容恐怕只能用「強大」,「完美」來形容。融合了remoting和webservices的強大功能,還推出了WCF配置的小工具,更加方便入手和進階。

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
 本實例源碼
 感謝你們拍磚,偶也是摸着石頭過河
相關文章
相關標籤/搜索