tags:

views:

33

answers:

2

Hi, I have the following WCF code:

ServiceHost host = null;

if (host == null)
    host = new ServiceHost(typeof(RadisService));

How can i get a pointer to my RadisService, to make calls with it?

Well it was really for testing purposes, but please allow me to ask the question anyway, for educational purposes. What happens if my service is running on a machine (using a GUI host), several clients from different remote machines connect to the service and through the GUI leave comments on my service.

The code on my service looks like this:

public class MyClass 
{ 
    [DataMember] 
    static Dictionary<String, Variable> m_Variables = new 
        Dictionary<String, Variable>();
    .... 
}

[ServiceContract] 
public interface IMyClassService 
{ 
    [OperationContract] 
    bool AddVariable(String name, Variable value); 

    [OperationContract] 
    bool RemoveVariable(String name);

    [OperationContract] 
    bool GetVariable(string name, Variable variable); 

    [OperationContract] List<String> GetVariableDetails();
    ... 
}

So from my service host GUI i would like to be able to access GetVariableDetails(), and preview all the comments added from all the different clients at this point. How would i achieve this?

A: 

You cannot. The ServiceHost will host 1-n service class instances to handle incoming requests, but those are typically "per-call", e.g. a service class instance is created when a new request comes in, a method is called on the service class, and then it's disposed again.

So the ServiceHost doesn't really have any "service" class instance at hand that it can use and call methods on.

What exactly are you trying to achieve?

Update: the service host should really not do anything besides hosting the service - it should definitely not be calling into the service itself.

What you're trying to achieve is some kind of an administrative console - a GUI showing the current comments in your system. Do this either via a direct database query, or then just have a GUI console to call into your service and get those entries - but don't put that burden on the ServiceHost - that's the wrong place to put this functionality.

marc_s
This is a correct logical answer, since the service host shouldn't really be responsible for this. Thanks for pointing that out.
Tamer
A: 

If you make your service a singleton you can create an instance of the service and give it to the ServiceHost:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class CalculatorService: ICalculatorService
{

....

CalculatorService service = new CalculatorService();
ServiceHost serviceHost = new ServiceHost(service, baseAddress);
BasvdL
This is the exactly correct answer, that's basically the answer i was looking for, since my service was a singleton. Thank you.
Tamer