如果用self-host的方式来加载服务的话,我们一般是用这样的代码:ServiceHost host1 = new ServiceHost(typeof(UserService)); 问题是,如果当你的服务很多的时候这样做是不胜其烦的,今天在看Ingo Rammer(大名鼎鼎的《Advanced .Net Remoting》作者)的Blog的时候,看到了他解决这一问题的方法:
一.服务配置在app.config或web.config中:

 1Host多个WCF服务(Self-host)using System;
 2Host多个WCF服务(Self-host)using System.Collections.Generic;
 3Host多个WCF服务(Self-host)using System.Reflection;
 4Host多个WCF服务(Self-host)using System.Configuration;
 5Host多个WCF服务(Self-host)using System.ServiceModel.Configuration;
 6Host多个WCF服务(Self-host)using System.ServiceModel;
 7Host多个WCF服务(Self-host)
 8Host多个WCF服务(Self-host)public class ServiceHostGroup
 9}



二.服务配置在外部配置文件中:

Services.XML:

<configuredServices>
  <service type="ServiceImplementation.ArticleService, ServiceImplementation" />
</configuredServices>


ServiceHostBase.cs:

 1Host多个WCF服务(Self-host)using System;
 2Host多个WCF服务(Self-host)using System.Collections.Generic;
 3Host多个WCF服务(Self-host)using System.Reflection;
 4Host多个WCF服务(Self-host)using System.Configuration;
 5Host多个WCF服务(Self-host)using System.ServiceModel.Configuration;
 6Host多个WCF服务(Self-host)using System.ServiceModel;
 7Host多个WCF服务(Self-host)using System.Xml;
 8Host多个WCF服务(Self-host)using System.Xml.Serialization;
 9Host多个WCF服务(Self-host)using System.IO;
10Host多个WCF服务(Self-host)
11Host多个WCF服务(Self-host)public class ServiceHostGroup
12}



鉴于本人的英文水平:),Ingo Rammer先生的说明大家还是去看原文吧。

Start ServiceHosts for all configured Services 




 

相关文章: