I am building a windows service to host a WCF service, and I am using :
var ServicesToRun = new ServiceBase[]
{ new Service_1_Host() };
ServiceBase.Run(ServicesToRun);
My question is:
if I have a second service with its host, and I added to the array in th开发者_高级运维e above code, like the following:
var ServicesToRun = new ServiceBase[] { new Service_1_Host(),
new Service_2_Host() };
ServiceBase.Run(ServicesToRun);
does the second host run with its own app domain, or there is some configuration I have to do to make the two hosts run it separate app domain each?
You approaching this all wrong.... you need to keep apart:
the Windows NT Service (derived from
ServiceBase) which is merely here to be running around the clockthe actual WCF service hosts (derived from
ServiceHost) which provide the real WCF service interfaces.
Basically, what you need to do is this:
in your NT Service (
ServiceBase) there's anOnStartevent - inside that event, you need to create and open your WCFServiceHostinstances - one per WCF service (implementation) classin that NT Service (
ServiceBase) there's anOnStopevent, inside of which you should close your WCF service hosts
Some your code would look roughly something like:
using System;
using System.ServiceModel;
namespace YourNameSpace
{
public class WcfHostService : ServiceBase
{
private ServiceHost _serviceHost1 = null;
private ServiceHost _serviceHost2 = null;
protected override void OnStart(string[] args)
{
// instantiate new ServiceHost instances
if (_serviceHost1 == null)
{
_serviceHost1 = new ServiceHost(typeof(YourService1));
}
if (_serviceHost2 == null)
{
_serviceHost2 = new ServiceHost(typeof(YourService2));
}
// open service hosts
_serviceHost1.Open();
_serviceHost2.Open();
}
protected override void OnStop()
{
if (_serviceHost1.State != CommunicationState.Closed)
{
_serviceHost1.Close();
}
if (_serviceHost2.State != CommunicationState.Closed)
{
_serviceHost2.Close();
}
}
}
}
and then in your NT Service main app, you would have:
var servicesToRun = new ServiceBase[] { new WcfHostService() };
ServiceBase.Run(servicesToRun);
That's all, really!
Notice that this is an array of ServiceBase, not of HostsBase or something. You are simply starting up multiple Windows Services. This has nothing to do with WCF or even with AppDomains.
加载中,请稍侯......
精彩评论