简体   繁体   中英

Windows Service - WCF Service Design

I have a Windows Service that hosts a WCF service and I am successfully able to connect to it using WCFTestClient and a Custom Client. The windows service is based upon what used to be an exe, but since the program will be used for a long running process on a server, the service is a better route. The problem is that I cannot access static variables in the application from the WCF service.

In the .exe (I switched this to a .dll which is the server application) I use a global class implemented as such:

public static class Globals
{
    ....
}

This holds references to the major parts of the program so that if any part needs to reference another I can use the syntax Globals.JobManager.RunJob() .

The problem that I am encountering is that the WCF service is not able to reference Globals at run-time. One example of where I need this to be done is in the GetJob method:

public class ConsoleConnection : IConsoleConnection
{
    public string[] RetrieveJobList()
    {
        string[] jobs = Globals.JobManager.GetAllJobNames().ToArray();
        return jobs;
    }
}

This method returns null when tested in WCFTestClient and throws an exception in the created client.

I believe this issue to be caused by the way the Windows Service, WCF Service, and the application DLL are initiated. The current method is such:

public class ETLWindowsService : ServiceBase
{
....
    protected override void OnStart(string[] args)
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
        }

        Globals.InitializeGlobals();
        serviceHost = new ServiceHost(typeof(ConsoleConnection));

        serviceHost.Open();
    }
....
}

Here the Windows Service starts, Calls the Globals.InitializeGlobals() that creates all the necessary parts of the application, then starts the WCF service (If this is the wrong way to do this, let me know. I'm piecing this together as I go). I'm assuming that these actions are being done in the wrong order and that is the cause of the problems.

Do I need to have the Windows Service create the WCF Service which in turn creates the application (this doesnt make sense to me), or do I have the Windows Service create the application which then creates the WCF Service? Or is there a third option that I am missing?

The application is in a .dll with the WCF in a separate .dll

I totally agree with Andy H .

If I review this kind of code, I won't try to make the stuff work with the global static variable (even if in the end this is probably possible). A static global class is smelly. First of all, I will figure out to make it work without it.

There are several solution: dependency injection, messaging communication, event driven...

To help you: a long running process in a web service is very common, youy have a good description here . But in any case, it never uses a static class to synchronize the jobs :)

Improve your design, and you will see that your current problem won't exist at all.

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