开发者

Max concurrent sessions for WCF using IIS and Asynch methods - releasing resources

I am calling a WCF service from a form. Service is hosted in IIS. The service with the following attributes: InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple

I have set the throttling behaviour to max out at 2000 for all concurrent calls, instances and sessions.

However I cant seem to get more than 200 ASynch requests. After that the service just does not respond, and the session eventually times out.

I am using an Asynch call to call the method in the service ie Collapse | Copy Code

IASyncResult res= Proxy.BeginMethod(endCall,null);

and then to catch the response I have a endCall function that takes in the IASyncResult and processes the result via EndMethod().

in a load simulation @ 1 method call a second, it all works fine until around 200 calls, and then just waits.开发者_Python百科.. (I have 199 or 198 responses from those 200 calls at this point in time).. - so theoretically there should NOT be 200 concurrent sessions just 2 or so..

Perhaps there is some garbage collection or closing that I am not doing? any suggestions?

----update---

I think the answer maybe more that the closing of the proxy is not handled in a thread safe way. As Radik points out above, closing the proxy is important however with many IASyncState results coming in simultaneously you have to make sure you close the right one at the right time.

I did try firing off the close proxy from a thread to let it handle it seperately:

ThreadPool.QueueUserWorkItem(CloseProxy, ar.AsyncState);

but that does not seem to work. Any suggestions on this?


When you create service reference in VS, generated proxy allow you asynchronously call service by two ways with callback or event handler. And two different places where you can close proxy. little sample project here

//close proxy in callback function
private void ButtonCallbackClick(object sender, EventArgs e)
{
    var proxy = new ServiceClient("BasicHttpBinding_IService");
    proxy.BeginDoWork(DateTime.Now.ToShortDateString(), CallBack, proxy);
}

private void CallBack(IAsyncResult ar)
{
    var result = (ar.AsyncState as ServiceClient).EndDoWork(ar);
    if (ar.IsCompleted)
        UpdateView(result);
    CloseProxy(ar.AsyncState);
}
//close proxy in event handler
private void ButtonCompletedClick(object sender, EventArgs e)
{
    var proxy = new ServiceClient("BasicHttpBinding_IService");
    proxy.DoWorkAsync(DateTime.Now.ToShortDateString());
    proxy.DoWorkCompleted += DoWorkCompleted;
}

private void DoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
{
    if (e.Error == null)
        UpdateView(e.Result);
    CloseProxy(sender);
}

private static void CloseProxy(object sender)
{
    var proxy = sender as ServiceClient;
    if (proxy == null) return;
    try
    {
        proxy.Close();
    }
    catch (CommunicationException e)
    {
        proxy.Abort();
    }
    catch (TimeoutException e)
    {
        proxy.Abort();
    }
    catch (Exception e)
    {
        proxy.Abort();
    }
}

private static bool _run = false;
//run async query in infinite cycle
private void ButtonCycleClick(object sender, EventArgs e)
{
    _run = !_run;
    if (!_run) return;
    Action<object> action = WaitEvent;
    ThreadPool.QueueUserWorkItem(a => action(action));
}

private void WaitEvent(object action)
{
    var proxy = new ServiceClient("BasicHttpBinding_IService");
    proxy.DoWorkAsync(DateTime.Now.ToShortDateString());
    proxy.DoWorkCompleted += (x, y) => DoWorkCompleted(x, y, action as Action<object>);
}

private void DoWorkCompleted(object sender, DoWorkCompletedEventArgs e, Action<object> action)
{
    if (!_run)
        return;

    if (e.Error == null)
        UpdateView(e.Result);
    CloseProxy(sender);
    action(action);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜