/// <summary> /// Close the SpringServiceHost /// </summary> public void Dispose() { if (SpringServiceHost != null) { SpringServiceHost.Dispose(); } }
private void CreateMetadataBehavior(SpringServiceHost ssh, ServiceMetadataBehavior smb, ServiceEndpointElement metadataEle) { if (smb == null) { smb = new ServiceMetadataBehavior(); if (ssh.BaseAddresses.Any(o => o.Scheme.ToLower() == Uri.UriSchemeHttp)) { smb.HttpGetEnabled = true; } ssh.Description.Behaviors.Add(smb); } foreach (var baseAddress in ssh.BaseAddresses) { //BindingElement bindingElement = null; Binding bindingElement = null; switch (baseAddress.Scheme) { case "net.tcp": { bindingElement = MetadataExchangeBindings.CreateMexTcpBinding(); //bindingElement = new TcpTransportBindingElement(); break; } case "net.pipe": { bindingElement = MetadataExchangeBindings.CreateMexNamedPipeBinding(); //bindingElement = new NamedPipeTransportBindingElement(); break; } case "http": { bindingElement = MetadataExchangeBindings.CreateMexHttpBinding(); //bindingElement = new HttpTransportBindingElement(); break; } case "https": { bindingElement = MetadataExchangeBindings.CreateMexHttpsBinding(); //bindingElement = new HttpsTransportBindingElement(); break; } default: throw new ProtocolException("The base address {0} Unable to identify".FormatString(baseAddress.ToString())); } if (bindingElement != null) { //Binding binding = new CustomBinding(bindingElement); ssh.AddServiceEndpoint(typeof(IMetadataExchange), bindingElement, "MEX"); } } }
/// <summary> /// Publish the object. /// </summary> public virtual void AfterPropertiesSet() { ValidateConfiguration(); springServiceHost = new SpringServiceHost(TargetName, objectFactory, UseServiceProxyTypeCache, BaseAddresses); springServiceHost.Open(); #region Instrumentation if (LOG.IsInfoEnabled) { LOG.Info(String.Format("The service '{0}' is ready and can now be accessed.", TargetName)); } #endregion }
/// <summary> /// Publish the object. /// </summary> public virtual void AfterPropertiesSet() { ValidateConfiguration(); //springServiceHost = new SpringServiceHost(TargetName, objectFactory, UseServiceProxyTypeCache, BaseAddresses); SpringServiceHost.Export(); #region Instrumentation if (LOG.IsInfoEnabled) { LOG.Info($"The service '{ObjectType.Name}' is ready and can now be accessed."); } #endregion }
static void Main(string[] args) { try { //DbProviderFactory.DBPROVIDER_ADDITIONAL_RESOURCE_NAME = "assembly://Chanyi.ERP.Dao/DbProviders.xml"; // 加载配置 IApplicationContext context = ContextRegistry.GetContext(); // 启动多个服务。 Logger.Info("准备启动服务。"); Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location); ServiceModelSectionGroup svcmod = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel"); foreach (ServiceElement el in svcmod.Services.Services) { if (context.ContainsObjectDefinition(el.Name)) { Logger.Info(String.Format("启动{0}服务...", el.Name)); SpringServiceHost serviceHost = new SpringServiceHost(el.Name); serviceHost.Opened += delegate { Logger.Info(el.Name + " 服务已经启动了。"); }; serviceHost.Open(); } } DomainConfiguration.Create().RegisterEventHandler(context.GetObject("eRPDao")); // 等待按 q 键退出。 Logger.Info("服务已经启动,输入 q 退出!"); string s1 = Console.ReadLine(); while (String.Compare("q", s1, true) != 0) { s1 = Console.ReadLine(); } } catch (Exception ex) { Logger.Error("启动服务时发生错误", ex); Console.ReadLine(); } }
static void Main(string[] args) { try { // 加载配置 IApplicationContext context = ContextRegistry.GetContext(); // 启动多个服务。 Logger.Info("准备启动服务。"); Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location); ServiceModelSectionGroup svcmod = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel"); foreach (ServiceElement el in svcmod.Services.Services) { if (context.ContainsObjectDefinition(el.Name)) { Logger.Info(String.Format("启动{0}服务...", el.Name)); SpringServiceHost serviceHost = new SpringServiceHost(el.Name); serviceHost.Opened += delegate { Logger.Info(el.Name + " 服务已经启动了。"); }; serviceHost.Open(); } } // 等待按 q 键退出。 Logger.Info("服务已经启动,输入 q 退出!"); string s1 = Console.ReadLine(); while (String.Compare("q", s1, true) != 0) { s1 = Console.ReadLine(); } } catch (Exception ex) { Logger.Error("启动服务时发生错误", ex); Console.ReadLine(); } }
/// <summary> /// Publish the object. /// </summary> public virtual void AfterPropertiesSet() { ValidateConfiguration(); springServiceHost = new SpringServiceHost(TargetName, objectFactory, UseServiceProxyTypeCache, BaseAddresses); springServiceHost.Open(); #region Instrumentation if (LOG.IsInfoEnabled) { LOG.Info(String.Format("The service '{0}' is ready and can now be accessed.", TargetName)); } #endregion }
/// <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); }
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; } } } } } } }