简体   繁体   English

使用IIS和Asynch方法的WCF的最大并发会话-释放资源

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

I am calling a WCF service from a form. 我正在从表单调用WCF服务。 Service is hosted in IIS. 服务托管在IIS中。 The service with the following attributes: InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple 具有以下属性的服务:InstanceContextMode.PerSession,ConcurrencyMode = ConcurrencyMode.Multiple

I have set the throttling behaviour to max out at 2000 for all concurrent calls, instances and sessions. 我已将所有并发调用,实例和会话的限制行为设置为最大2000。

However I cant seem to get more than 200 ASynch requests. 但是我似乎无法收到200多个ASynch请求。 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 | 我正在使用Asynch调用来调用服务中的方法,即Collapse | Copy Code 复制代码

IASyncResult res= Proxy.BeginMethod(endCall,null); 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(). 然后要捕获响应,我有一个endCall函数,该函数接受IASyncResult并通过EndMethod()处理结果。

in a load simulation @ 1 method call a second, it all works fine until around 200 calls, and then just waits... (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.. 在每秒调用1次方法的负载模拟中,一切正常,直到大约200次调用,然后等待...(此时我对这200次调用有199或198个响应)..-从理论上讲,不应是200个并发会话,只有2个左右。

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. 正如Radik指出的那样,关闭代理很重要,但是由于同时出现许多IASyncState结果,因此必须确保在正确的时间关闭正确的代理。

I did try firing off the close proxy from a thread to let it handle it seperately: 我确实尝试从线程中触发关闭代理,以使其单独处理:

ThreadPool.QueueUserWorkItem(CloseProxy, ar.AsyncState); 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. 在VS中创建服务引用时,生成的代理允许您使用回调或事件处理程序通过两种方式异步调用服务。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM