protected void EnsureComMetaDataExchangeBehaviorAdded(Configuration config) { ServiceModelSectionGroup sg = ServiceModelSectionGroup.GetSectionGroup(config); if (!sg.Behaviors.ServiceBehaviors.ContainsKey(comServiceBehavior)) { ServiceBehaviorElement behavior = new ServiceBehaviorElement(comServiceBehavior); sg.Behaviors.ServiceBehaviors.Add(behavior); ServiceMetadataPublishingElement metadataPublishing = new ServiceMetadataPublishingElement(); if (Tool.Options.Hosting == Hosting.Complus || Tool.Options.Hosting == Hosting.NotSpecified) { metadataPublishing.HttpGetEnabled = false; } else { metadataPublishing.HttpGetEnabled = true; } behavior.Add(metadataPublishing); ServiceDebugElement serviceDebug = new ServiceDebugElement(); serviceDebug.IncludeExceptionDetailInFaults = false; behavior.Add(serviceDebug); } }
public static string GetServiceBehaviorElementXml(this IEnumerable <BehaviorExtensionElement> serviceBehaviors) { var serviceBehaviorElement = new ServiceBehaviorElement("ServiceBehavior"); serviceBehaviors.Each(b => serviceBehaviorElement.Add(b)); var configurationProxy = new ConfigurationProxy(); configurationProxy.SetServiceBehaviorElement(serviceBehaviorElement); return(configurationProxy.GetServiceBehaviorElementXml()); }
/// <summary> /// This method adds the neccessary configuration elements to hook up /// Thinktecture.ServiceModel.Extensions.Metdata extension to service the /// service code being generated. /// </summary> private void AddMetadataServiceBehavior() { // Get a pointer to system.serviceModel section. ConfigurationSectionGroup csg = configuration.SectionGroups["system.serviceModel"]; // Notify if we get a null reference. Debug.Assert(csg != null, "system.serviceModel section could not be found in the configuration."); if (csg != null) { // Try to find the extensions element. ExtensionsSection extensionsSection = csg.Sections["extensions"] as ExtensionsSection; // Create it if it wasn't found. if (extensionsSection == null) { extensionsSection = new ExtensionsSection(); csg.Sections.Add("extensions", extensionsSection); } // Now create the new behavior extension. ExtensionElement metadataServiceExtensionElement = new ExtensionElement(); metadataServiceExtensionElement.Name = "metadataService"; //TODO: Make this more dynamic so it can discover the assembly version etc otherwise this will always throw exceptions // that the behavior extension was not found in the collection. metadataServiceExtensionElement.Type = "Thinktecture.ServiceModel.Extensions.Metadata.StaticMetadataBehaviorElement, Thinktecture.ServiceModel.Extensions.Metadata, Version=1.1.0.0, Culture=neutral, PublicKeyToken=20fb7cabbfb92df4"; // Add the newly created behavior extension to the extensions section. extensionsSection.BehaviorExtensions.Add(metadataServiceExtensionElement); // Try to find the behaviors element. BehaviorsSection behaviorsSection = csg.Sections["behaviors"] as BehaviorsSection; // Create it if it wasn't found. if (behaviorsSection == null) { behaviorsSection = new BehaviorsSection(); csg.Sections.Add("behaviors", behaviorsSection); } // Add the new service behavior. ServiceBehaviorElement serviceBehavior = new ServiceBehaviorElement(); serviceBehavior.Name = "metadataServiceExtension"; behaviorsSection.ServiceBehaviors.Add(serviceBehavior); StaticMetadataBehaviorElement behaviorExtensionElement = new StaticMetadataBehaviorElement(); behaviorExtensionElement.RootMetadataFileLocation = options.MetadataLocation; behaviorExtensionElement.MetadataUrl = "wsdl"; serviceBehavior.Add(behaviorExtensionElement); // Find the service section. ServicesSection servicesSection = csg.Sections["services"] as ServicesSection; if (servicesSection != null) { string fqServiceTypeName = GetFullyQulifiedTypeName(GetServiceTypeName()); ServiceElement serviceElement = servicesSection.Services[fqServiceTypeName] as ServiceElement; if (serviceElement != null) { serviceElement.BehaviorConfiguration = "metadataServiceExtension"; } } } }
protected void EnsureComMetaDataExchangeBehaviorAdded(Configuration config) { ServiceModelSectionGroup sg = ServiceModelSectionGroup.GetSectionGroup(config); if (!sg.Behaviors.ServiceBehaviors.ContainsKey(comServiceBehavior)) { ServiceBehaviorElement behavior = new ServiceBehaviorElement(comServiceBehavior); sg.Behaviors.ServiceBehaviors.Add(behavior); ServiceMetadataPublishingElement metadataPublishing = new ServiceMetadataPublishingElement(); if (Tool.Options.Hosting == Hosting.Complus || Tool.Options.Hosting == Hosting.NotSpecified) metadataPublishing.HttpGetEnabled = false; else metadataPublishing.HttpGetEnabled = true; behavior.Add(metadataPublishing); ServiceDebugElement serviceDebug = new ServiceDebugElement(); serviceDebug.IncludeExceptionDetailInFaults = false; behavior.Add(serviceDebug); } }
/// <summary> /// This method adds the neccessary configuration elements to hook up /// Thinktecture.ServiceModel.Extensions.Metdata extension to service the /// service code being generated. /// </summary> private void AddMetadataServiceBehavior() { // Get a pointer to system.serviceModel section. ConfigurationSectionGroup csg = configuration.SectionGroups["system.serviceModel"]; // Notify if we get a null reference. Debug.Assert(csg != null, "system.serviceModel section could not be found in the configuration."); if (csg != null) { // Try to find the extensions element. ExtensionsSection extensionsSection = csg.Sections["extensions"] as ExtensionsSection; // Create it if it wasn't found. if (extensionsSection == null) { extensionsSection = new ExtensionsSection(); csg.Sections.Add("extensions", extensionsSection); } // Now create the new behavior extension. ExtensionElement metadataServiceExtensionElement = new ExtensionElement(); metadataServiceExtensionElement.Name = "metadataService"; //TODO: Make this more dynamic so it can discover the assembly version etc otherwise this will always throw exceptions // that the behavior extension was not found in the collection. metadataServiceExtensionElement.Type = "Thinktecture.ServiceModel.Extensions.Metadata.StaticMetadataBehaviorElement, Thinktecture.ServiceModel.Extensions.Metadata, Version=1.0.14.0, Culture=neutral, PublicKeyToken=20fb7cabbfb92df4"; // Add the newly created behavior extension to the extensions section. extensionsSection.BehaviorExtensions.Add(metadataServiceExtensionElement); // Try to find the behaviors element. BehaviorsSection behaviorsSection = csg.Sections["behaviors"] as BehaviorsSection; // Create it if it wasn't found. if (behaviorsSection == null) { behaviorsSection = new BehaviorsSection(); csg.Sections.Add("behaviors", behaviorsSection); } // Add the new service behavior. ServiceBehaviorElement serviceBehavior = new ServiceBehaviorElement(); serviceBehavior.Name = "metadataServiceExtension"; behaviorsSection.ServiceBehaviors.Add(serviceBehavior); StaticMetadataBehaviorElement behaviorExtensionElement = new StaticMetadataBehaviorElement(); behaviorExtensionElement.RootMetadataFileLocation = options.MetadataLocation; behaviorExtensionElement.MetadataUrl = "wsdl"; serviceBehavior.Add(behaviorExtensionElement); // Find the service section. ServicesSection servicesSection = csg.Sections["services"] as ServicesSection; if (servicesSection != null) { string fqServiceTypeName = GetFullyQulifiedTypeName(GetServiceTypeName()); ServiceElement serviceElement = servicesSection.Services[fqServiceTypeName] as ServiceElement; if (serviceElement != null) { serviceElement.BehaviorConfiguration = "metadataServiceExtension"; } } } }
/// <summary> /// 获取启动服务参数 /// </summary> /// <param name="implementsContractType">实体契约类的类型</param> /// <param name="interfaceContractType">契约类型</param> /// <param name="uri">服务地址</param> /// <param name="binding">启动服务类型</param> public void GetServiceConfig(Type implementsContractType, Type interfaceContractType, Uri uri, BindingType binding) { ImplementsContractType = implementsContractType; AddService(implementsContractType.ToString()); ServiceEndpointElement item = new ServiceEndpointElement(uri, interfaceContractType.ToString()); item.BindingConfiguration = item.Name = interfaceContractType.ToString(); item.Binding = binding.ToString(); ServiceElement service = serviceconfig.Services[implementsContractType.ToString()]; service.Endpoints.Add(item); SetBindingParam(uri, binding, item.BindingConfiguration); if (!behaviorconfig.ServiceBehaviors.ContainsKey(service.BehaviorConfiguration)) { ServiceBehaviorElement haviorelement = new ServiceBehaviorElement();// _setting.BehaviorConfig.ServiceBehaviors[service.BehaviorConfiguration]; haviorelement.Name = service.BehaviorConfiguration; #region 并发设置 //List<ServiceThrottlingElement> throttlingConfig = haviorelement.OfType<ServiceThrottlingElement>().ToList(); ServiceThrottlingElement throttlingBehavior = new ServiceThrottlingElement();// host.Description.Behaviors.Find<ServiceThrottlingBehavior>(); //当前ServiceHost能够处理的最大并发消息数量,默认值为16 throttlingBehavior.MaxConcurrentCalls = int.MaxValue; //当前ServiceHost允许存在的InstanceContext的最大数量,默认值为26 throttlingBehavior.MaxConcurrentInstances = int.MaxValue; //当前ServiceHost允许的最大并发会话数量,默认值为10 throttlingBehavior.MaxConcurrentSessions = int.MaxValue; //throttlingConfig.Add(throttlingBehavior); haviorelement.Add(throttlingBehavior); #endregion #region 序列化最大项 DataContractSerializerElement dataContractSerializerElement = new System.ServiceModel.Configuration.DataContractSerializerElement(); dataContractSerializerElement.MaxItemsInObjectGraph = 2147483647; haviorelement.Add(dataContractSerializerElement); #endregion #region 是否充许客户端看到详细错误信息 ServiceDebugElement debugConfig = new ServiceDebugElement(); debugConfig.IncludeExceptionDetailInFaults = _isShowErrorInfoToClient; haviorelement.Add(debugConfig); #endregion behaviorconfig.ServiceBehaviors.Add(haviorelement); } }
public CodeGenerationResults Generate(IArtifactLink link) { CodeGenerationResults result = new CodeGenerationResults(); string serviceImplementationName = string.Empty; string serviceContractName = string.Empty; string serviceNamespace = string.Empty; const string behavior = "_Behavior"; if (link is IModelReference) { this.serviceProvider = Utility.GetData <IServiceProvider>(link); ProjectNode project = Utility.GetData <ProjectNode>(link); ServiceDescription serviceDescription = ((IModelReference)link).ModelElement as ServiceDescription; Configuration configuration = GetConfiguration(link, project); // abort if we got errors in config file if (configuration == null) { return(result); } try { ServiceReference serviceReference = (ServiceReference)serviceDescription; SCModel.Service service = GetMelReference <SCModel.Service>(serviceReference.ServiceImplementationType); serviceImplementationName = ResolveTypeReference(service); serviceContractName = GetServiceContractName(service.ServiceContract); serviceNamespace = service.ServiceContract.Namespace; ServiceModelConfigurationManager manager = new ServiceModelConfigurationManager(configuration); ServiceElement serviceElement = new ServiceElement(); serviceElement.Name = serviceImplementationName; serviceElement.BehaviorConfiguration = string.Concat(serviceImplementationName, behavior); foreach (Endpoint endpoint in serviceDescription.Endpoints) { ServiceEndpointElement endpointElement = new ServiceEndpointElement(); endpointElement.Name = endpoint.Name; endpointElement.Contract = serviceContractName; endpointElement.Binding = ((WcfEndpoint)endpoint.ObjectExtender).BindingType.ToString(); endpointElement.Address = new Uri(endpoint.Address ?? string.Empty, UriKind.RelativeOrAbsolute); endpointElement.BindingNamespace = serviceNamespace; serviceElement.Endpoints.Add(endpointElement); } manager.UpdateService(serviceElement); ServiceBehaviorElement behaviorElement = new ServiceBehaviorElement(); behaviorElement.Name = string.Concat(serviceImplementationName, behavior); ServiceDebugElement debugElement = new ServiceDebugElement(); debugElement.IncludeExceptionDetailInFaults = false; behaviorElement.Add(debugElement); if (((WcfServiceDescription)serviceDescription.ObjectExtender).EnableMetadataPublishing) { ServiceMetadataPublishingElement metadataPublishingElement = new ServiceMetadataPublishingElement(); metadataPublishingElement.HttpGetEnabled = true; behaviorElement.Add(metadataPublishingElement); ServiceEndpointElement mexEndpointElement = ServiceModelConfigurationManager.GetMetadataExchangeEndpoint(); serviceElement.Endpoints.Add(mexEndpointElement); } manager.UpdateBehavior(behaviorElement); manager.Save(); result.Add(link.ItemPath, File.ReadAllText(configuration.FilePath)); } finally { if (configuration != null && File.Exists(configuration.FilePath)) { File.Delete(configuration.FilePath); } } } return(result); }