zoukankan      html  css  js  c++  java
  • WCF 自承载

    WCF 自承载 提供源码

    一、WCF 简单介绍
    Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,是一套通讯接口。现在比较流行的SOA就可以通过WCF实现。他的功能如果非要用一个词来形容恐怕只能用“强大”,“完美”来形容。融合了remoting和webservices的强大功能,还推出了WCF配置的小工具,更加方便入手和进阶。

    WCF 体系结构

    WCF 体系结构

    二、今天想说的
    1、Wcf经常需要添加新功能,需要发布新功能与修改配置文件
    2、需要配置的信息很有特点(基本就是service,endpoint)
    3、配置多了,真的很烦,而且一不小心要是写错了就,糟糕了
    进入主题啦,我们要说的是WCF自承载
    三、WCF自承载
    1、简单实现
    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         }
    复制代码
    2、平常用较常用的有可以通过防火墙的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服务启动中" + "
    "); };
     7             host.Opening += delegate { Console.WriteLine(DateTime.Now.ToString() + "  Http服务已经启动" + "
    "); };
     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服务启动中" + "
    "); };
    14             host1.Opening += delegate { Console.WriteLine(DateTime.Now.ToString() + "  Http服务已经启动" + "
    "); };
    15             host1.Open();
    16         }
    复制代码

    这样已经强大了,传入服务和协定的Type,就可以实现服务在Http和Tcp上的承载了。当有一天我们需要msmq,当有一天我们想把服务运行情况的记录到数据库中,当...。通常需求是多变的,程序员要多给自己留点后路

    3、后路
    复制代码
     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 + "
    ");
    44         }
    复制代码

    第一眼相同的功能代码多多了。是不是很麻烦呢?要是你觉得是,那我承认我太失败了,这样之前说的问题就不用担心了,很简单就解决了。当时新的服务总是有,总是要我改代码,重新发布也麻烦啊。下面说不改代码的办法

    4、不改代码的办法
    复制代码
     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文件记录下。

    然后反射去你所有的类。然后自动加载所有有协定的类。以后有新的服务在其他dll中,我加行BLLFil,哈哈哈,在此笑过。。。。

     我用这样的方式实现了,一套自己的管理系统,下载 用户名1 密码 111
     本实例源码
     感谢大家拍砖,偶也是摸着石头过河
     
     
    分类: 框架设计模式
    标签: c#WCF自承载TCPHTTP
  • 相关阅读:
    csp-s模拟110
    csp-s模拟109
    留念
    csp-s 2019 游记
    HEOI2020
    CSP-S2019记
    堆积的$TIPS$
    低错复习
    倍增并查集
    4.26
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3477682.html
Copyright © 2011-2022 走看看