C# WCF NetTCPBinding Blocking Application
Asked Answered
G

1

6

I have a basic buddylist type application which is a pub/sub deal in WCF. My problem is one or two of the calls are long running and this blocks up the entire server application (gui updates etc).

Here's my code:

[ServiceContract(SessionMode = SessionMode.Required,
CallbackContract = typeof(IBuddyListContract))]
public interface IBuddyListPubSubContract
{
    [OperationContract]
    string GetABunchOfDataZipped(String sessionId); // this can take > 20 seconds

    ....
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, 
    ConcurrencyMode = ConcurrencyMode.Multiple)]
public class BuddyListPubSubContract : IBuddyListPubSubContract
{
    string GetABunchOfDataZipped(String sessionId)
    {
        // do some calculations and data retrival
        return  result;
    }
}

So far I have an idea on how to go about it but is there a simpler way?

Idea 1: Have GetABunchOfDataZipped(String sessionId) be a void, when it finishes have another endpoint which on my duplex contract which I hit. I don't like this as ifs a fundamental change in my architecture and if the string is a large block of text over a slow internet connection it will still suffer from the same issue?

Grosbeak answered 31/5, 2011 at 19:7 Comment(0)
G
2

My problem is one or two of the calls are long running and this blocks up the entire server application (gui updates etc).

You're not clear on where you're seeing the blocking behavior, but it sounds like it would be on the client side. You should be making your call to your WCF service from a background thread, not the UI thread. Then when you handle the result, you won't be able to interact with your UI elements directly, you will need to use each control's Invoke method.

Gilford answered 31/5, 2011 at 19:33 Comment(3)
I currently create the ServiceHost right in my UI thread in the load event. I should create it in a background thread instead?Grosbeak
Also the blocking behavior is in the server. The client calls all the methods inside a background thread.Grosbeak
@nextgenneo If you create your ServiceHost on the UI thread, yes, by default it will only handle requests on the UI thread. However, you can set [ServiceBehavior(UseSynchronizationContext=false)] on your service implementation class, and that will cause requests to be processed by worker threads from the thread pool. See code-magazine.com/article.aspx?quickid=0701041&page=3 for a full explanation.Gilford

© 2022 - 2024 — McMap. All rights reserved.