我已经成功地使用了这里描述的方法:
http://weblogs.asp.net/andresv/archive/2011/08/29/registering-a-wcf-service-dynamically.aspx
它使用一些反射黑客来访问 ServiceHostingEnvironment 类上的私有字段,以在运行时添加自定义的“基于配置的激活”条目。
我选择使用 HttpModule 来注册服务激活元素,如下所示:
public class ServiceRegistrationModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
// Discover and register your services here
RegisterService("~/Some/Path.svc", null, "The full service class name");
}
static object _syncRoot = new object();
static Hashtable serviceActivations;
private static void RegisterService(string addr, string factory, string service)
{
// This is the code that injects the service activation configuration.
// In WCF 4 we have the "not very well known" CBA Services (Configuration Based Activation)
// to allow us to define "file-less" WCF Services (like our Service2 here in this demo) where
// we have a section in our web.config to register services without the need of having a physical .svc file.
// The section in the web.config looks like this:
//
// <serviceHostingEnvironment multipleSiteBindingsEnabled="true" >
// <serviceActivations>
// <add relativeAddress="Service2.svc" service="WcfService2.Service2" />
// </serviceActivations>
// </serviceHostingEnvironment>
//
// And is this configuration what we are going to inject at runtime to simulate having that
// information in our web.config, while we haven't.
lock (_syncRoot)
{
if (serviceActivations == null)
{
var ensureInitialized = typeof(ServiceHostingEnvironment).GetMethod("EnsureInitialized");
ensureInitialized.Invoke(null, new object[] { });
var hostingManagerField = typeof(ServiceHostingEnvironment).GetField("hostingManager", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.GetField);
var hostingManager = hostingManagerField.GetValue(null);
var serviceActivationsField = hostingManager.GetType().GetField("serviceActivations", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField);
serviceActivations = (Hashtable)serviceActivationsField.GetValue(hostingManager);
}
if (!serviceActivations.ContainsKey(addr))
{
string value = string.Format("{0}|{1}|{2}", addr, factory, service);
serviceActivations.Add(addr, value);
}
}
}
}
使用 HttpModule 的缺点是它不适用于基于 TCP 的端点。这对我来说不是一个问题。如果这对您来说是个问题,我上面引用的博客文章描述了一种适用于基于 TCP 的端点的方法。