views:

91

answers:

2

Hi there,

i'd like to create a windows service that would be accessible via WCF from a web application. The workflow would be:

  1. Windows Service starts and would hold a default value in memory.
  2. A web app would connect the windows service via wcf and take the value and after some processing set the value back in the memory.

  3. In a while would happend the same as in point 2., and so on,so on

This value would be hold only in memory.

The point is I dont know where put the variable that would be hold in the memory of the windows service.There is a Service class wich is instancied in a Program class wich is static class. So whats the best place to put a variable that would be hold in the memory as long as the service is running ?

And second question,is it correct to reference the exe of the windows service in a DLL ??

+1  A: 

There is a service class for every windows service that also contains the start and stop methods. However, for your service, I'd simply create a singleton class that is accessed from the class that handles the WCF client requests.

There's no need to reference the exe of the service (and I strongly recommend you not to do that), as when you're using WCF you'll insert a service reference into your client project and just need two methods to get and set the data.

I suggest: Design your operation and data contracts for the service and then create the service reference within your client project. That'll make things clear.

If you don't know what I'm talking about, I recommend googleing for WCF samples.

EDIT
You write in the comment that you created a service class with a private field. I suppose you didn't actually do what I suggested :-) I said: Create a singleton class that is accessed by the class that handles the get/set requests.

public class ValueHolder
{
    private static ValueHolder m_singleton = null;
    private int m_someValue;

    private ValueHolder()
    {
        m_someValue = 0;
    }

    public static ValueHolder Instance
    {
        get
        {
            if (m_singleton = null)
                m_singleton = new ValueHolder();
            return m_singleton;
        }
    }

    public int SomeValue
    {
        get { return m_someValue; }
        set { m_someValue = value; }
    }
}

So now you have a window service class that you use to host a WCF service. The WCF service contains methods to get/set the value from ValueHolder.Instance.SomeValue. These methods are exposed to the client using the data contract.

Start your service and add a service reference to the client using the respective option in Visual Studio (not a reference to the DLL, but a service reference!!). The client now accesses the get/set methods of the service.

Job done, where's my money? :-D

Thorsten Dittmar
hello i did like u said,but its not working. I created a Service class with a set and get method and a private field with default value 5.This class is referenced by the windows service. I ran the service.Then a called the get method from a unit test class and got the default value 5,then I called the Set method with argument 9 and afterwards a called again the Get method. The mathod returned a 5 value again,not 9 as expected.
Post some code. Can't tell without code.
Thorsten Dittmar
just great, it works perfect! whats your account number ?? :-) To go one step further, wouldn't it be better to have a factory method for getting the singletons ?
Well, I guess in this case creating a factory would be over the top as the purpose and implementation of the singleton is quite clear and will supposedly not be replaced by other implementations (actually you could also replace the singleton class with a private static variable within your WCF service class, but the singleton-way seemed "cleaner" to me)
Thorsten Dittmar
A: 

It almost sounds like a separate service might be a little overkill for what you're after - maybe you should take a look at Inter-Process shared memory.

Take a look at #3 in this article.

http://www.codeproject.com/KB/threads/csthreadmsg.aspx

Jaco Pretorius
I think the client should be a web application, which makes Inter Process share memory hard to use :-)
Thorsten Dittmar