简体   繁体   中英

Using SqlDependency in WCF Class Library project hosted as Windows Service

I have created a notification service using SqlDependency for notifying other services about the change in a particular table. This is done in the Windows Service template from Visual Studio. Hence in the OnStart event, subscription of SqlDependency is created, and during OnStop event, un-subscription is done.

The above solution is working perfectly fine.

Now, due to some reasons, I have to make this a WCF class library project and host it as a Windows service. I couldn't think of a place where the subscription and un-subscription of SqlDependency can be done in such case.

When the Windows service is started, the subscription has to be done automatically.

Note: I have a generic Windows service host, which will host all my WCF class library project and can't do much there.

Is there any solution or workaround for this?

I hope that I got your problem right. You have all the logic inside of WCF library but don't know how to sync your subscribe and unsubscribe to SqlDependancy events on Windows Service start-up that will host your WCF service.

My idea is to use a ServiceHostFactory to create an instance of your service and hook on opening and closing events of your service, calling all necessary constructors and connectors from there.

I based most of my answer from this great and lengthy article about hosting WCF services, with link pointing directly to the topic in your case. Some additional docs on ServiceHost and ServiceHostFactory classes.

Bear in mind that this is not a complete code that you can just copy/paste but rather a demonstration of usage.

Here is an example of your Windows service:

public partial class YourWindowsService : ServiceBase
{

    // It's your choice where to create this instance, I used constructor injection here arbitrarily
    private readonly YourWCFServiceFactory serviceFactory;   

    private ServiceHost host;

    public YourWindowsService(YourWCFServiceFactory serviceFactory)
    {
        InitializeComponent();
        this.serviceFactory = serviceFactory;
    }

    protected override void OnStart(string[] args)
    {
        Type serviceType = typeof(YourService);
        host = serviceFactory.CreateServiceHost(serviceType, new string[] { "yourBaseUri" });
        host.Open();
    }

    protected override void OnStop()
    {
        if(host != null)
           host.Close();
    }
}

And an example of your factory:

public class YourWCFServiceFactory: ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses);
        host.Opening += new EventHandler(host_Opening);
        host.Closing += new EventHandler(host_Closing);
        return host;
    }

    private void host_Opening(object sender, EventArgs e)
    {
        // Initialization here
    }

    private void host_Opening(object sender, EventArgs e)
    {
        // Cleanup here
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM