/// <summary> /// 初始化WCF元数据 /// </summary> /// <param name="config"></param> private static WCFServiceMeta InitializeWCFServiceMeta(string contextName, System.Configuration.Configuration config) { WCFServiceMeta wm = new WCFServiceMeta(); ServicesSection sconfig = config.GetSection(GlobalParams.ServiceHostSession) as ServicesSection; if (sconfig != null) { if (sconfig.Services.Count > 0) { wm.IsExistService = true; wm.ServicesConfiguration = sconfig; wm.ContextName = contextName.ToLower(); wm.ChildConfiguration = config; } else { wm.IsExistService = false; } } BehaviorsSection bconfig = config.GetSection(GlobalParams.BehaviorSession) as BehaviorsSection; if (bconfig != null) { wm.BehaviorsConfiguration = bconfig; } return(wm); }
public static void BuildingServiceaBehavior(WCFServiceMeta wCFServiceMeta, ServiceElement serviceElement, SpringServiceHost ssh) { ServiceBehaviorElement sbe = null; if (serviceElement.BehaviorConfiguration != "" && wCFServiceMeta.BehaviorsConfiguration != null) { if (wCFServiceMeta.BehaviorsConfiguration.ServiceBehaviors.ContainsKey(serviceElement.BehaviorConfiguration)) { ServiceBehaviorElementCollection sbec = wCFServiceMeta.BehaviorsConfiguration.ServiceBehaviors; foreach (ServiceBehaviorElement o in sbec) { if (o.Name == serviceElement.BehaviorConfiguration) { sbe = o; break; } } if (sbe != null) { //ServiceBehavior smb = new ServiceMetadataBehavior(); foreach (var metadata in sbe) { switch (metadata.GetType().FullName) { case "System.ServiceModel.Configuration.DataContractSerializerElement": { DataContractSerializerElement dse = metadata as DataContractSerializerElement; if (dse != null) { int i = dse.MaxItemsInObjectGraph; ContractDescription cd = ssh.Description.Endpoints.FirstOrDefault(o => o.Name != "IMetadataExchange").Contract; OperationDescriptionCollection opdc = cd.Operations; foreach (OperationDescription odp in opdc) { DataContractSerializerOperationBehavior dsb = new DataContractSerializerOperationBehavior(odp); dsb.IgnoreExtensionDataObject = dse.IgnoreExtensionDataObject; dsb.MaxItemsInObjectGraph = dse.MaxItemsInObjectGraph; odp.Behaviors.Remove <DataContractSerializerOperationBehavior>(); odp.Behaviors.Add(dsb); } return; } break; } } } } } } }
/// <summary> /// 按指定的容器环境名以及配置创建WCF服务,并添加到容器 /// <code> /// WCFServiceContainer.Create(contextName, CurrentAddinConfiguration);//初始化WCF服务 /// </code> /// </summary> /// <param name="contextName"></param> /// <param name="configuration"></param> public static void Create(string contextName, System.Configuration.Configuration configuration) { WCFServiceMeta wm = InitializeWCFServiceMeta(contextName, configuration); if (wm.IsExistService) { lock (_container) { WCFService wcfservice = new WCFService(); _container.Add(wm, wcfservice.Builder(wm)); } } }
/// <summary> /// 根据WCF元数据创建SpringServiceHost列表 /// <code> /// WCFService wcfservice = new WCFService(); /// _container.Add(wm, wcfservice.Builder(wm)); /// </code> /// </summary> /// <param name="serviceMeta">WCF元数据</param> /// <returns>SpringServiceHost列表</returns> public List <SpringServiceHost> Builder(WCFServiceMeta serviceMeta) { WCFServiceMeta = serviceMeta; List <SpringServiceHost> ssh = new List <SpringServiceHost>(); ServiceEndpointElement metaServiceEndpoint = null; try { foreach (ServiceElement service in serviceMeta.ServicesConfiguration.Services) { List <Uri> baseUris = new List <Uri>(); foreach (BaseAddressElement ba in service.Host.BaseAddresses) { baseUris.Add(new Uri(ba.BaseAddress)); } //ServiceHost sh = new System.ServiceModel.ServiceHost(CreateContactType(service.Name), baseUris.ToArray()); //IApplicationContext c = ContextRegistry.GetContext(serviceMeta.ContextName); SpringWebServiceHost sh2 = null; SpringServiceHost sh = null;// new SpringServiceHost(service.Name, serviceMeta.ContextName, baseUris.ToArray()); // SpringWebServiceHost sh = new WebServiceHost() foreach (ServiceEndpointElement see in service.Endpoints) { Type contactType; if (see.Contract == "IMetadataExchange") { //contactType = typeof(IMetadataExchange); metaServiceEndpoint = see; continue; } else { contactType = CreateContactType(see.Contract); } //ContractDescription cd = ContractDescription.GetContract(contactType); Binding binding = WCFMateHelper.BindingFactory(serviceMeta, see); if (binding is WebHttpBinding) { sh2 = new SpringWebServiceHost(service.Name, serviceMeta.ContextName, baseUris.ToArray()); sh2.AddServiceEndpoint(contactType, binding, see.Address); } else { try { sh = new SpringServiceHost(service.Name, serviceMeta.ContextName, baseUris.ToArray()); sh.AddServiceEndpoint(contactType, binding, see.Address); }catch (Exception ex) { throw new Exception(string.Format("创建服务失败,WCF配置中的服务名称{0},不能在容器中获取实例", service.Name), ex.InnerException); } } } try { if (sh2 == null) { ServiceDebugBehavior sdb = sh.Description.Behaviors.Find <ServiceDebugBehavior>(); { if (sdb != null) { sdb.IncludeExceptionDetailInFaults = true; } else { ServiceDebugBehavior sb = new ServiceDebugBehavior(); sb.IncludeExceptionDetailInFaults = true; sh.Description.Behaviors.Add(sb); } } ServiceMetadataBehavior behavior = sh.Description.Behaviors.Find <ServiceMetadataBehavior>(); { CreateMetadataBehavior(sh, behavior, metaServiceEndpoint); } WCFMateHelper.BuildingServiceaBehavior(serviceMeta, service, sh); sh.Faulted += sh_Faulted; sh.UnknownMessageReceived += sh_UnknownMessageReceived; if (sh.State != CommunicationState.Opened) { sh.Open(); } ssh.Add(sh); } else { ServiceDebugBehavior sdb = sh2.Description.Behaviors.Find <ServiceDebugBehavior>(); { if (sdb != null) { sdb.IncludeExceptionDetailInFaults = true; } else { ServiceDebugBehavior sb = new ServiceDebugBehavior(); sb.IncludeExceptionDetailInFaults = true; sh2.Description.Behaviors.Add(sb); } } sh2.Faulted += sh_Faulted; sh2.UnknownMessageReceived += sh_UnknownMessageReceived; if (sh2.State != CommunicationState.Opened) { sh2.Open(); } } } catch (Exception ex) { throw new WCFServiceCreateException(Resources.WCFServiceCreateException, ex); } } } catch (Exception serException) { throw serException; } return(ssh); }
/// <summary> /// 生成Binding对象 /// <code> /// WCFMateHelper.BindingFactory(serviceMeta,see) /// </code> /// </summary> /// <param name="wCFServiceMeta"></param> /// <param name="serviceEndpoint"></param> /// <returns></returns> public static Binding BindingFactory(WCFServiceMeta wCFServiceMeta, ServiceEndpointElement serviceEndpoint) { BindingsSection bindings = wCFServiceMeta.ChildConfiguration.GetSection("system.serviceModel/bindings") as BindingsSection; BindingCollectionElement bc = bindings[serviceEndpoint.Binding]; switch (bc.BindingName.ToLower()) { case "nettcpbinding": { NetTcpBinding ntb = new NetTcpBinding(); NetTcpBindingElement bce = bc.ConfiguredBindings.FirstOrDefault(o => o.Name == serviceEndpoint.BindingConfiguration) as NetTcpBindingElement; if (bce != null) { ntb.CloseTimeout = bce.CloseTimeout; ntb.OpenTimeout = bce.OpenTimeout; ntb.ReceiveTimeout = bce.ReceiveTimeout; ntb.SendTimeout = bce.SendTimeout; ntb.MaxBufferPoolSize = bce.MaxBufferPoolSize; ntb.HostNameComparisonMode = bce.HostNameComparisonMode; ntb.ListenBacklog = (bce.ListenBacklog != 0 ? bce.ListenBacklog : ntb.ListenBacklog); ntb.MaxBufferSize = bce.MaxBufferSize; ntb.MaxConnections = (bce.MaxConnections == 0 ? ntb.MaxConnections : bce.MaxConnections); ntb.MaxReceivedMessageSize = bce.MaxReceivedMessageSize; ntb.PortSharingEnabled = bce.PortSharingEnabled; ntb.ReaderQuotas.MaxArrayLength = (bce.ReaderQuotas.MaxArrayLength != 0 ? bce.ReaderQuotas.MaxArrayLength : ntb.ReaderQuotas.MaxArrayLength); ntb.ReaderQuotas.MaxDepth = (bce.ReaderQuotas.MaxDepth != 0 ? bce.ReaderQuotas.MaxDepth : ntb.ReaderQuotas.MaxDepth); ntb.ReaderQuotas.MaxBytesPerRead = (bce.ReaderQuotas.MaxBytesPerRead != 0 ? bce.ReaderQuotas.MaxBytesPerRead : ntb.ReaderQuotas.MaxBytesPerRead); ntb.ReaderQuotas.MaxNameTableCharCount = (bce.ReaderQuotas.MaxNameTableCharCount != 0 ? bce.ReaderQuotas.MaxNameTableCharCount : ntb.ReaderQuotas.MaxNameTableCharCount); ntb.ReaderQuotas.MaxStringContentLength = (bce.ReaderQuotas.MaxStringContentLength != 0 ? bce.ReaderQuotas.MaxStringContentLength : ntb.ReaderQuotas.MaxStringContentLength); ntb.ReliableSession = new OptionalReliableSession() { Enabled = bce.ReliableSession.Enabled, InactivityTimeout = bce.ReliableSession.InactivityTimeout, Ordered = bce.ReliableSession.Ordered }; ntb.Security = new NetTcpSecurity() { Mode = SecurityMode.None }; ntb.TransactionFlow = bce.TransactionFlow; ntb.TransactionProtocol = bce.TransactionProtocol; ntb.TransferMode = bce.TransferMode; } return(ntb); } case "basichttpbinding": { BasicHttpBinding bhb = new BasicHttpBinding(BasicHttpSecurityMode.None); BasicHttpBindingElement bce = bc.ConfiguredBindings.FirstOrDefault(o => o.Name == serviceEndpoint.BindingConfiguration) as BasicHttpBindingElement; if (bce != null) { bhb.AllowCookies = bce.AllowCookies; bhb.BypassProxyOnLocal = bce.BypassProxyOnLocal; bhb.CloseTimeout = bce.CloseTimeout; bhb.HostNameComparisonMode = bce.HostNameComparisonMode; bhb.MaxBufferPoolSize = bce.MaxBufferPoolSize; bhb.MaxBufferSize = bce.MaxBufferSize; bhb.MaxReceivedMessageSize = bce.MaxReceivedMessageSize; bhb.MessageEncoding = bce.MessageEncoding; bhb.OpenTimeout = bce.OpenTimeout; bhb.ProxyAddress = bce.ProxyAddress; bhb.ReaderQuotas.MaxArrayLength = (bce.ReaderQuotas.MaxArrayLength != 0 ? bce.ReaderQuotas.MaxArrayLength : bhb.ReaderQuotas.MaxArrayLength); bhb.ReaderQuotas.MaxDepth = (bce.ReaderQuotas.MaxDepth != 0 ? bce.ReaderQuotas.MaxDepth : bhb.ReaderQuotas.MaxDepth); bhb.ReaderQuotas.MaxBytesPerRead = (bce.ReaderQuotas.MaxBytesPerRead != 0 ? bce.ReaderQuotas.MaxBytesPerRead : bhb.ReaderQuotas.MaxBytesPerRead); bhb.ReaderQuotas.MaxNameTableCharCount = (bce.ReaderQuotas.MaxNameTableCharCount != 0 ? bce.ReaderQuotas.MaxNameTableCharCount : bhb.ReaderQuotas.MaxNameTableCharCount); bhb.ReaderQuotas.MaxStringContentLength = (bce.ReaderQuotas.MaxStringContentLength != 0 ? bce.ReaderQuotas.MaxStringContentLength : bhb.ReaderQuotas.MaxStringContentLength); bhb.ReceiveTimeout = bce.ReceiveTimeout; bhb.SendTimeout = bce.SendTimeout; bhb.TextEncoding = bce.TextEncoding; bhb.TransferMode = bce.TransferMode; bhb.UseDefaultWebProxy = bce.UseDefaultWebProxy; } return(bhb); } case "webhttpbinding": { WebHttpBinding bhb = new WebHttpBinding(WebHttpSecurityMode.None); bhb.CrossDomainScriptAccessEnabled = true; WebHttpBindingElement bce = bc.ConfiguredBindings.FirstOrDefault(o => o.Name == serviceEndpoint.BindingConfiguration) as WebHttpBindingElement; if (bce != null) { bhb.AllowCookies = bce.AllowCookies; bhb.BypassProxyOnLocal = bce.BypassProxyOnLocal; bhb.CloseTimeout = bce.CloseTimeout; bhb.HostNameComparisonMode = bce.HostNameComparisonMode; bhb.MaxBufferPoolSize = bce.MaxBufferPoolSize; bhb.MaxBufferSize = bce.MaxBufferSize; bhb.MaxReceivedMessageSize = bce.MaxReceivedMessageSize; //bhb.MessageVersion = bce.m.MessageEncoding; bhb.OpenTimeout = bce.OpenTimeout; bhb.ProxyAddress = bce.ProxyAddress; bhb.ReaderQuotas.MaxArrayLength = (bce.ReaderQuotas.MaxArrayLength != 0 ? bce.ReaderQuotas.MaxArrayLength : bhb.ReaderQuotas.MaxArrayLength); bhb.ReaderQuotas.MaxDepth = (bce.ReaderQuotas.MaxDepth != 0 ? bce.ReaderQuotas.MaxDepth : bhb.ReaderQuotas.MaxDepth); bhb.ReaderQuotas.MaxBytesPerRead = (bce.ReaderQuotas.MaxBytesPerRead != 0 ? bce.ReaderQuotas.MaxBytesPerRead : bhb.ReaderQuotas.MaxBytesPerRead); bhb.ReaderQuotas.MaxNameTableCharCount = (bce.ReaderQuotas.MaxNameTableCharCount != 0 ? bce.ReaderQuotas.MaxNameTableCharCount : bhb.ReaderQuotas.MaxNameTableCharCount); bhb.ReaderQuotas.MaxStringContentLength = (bce.ReaderQuotas.MaxStringContentLength != 0 ? bce.ReaderQuotas.MaxStringContentLength : bhb.ReaderQuotas.MaxStringContentLength); bhb.ReceiveTimeout = bce.ReceiveTimeout; bhb.SendTimeout = bce.SendTimeout; bhb.Name = bce.Name; bhb.TransferMode = bce.TransferMode; bhb.UseDefaultWebProxy = bce.UseDefaultWebProxy; } return(bhb); } case "wshttpbinding": { WSHttpBinding bhb = new WSHttpBinding(SecurityMode.None); WSHttpBindingElement bce = bc.ConfiguredBindings.FirstOrDefault(o => o.Name == serviceEndpoint.BindingConfiguration) as WSHttpBindingElement; if (bce != null) { bhb.AllowCookies = bce.AllowCookies; bhb.BypassProxyOnLocal = bce.BypassProxyOnLocal; bhb.CloseTimeout = bce.CloseTimeout; bhb.HostNameComparisonMode = bce.HostNameComparisonMode; bhb.MaxBufferPoolSize = bce.MaxBufferPoolSize; bhb.MaxReceivedMessageSize = bce.MaxReceivedMessageSize; bhb.MessageEncoding = bce.MessageEncoding; bhb.OpenTimeout = bce.OpenTimeout; bhb.ProxyAddress = bce.ProxyAddress; bhb.ReaderQuotas.MaxArrayLength = (bce.ReaderQuotas.MaxArrayLength != 0 ? bce.ReaderQuotas.MaxArrayLength : bhb.ReaderQuotas.MaxArrayLength); bhb.ReaderQuotas.MaxDepth = (bce.ReaderQuotas.MaxDepth != 0 ? bce.ReaderQuotas.MaxDepth : bhb.ReaderQuotas.MaxDepth); bhb.ReaderQuotas.MaxBytesPerRead = (bce.ReaderQuotas.MaxBytesPerRead != 0 ? bce.ReaderQuotas.MaxBytesPerRead : bhb.ReaderQuotas.MaxBytesPerRead); bhb.ReaderQuotas.MaxNameTableCharCount = (bce.ReaderQuotas.MaxNameTableCharCount != 0 ? bce.ReaderQuotas.MaxNameTableCharCount : bhb.ReaderQuotas.MaxNameTableCharCount); bhb.ReaderQuotas.MaxStringContentLength = (bce.ReaderQuotas.MaxStringContentLength != 0 ? bce.ReaderQuotas.MaxStringContentLength : bhb.ReaderQuotas.MaxStringContentLength); bhb.ReceiveTimeout = bce.ReceiveTimeout; bhb.SendTimeout = bce.SendTimeout; bhb.TextEncoding = bce.TextEncoding; bhb.TransactionFlow = bce.TransactionFlow; bhb.UseDefaultWebProxy = bce.UseDefaultWebProxy; } return(bhb); } } throw new BindingNotFoundException(Resources.BindingNotFoundException); }