Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Service With A Worker Thread?

I am trying to write a WCF Servce which will reside in a Windows Service. This WCF Service will just add strings to a list and then the worker thread will process this list periodically. What is the best way to achieve this? I have read conflicting examples which has left me confused. What is the best way of the service and thread sharing a list object?

Update: Thanks for the replies so far. Just to clarify, I am not struggling with the synchronization of the list or how to make it thread safe. That all seems the same principles as what I am used to in C++. What I am struggling with is where to define this list so it is accessible from both the WCF service and the worker thread. In C++ I would have created the list at global scope but C# doesn't have a global scope. If I define it in the WCF service class, the thread can't see it. If I definite it in the service class (where the thread function is defined and started) the WCF service cannot see it. I am sure I did something similar to this in ATL some time ago but it's Friday afternoon and the grey cells have given up for the day.

Update2: Where should I be defining the worker thread? In the Windows Service class (i.e. the host) or in the WCF Service? Should the WCF Service be a singleton service which has a list member and a thread function? That solves the access issue. Having done a lot of COM, I'm thinking of a WCF service as a COM component with the instance dying after it is accessed. This led me to want to put the static list and thread function in the Windows service class. That still feels the more natural place for it but perhaps I am just not thinking in a .NET way.

like image 849
Jonnster Avatar asked May 20 '11 14:05

Jonnster


2 Answers

1) Create Windows Service

2) Host WCF in the WS ( http://msdn.microsoft.com/en-us/library/ms731758.aspx )

3) Make a thread synchronization ( http://www.albahari.com/threading/part2.aspx )

4) profits!

like image 117
Wojtek Turowicz Avatar answered Sep 21 '22 11:09

Wojtek Turowicz


Here's a quick little shell which uses the concurrent collections in .NET 4 (for that matter, I'm also using for this example the Task Parallel Library in .NET 4). Your problem statement seems to be a classic producer/consumer on differing threads or processes, so this should give you a decent idea of how to structure things:

namespace ConcurrentCollectionTest
{
    using System;
    using System.Collections.Concurrent;
    using System.Threading.Tasks;

    internal static class Program
    {
        private static void Main(string[] args)
        {
            ConcurrentQueue<string> cq = new ConcurrentQueue<string>();
            BlockingCollection<string> bc = new BlockingCollection<string>(cq);
            bool moreItemsToAdd = true;

            // Consumer thread
            Task.Factory.StartNew(() =>
            {
                while (!bc.IsCompleted)
                {
                    string s = bc.Take();

                    Console.WriteLine(s);
                }
            });

            // Producer thread
            Task.Factory.StartNew(() =>
            {
                int i = 1;

                while (moreItemsToAdd)
                {
                    bc.Add("string " + i++);
                }

                bc.CompleteAdding();
            });

            // Main Thread
            Console.ReadLine();
            moreItemsToAdd = false;
            Console.ReadLine();
        }
    }
}
like image 25
Jesse C. Slicer Avatar answered Sep 21 '22 11:09

Jesse C. Slicer