您可以托管多个ServiceHost,每个都有自己的服务和端点,全部共享相同的基地址和端口。下面是我的实现,封装到ServiceHosting类:
public class ServiceHosting<T1, T2>
{
//Declaration
protected ServiceHost SelfHost;
protected string BaseUrlString;
protected int Port;
protected string HostUrlString = "";
protected bool ExtendedBinding;
//Constructor
public ServiceHosting(string url, int port, bool extendedBinding = false)
{
BaseUrlString = url;
Port = port;
ExtendedBinding = extendedBinding;
}
//Properties
protected int Max => int.MaxValue;
public virtual bool StartService(int port)
{
try
{
var hostName = System.Net.Dns.GetHostName();
HostUrlString = [email protected]"net.tcp://{hostName}:{port}{BaseUrlString}"; //GM 10.09.2012:
try
{
SelfHost = new ServiceHost(typeof(T1), new Uri(HostUrlString));
var smb = SelfHost.Description.Behaviors.Find<ServiceMetadataBehavior>() ??
new ServiceMetadataBehavior() { };
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
SelfHost.Description.Behaviors.Add(smb);
var throttleBehavior = new ServiceThrottlingBehavior();
SelfHost.Description.Behaviors.Add(throttleBehavior);
var mexUrlString = String.Format(@"net.tcp://{0}:{1}{2}/mex", hostName, port, BaseUrlString);
// Add MEX endpoint
SelfHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexTcpBinding(), new Uri(mexUrlString));
// Add binding
var binding = ConfigureBinding();
// Add application endpoint
SelfHost.AddServiceEndpoint(typeof(T2), binding, "");
if (ExtendedBinding)
{
foreach (ServiceEndpoint ep in SelfHost.Description.Endpoints)
{
foreach (OperationDescription op in ep.Contract.Operations)
{
var dataContractBehavior = op.Behaviors[typeof(DataContractSerializerOperationBehavior)] as DataContractSerializerOperationBehavior;
if (dataContractBehavior != null)
{
dataContractBehavior.MaxItemsInObjectGraph = Max;
}
}
}
}
// Open the service host to accept incoming calls
SelfHost.Open();
}
catch (CommunicationException)
{
// log
SelfHost.Abort();
return false;
}
catch (Exception)
{
// log
SelfHost.Abort();
return false;
}
}
catch (Exception)
{
// log
return false;
}
return true;
}
private NetTcpBinding BaseConfigureBinding()
{
return new NetTcpBinding
{ Security = { Mode = SecurityMode.None }, CloseTimeout = new TimeSpan(0, 0, 0, 5) };
}
protected virtual NetTcpBinding ConfigureBinding()
{
var binding = BaseConfigureBinding();
if (ExtendedBinding)
{
binding.MaxBufferPoolSize = Max;
binding.MaxReceivedMessageSize = Max;
binding.MaxBufferSize = Max;
binding.MaxConnections = 200; //rdoerig 12-03-2013 default value is 10:
binding.ListenBacklog = 200; //rdoerig 12-03-2013 default value is 10 : buffer of pending connections
binding.ReaderQuotas.MaxDepth = Max;
binding.ReaderQuotas.MaxStringContentLength = Max;
binding.ReaderQuotas.MaxArrayLength = Max;
binding.ReaderQuotas.MaxBytesPerRead = Max;
binding.ReaderQuotas.MaxNameTableCharCount = Max;
binding.CloseTimeout = new TimeSpan(0, 0, 10, 0);
binding.OpenTimeout = new TimeSpan(0, 0, 10, 0);
binding.ReceiveTimeout = new TimeSpan(0, 0, 10, 0);
binding.SendTimeout = new TimeSpan(0, 0, 10, 0);
}
return binding;
}
public bool StopService()
{
try
{
SelfHost?.Close();
}
catch (Exception)
{
// log
return false;
}
return true;
}
}
这可以被实例化,像这样:
private readonly ServiceHosting<LoginService, ILoginService> _serviceHostLogin = new ServiceHosting<LoginService, ILoginService>(LoginUrl, true);
启动/停止,就像这样:
_serviceHostLogin.StartService();
_serviceHostLogin.StopService();
为了确保您在托管多个服务时不会出错,您应该将服务的URI配置为不同,例如
new ServiceHosting<LoginService, ILoginService>("/Services/LoginService", true);
new ServiceHosting<ConfigService, IConfigService>("/Services/ConfigService", true);
您是否必须在同一个ServiceHost中托管两个服务,或者您是否愿意拥有多个ServiceHost实例,每个实例都有自己的服务和端点? –
@WicherVisser不幸的是,并不是因为所有的服务都需要在同一个端口上运行。 – artganify
[运行WCF ServiceHost与多个合同]可能的重复(http://stackoverflow.com/questions/334472/run-wcf-servicehost-with-multiple-contracts) –