开发者

How to determine whether WCF Service is hosted within IIS or something else?

I have a WCF Service that is sometimes hosted within IIS and sometimes hosted within a Windows Service. This is necessary for different de开发者_如何学Cployment options for my application. What's a good way within the service object to determine where it is hosted?

The reason I want this is that when my service is hosted in IIS it gets connection string info from the web.config file in the normal way. When it's hosted by the windows service it gets config info in a different way (from info in the registry). Within the constructor of my service object I want to note where to get connection string info from, e.g. something like this:

public void MyServiceObject()
{
     _hostedInIis = isHostedInIis();
}
private string ConnectionString()
{ 
     if (_hostedInIis)
         return ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
     else 
         return GetConnectionStringFromRegistry();
}

Alternatively, is there a way I can pass some configuration information to it when I'm hosting it within the windows service? e.g. pass it the connection string or a flag saying it's hosted within the windows service.


I needed something similar when I set up a service to run either standalone with config, or under IIS with no config file at all. I ended up using a custom ServiceHostFactory for the IIS case. After the factory constructs the ServiceHost, it adds a custom configuration object to its Extensions property with the information that I couldn't put into web.config. Something like this:

protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
  var host = base.CreateServiceHost(serviceType, baseAddresses);
  host.Extensions.Add(new CustomConfigurer());
  return host;
}

Then in the .svc file for the service, you can specify the custom factory, and IIS will just use it:

<%@ ServiceHost Service="..." Factory="..." %>

When your service operation is called, it can look for the config object on the host's extensions:

public int Add(int x, int y)
{
  var config = OperationContext.Current.Host.Extensions.Find<CustomConfigurer>();
  if (config == null)
  {
    // Load elsewhere
  }
}


I opted to subclass my service class and used the subclass when hosting in my Windows Service. The only difference in the subclass is that it knows to get its config info from the registry instead of the default location: the config manager. This is kinda nicer that what I originally wanted because the service doesn't need to know where it is.


probabily is not the best way but you can check HttpContext which is normally present in an Asp.net application

 private string ConnectionString()
        {
            if (HttpContext.Current!=null)
                return ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
            else
                return GetConnectionStringFromRegistry();
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜