zoukankan      html  css  js  c++  java
  • 无配置wcf Host

    internal class Program
        {
            private static bool IsInterFaceDll(FileInfo file)
            {
                Assembly ass = Assembly.LoadFile(file.FullName);
                Type[] types = ass.GetTypes();
                Type sContractType = typeof(ServiceContractAttribute);
                foreach (Type type in types)
                {
                    if (type.IsInterface)
                    {
                        Object[] aa = type.GetCustomAttributes(false);
                        foreach (Object a in aa)
                        {
                            if (a.ToString() == sContractType.ToString())
                            {
                                return true;
                            }
                        }
                    }
                }
                return false;
            }
    
            private static List<Type> GetInterFaceTypes(FileInfo file)
            {
                List<Type> result = new List<Type>();
    
                Assembly ass = Assembly.LoadFile(file.FullName);
                Type[] types = ass.GetTypes();
                Type sContractType = typeof(ServiceContractAttribute);
    
                foreach (Type type in types)
                {
                    if (type.IsInterface)
                    {
                        Object[] aa = type.GetCustomAttributes(false);
                        foreach (Object a in aa)
                        {
                            if (a.ToString() == sContractType.ToString())
                            {
                                result.Add(type);
                            }
                        }
                    }
                }
                return result;
            }
    
            private static void Main(string[] args)
            {
                try
                {
                    DirectoryInfo directory = new DirectoryInfo(Environment.CurrentDirectory);
                    FileInfo[] files = directory.GetFiles();
    
                    List<FileInfo> list = new List<FileInfo>();
    
                    foreach (FileInfo file in files)
                    {
                        if (file.Extension.ToLower() == ".dll")
                        {
                            if (IsInterFaceDll(file))
                            {
                                list.Add(file);
                            }
                        }
                    }
    
                   string IP= ConfigurationManager.AppSettings["IP"];
                   Uri baseHttpAddress = new Uri(string.Format("http://{0}/Bll/", IP));
                   Uri baseTcpAddress = new Uri(string.Format("net.tcp://{0}/Bll/", IP));
    
                    string impAssemblyName = string.Empty;
                    string strClassType = string.Empty;
    
                    Type classType = null;
                   
    
                    ServiceHost host = null;
                    Binding wsBinding = null;
                    Binding tcpBinding = null;
                    List<Type> interFaceList=new List<Type>();
                    ServiceMetadataBehavior metadataBehavior = null;
                    for (int i = 0; i < list.Count; i++)
                    {
                        impAssemblyName = list[i].ToString().Replace(".Contract", "");
                        interFaceList = GetInterFaceTypes(list[i]);
                        foreach (Type myType in interFaceList)
                        {
                            strClassType = myType.ToString().Replace(".Contract.I", ".");
    
                             classType =Assembly.LoadFrom(impAssemblyName).GetType(strClassType);
    
                            host = new ServiceHost(classType, new Uri[] { baseHttpAddress, baseTcpAddress });
                            wsBinding = new WSHttpBinding();
                            tcpBinding = new NetTcpBinding();
                          
                            host.AddServiceEndpoint(myType, wsBinding, "");
                            host.AddServiceEndpoint(myType, tcpBinding, "tcp");
    
    
                            metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
    
                            if (metadataBehavior == null)
                            {
                                metadataBehavior = new ServiceMetadataBehavior();
                                metadataBehavior.HttpGetEnabled = true;
                                host.Description.Behaviors.Add(metadataBehavior);
                            }
    
                            host.Open();
                            Console.WriteLine("契约接口:{0};", myType);
                            Console.WriteLine("  实现类:{0}", classType);
                            Console.WriteLine("Host start...");
                            Console.WriteLine();
    
                        }
                    }
                    Console.ReadLine();
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                    Console.ReadLine();
                }
            }
    
        }
    

      

  • 相关阅读:
    分化Oracle数据库日记文件(1)
    ORACLE稀有错误代码的阐发与经管(二)
    Oracle暗码文件的运用和维护
    在ORACLE中移动数据库文件
    ORACLE8的分区管理
    Oracle中如何间接运转OS号令(上)
    Oracle数据库平安计谋阐明 (三)
    Oracle7.X 回滚表空间数据文件误删除措置举动措施
    Oracle功用究极优化 中
    网络知识爆炸的年代~如何更好地学习吸收有用的知识
  • 原文地址:https://www.cnblogs.com/stanley107/p/2823105.html
Copyright © 2011-2022 走看看