/// <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结构