.NET Framework Bookmark and Share   
 index > Windows Communication Foundation > Safe multi threading WCF services
 

Safe multi threading WCF services

I have some silverlight application which can trigger some WCF operation which can takes hours.
So i started a new thread (through ThreadPool) which does whatever it must. I also have in SL some pooling to check if operation is completed.

My problem is how execute some code from thread in main thread (if it even exists). Something like (in Silverlight) Dispatcher.BeginInvoke(....
In my thread, at one point, i am saving something to file, but at this moment some other thread could read from this file, so i must ensure that this can not happened. How?

I also notice that inside Thread variable HttpContext.Current is null.
Fora.Cof
For your file operation, can you use the lock synchronization mechanism?

Object fileOp = new Object(); //this object must be visiblefrom all thread

lock(fileOp)
{
[write file]
}

lock(fileOp)
{
[read file]
}


To execute something from the main thread (which always is the UI thread in Silverlight), use System.Windows.Deployment.Dispatcher.BeginInvoke(...)

Are you using AspNetCompatMode and PollingDuplex in combination? That would explain the HttpContext.Current.Thread being a null value.
Christopher Scrosati, Software Design Engineer, WCF Silverlight, Microsoft Corp.
Christopher Scrosati - MSFT
Thanks for the locking idea. It came to me a few minutes before i read your post, and now i know that this is the best solution.

About executing some code in Main thread you misunderstand me.
I mean in WCF WebService. If WebService even has a main thread?

I am running my own logic for polling.

And i run into another problem.

My code is:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyWSTest : IMyWSTest
{
    private static Dictionary<string, TProgressItem> fProgresses = null;
    public MyWSTest()
    {
        if (fProgresses == null)
            fProgresses = new Dictionary<string, TProgressItem>();
    }
//.....
I hope that variable fProgresses will be there for me forever, but that's not the case. On the next call to WebService, fProgresses is null. I understand that i jump every call to WebService to the constructor, but how can i keep alive my variable fProgresses for as long as the ASP.NET Development Server runs?

I think, that when i choose another hosting environment, this will not be the problem. Or my thinking is incorrect? But how to achieve this in development time?
Fora.Cof

You can use google to search for other answers

Custom Search

More Threads

• Different ways of returning data from WCF Services and their advantages
• Enumerations don't work correctly with svcutil or .svc generated schemas
• Service configuration Scalability and reusabliltiy
• Custom security header and namespace prefix control
• wcf function call hangs windows application
• Using Named Pipes while disconneted from the internet
• Help me to catch the logic behind session parameters ????
• Client is unable to finish the security negotiation within the configured timeout
• Windows Communication Foundation samples
• Adding an additional FaultContract to an inherited operation