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
}
}