/// <summary> /// 配置应用程序 /// </summary> public void Configure(IApplicationBuilder appBuilder) { //配置WCF服务 ServiceMetadataBehavior metadataBehavior = appBuilder.ApplicationServices.GetRequiredService <ServiceMetadataBehavior>(); metadataBehavior.HttpGetEnabled = true; metadataBehavior.HttpsGetEnabled = true; UseRequestHeadersForMetadataAddressBehavior addressBehavior = new UseRequestHeadersForMetadataAddressBehavior(); DependencyInjectionBehavior dependencyInjectionBehavior = new DependencyInjectionBehavior(); InitializationBehavior initializationBehavior = new InitializationBehavior(); IList <IServiceBehavior> serviceBehaviors = new List <IServiceBehavior> { addressBehavior, dependencyInjectionBehavior, initializationBehavior }; if (AspNetSetting.Authorized) { AuthenticationBehavior authenticationBehavior = new AuthenticationBehavior(); serviceBehaviors.Add(authenticationBehavior); } appBuilder.UseServiceModel(builder => { builder.ConfigureServiceHostBase <AuthenticationContract>(host => host.Description.Behaviors.AddRange(serviceBehaviors)); builder.ConfigureServiceHostBase <AuthorizationContract>(host => host.Description.Behaviors.AddRange(serviceBehaviors)); builder.ConfigureServiceHostBase <UserContract>(host => host.Description.Behaviors.AddRange(serviceBehaviors)); }); }
protected internal override object CreateBehavior() { UseRequestHeadersForMetadataAddressBehavior behavior = new UseRequestHeadersForMetadataAddressBehavior(); foreach (DefaultPortElement DefaultPort in this.DefaultPorts) { behavior.DefaultPortsByScheme.Add(DefaultPort.Scheme, DefaultPort.Port); } return(behavior); }
/// <summary> /// Creates a ServiceHost for a specified type of service with a specific base address. /// </summary> /// <param name="serviceType">Specifies the type of service to host.</param> /// <param name="baseAddresses">The Array of type Uri that contains the base addresses for the service hosted.</param> /// <returns>A ServiceHost for the type of service specified with a specific base address.</returns> protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { ServiceHost customServiceHost = base.CreateServiceHost(serviceType, baseAddresses); UseRequestHeadersForMetadataAddressBehavior loadBalancedBehavior = customServiceHost.Description.Behaviors.Find <UseRequestHeadersForMetadataAddressBehavior>(); if (null == loadBalancedBehavior) { loadBalancedBehavior = new UseRequestHeadersForMetadataAddressBehavior(); loadBalancedBehavior.DefaultPortsByScheme.Add("http", 80); loadBalancedBehavior.DefaultPortsByScheme.Add("https", 443); customServiceHost.Description.Behaviors.Add(loadBalancedBehavior); } return(customServiceHost); }
public void OpenHost() { try { //WebHttpBinding WebHttpBinding webBinding = new WebHttpBinding(); webBinding.MaxBufferPoolSize = int.MaxValue; webBinding.MaxReceivedMessageSize = int.MaxValue; webBinding.Security.Mode = WebHttpSecurityMode.None; webBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; //NetTcpBinding NetTcpBinding tcpBinding = new NetTcpBinding(); tcpBinding.MaxBufferPoolSize = int.MaxValue; tcpBinding.MaxReceivedMessageSize = int.MaxValue; tcpBinding.Security.Mode = SecurityMode.None; tcpBinding.Security.Message.ClientCredentialType = MessageCredentialType.None; //ServiceBehavior ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpsGetEnabled = false; smb.HttpGetEnabled = true; _host.Description.Behaviors.Add(smb); ServiceDebugBehavior sdb = _host.Description.Behaviors.Find<ServiceDebugBehavior>(); sdb.HttpHelpPageEnabled = Globals.IsDebugMode; sdb.HttpsHelpPageEnabled = Globals.IsDebugMode; sdb.IncludeExceptionDetailInFaults = Globals.IsDebugMode; UseRequestHeadersForMetadataAddressBehavior urhfmab = new UseRequestHeadersForMetadataAddressBehavior(); _host.Description.Behaviors.Add(urhfmab); //MEX endpoint _host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexTcpBinding(), "mex"); if (Globals.UseClientTcpHost && Globals.UseClientHttpHost) { _host.AddServiceEndpoint(typeof(IService), tcpBinding, _tcpBaseAddress); _host.AddServiceEndpoint(typeof(IService), webBinding, _httpBaseAddress); _host.Description.Endpoints[2].Contract = CopyContract(_host.Description.Endpoints[1].Contract); } else if (Globals.UseClientTcpHost) { _host.AddServiceEndpoint(typeof(IService), tcpBinding, _tcpBaseAddress); } else if (Globals.UseClientHttpHost) { _host.AddServiceEndpoint(typeof(IService), webBinding, _httpBaseAddress); _host.Description.Endpoints[1].Contract = CopyContract(_host.Description.Endpoints[1].Contract); } _host.Open(); IsOpen = true;
/// <summary> /// 配置应用程序 /// </summary> public void Configure(IApplicationBuilder appBuilder) { //配置WCF服务 ServiceMetadataBehavior metadataBehavior = appBuilder.ApplicationServices.GetRequiredService <ServiceMetadataBehavior>(); metadataBehavior.HttpGetEnabled = true; metadataBehavior.HttpsGetEnabled = true; UseRequestHeadersForMetadataAddressBehavior addressBehavior = new UseRequestHeadersForMetadataAddressBehavior(); DependencyInjectionBehavior dependencyInjectionBehavior = new DependencyInjectionBehavior(); IList <IServiceBehavior> serviceBehaviors = new List <IServiceBehavior> { addressBehavior, dependencyInjectionBehavior }; appBuilder.UseServiceModel(builder => { builder.ConfigureServiceHostBase <ProductService>(host => host.Description.Behaviors.AddRange(serviceBehaviors)); }); }