zoukankan      html  css  js  c++  java
  • 动态开启WCF服务

         /// <summary>
            /// 启动服务
            /// </summary>
            /// <param name="row">服务行</param>
            public void RunService(DSService.ServiceDLLRow row)
            {
                try
                {
                    //获取类型
                    Assembly ass = Assembly.LoadFrom(row.DLLPath);
                    Type serviceType = ass.GetType(row.DLLRealize);
                    if (serviceType == null)
                        throw new Exception(string.Format("无法反射:{0}的Tyte!", row.DLLRealize));
    
                    
                    bool isCallBackInterface =false;
                    Type[] interfaceArr = serviceType.GetInterfaces();
                    foreach (Type t in interfaceArr)
                    {
                        object[] arr = t.GetCustomAttributes(true);
                        if(arr ==null || arr.Length==0)
                            continue;
                        foreach (object attr in arr)
                        {
                            if (attr is System.ServiceModel.ServiceContractAttribute)
                            {
                                if (((System.ServiceModel.ServiceContractAttribute)(attr)).CallbackContract != null)
                                {
                                    isCallBackInterface = true;
                                    break;
                                }
                            }
                        }
                    }
    
                    //创建服务主机
                    Uri baseAddress = new Uri(row.AddressFull);
                    ServiceHost sh = new ServiceHost(serviceType, baseAddress);
                    //创建服务绑定
                    Binding binding = ServiceDLLBiz.GetServiceBinding(row.NetTransport, isCallBackInterface);
                    if (binding == null)
                        throw new Exception("无法支持的绑定!");
    
    
                    //创建Endpoint
                    //ServiceEndpoint endpoint = sh.AddServiceEndpoint(row.DLLContact, binding, row.DLLRealize);//interface
                    ServiceEndpoint endpoint = sh.AddServiceEndpoint(row.DLLContact, binding, string.Empty);//interface
    
    
                    if (binding is System.ServiceModel.WebHttpBinding)
                    {
                        WebHttpBehavior httpBehavior = new WebHttpBehavior();
                        
                        endpoint.Behaviors.Add(httpBehavior);
                    }
    
                    foreach (var operation in endpoint.Contract.Operations)
                    {
                        var dataContractBehavior = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
                        if (dataContractBehavior != null)
                        {
                            dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
                        }
                    }
    
                    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                    smb.HttpGetEnabled = true;
                    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                    string mex = row.AddressFull.Replace(dicNetTransport[row.NetTransport], "http://") + "/" + row.NetTransport;
                    if (binding is NetTcpBinding)
                    {
                        mex = mex.Replace(row.Port, (Int32.Parse(row.Port) + 1).ToString());
                        if (isCallBackInterface && binding is NetTcpBinding)
                            (binding as NetTcpBinding).TransferMode = TransferMode.Buffered;
                    }
                    smb.HttpGetUrl = new Uri(mex);
                    sh.Description.Behaviors.Add(smb);
    
                    
                    sh.Open();
    
                    //添加进服务Dic
                    row.IsOpen = true;
                    row.AddressMex = mex;
                    row.AddressFull = endpoint.Address.Uri.ToString();
                    _dicHost.Add(row, sh);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    

      2.DSService.ServiceDLLRow结构

  • 相关阅读:
    GJM: Unity3D AssetBundle 手记 [转载]
    GJM: Unity3D基于Socket通讯例子 [转载]
    GJM:用C#实现网络爬虫(二) [转载]
    JSONP(跨域请求) —— 一种非官方跨域数据交互协议
    经典布局之圣杯布局 —— 左右定宽,中间流式
    js中的callback(阻塞同步或异步时使用)
    Emmet:HTML/CSS代码快速编写神器
    CSS弹性盒模型 box-flex
    JSON对象的stringify()和parse()方法
    懒加载 lazy load
  • 原文地址:https://www.cnblogs.com/seacher/p/8565796.html
Copyright © 2011-2022 走看看