1

I'm trying to start up a project which takes care of stuff. I call this class the CentralLogic class. When this is started, I'd like to start a host. The host has some settings in the App.Config file like the baseAddress and endpoints.

This host can interact with this CentralLogic class. I'd like to pass on the instantiated object to this host.

Currently, I'm trying something like this:

centralLogic = new CentralLogic();
ServiceHost host = new ServiceHost(centralLogic, typeof(KLAService));
using (host) 
{ 
    host.Open();
    //Start a WPF UI. Also makes sure the host stays open
    //as long as the UI stays open.       
    Application app = new Application();
    app.Run(new ConfigurationWPF.MainWindow(centralLogic));

    host.Close();
}

The KLAService is defined like this:

public class KLAService : IKLAService
{
    CentralLogic centralLogic;

    public KLAService(CentralLogic theCentralLogic)
    {
        centralLogic= theCentralLogic;
    }
    .....
}

This doesn't work (the creation of the ServiceHost is wrong, both in number of arguments and the second parameter). I can get it started without parameter by doing:

ServiceHost host = new ServiceHost(typeof(KLAService));

So, the problem is that I don't know how to pass on the object on to the server. How do I do this?

EDIT: I tried the following:

centralLogic = new CentralLogic();
KLAService klaService = new KLAService(centralLogic);
using (ServiceHost host = new ServiceHost(klaService))
{ 
    host.Open();

    Application app = new Application();
    app.Run(new ConfigurationWPF.MainWindow(centralLogic));

    host.Close();
}

This popped an InvalidOperationException:

In order to use one of the ServiceHost constructors that takes a service instance, the InstanceContextMode of the service must be set to InstanceContextMode.Single. This can be configured via the ServiceBehaviorAttribute. Otherwise, please consider using the ServiceHost constructors that take a Type argument.

4

0 回答 0