在WCF中我有许多个Service怎样用一个host来托管呢?

来源:百度知道 编辑:UC知道 时间:2024/05/17 18:54:09
我要的是只声明一个ServiceHost而不是多个,我这里有400个service 难道要创建400个ServiceHost ...谢谢。

此次[我是新手,答得不好多多包涵]
----------------------------
1)Host如果是IIS的话
添加WCF即可,默认就是Host->多WCF

2)Console或者WinService的话

Sample:
创建ConsoleApplication后,添加俩个WCFService
Service1(IService1) / Service2(IService2)
然后删除App.config(我们将手动创建Host这样更加清楚)

进入Program.cs

static void Main(string[] args)
{
try
{
Uri baseAddress = new Uri("http://localhost:8002/Service1");
ServiceHost Host = new ServiceHost(typeof(Service1), baseAddress);
Host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "Service1");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
Host.Description.Behaviors.Add(smb);