C#WCF - 在代理类调用open()时抛出异常 [英] c# wcf - exception thrown when calling open() on proxy class

查看:175
本文介绍了C#WCF - 在代理类调用open()时抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题,基本上我有一个WCF服务,在小测试中操作罚款。然而,当我尝试批/负载测试中,我得到一个出现InvalidOperationException 与当open()方法被调用的代理类的消息:

I have the following problem, basically i have a WCF service which operates fine in small tests. However when i attempt a batch/load test i get an InvalidOperationException with the message when the open() method is called on the proxy class:

通信对象,System.ServiceModel.Channels.ServiceChannel,不能同时处于开启状态修改。

我已搜查谷歌,但找不到任何人真的引用此异常消息。

I have searched google, but cannot find anyone else really quoting this exception message.

我想上的服务的一些进一步的信息可以用于诊断需要 - 当服务通过的公开的方法它的一个接收数据,它基本上执行一些处理和路由数据以与该数据相关联的服务(不同的数据将产生不同的路由)。以确保该服务尽快运行,接收,处理和数据的路由的每个周期通过在线程池一个单独的线程来处理。这可能是一个线程调用 proxyClass.Open(),而另一个已经在使用它的问题?将一个锁定块消除这个问题,如果这确实是问题。

I guess some further info on the service may be necessary for a diagnosis - when the service receives data through one of it's exposed methods, it basically performs some processing and routes the data to a service associated with the data (different data will result in different routing). To ensure that the service runs as quickly as possible, each cycle of receiving, processing and routing of the data is handled by a seperate thread in the threadpool. could this be a problem arising from one thread calling proxyClass.Open() whilst another is already using it? would a lock block eliminate this problem, if indeed this is the problem?

谢谢你们, - 我一直workikng在这个项目上的久,终于要看到它的背面 - 但是这似乎是最后的绊脚石,所以任何帮助非常感谢: - )

thanks guys, - ive been workikng on this project for too long, and finally want to see the back of it - but this appears to be the last stumbling block, so any help much appreciated :-)

======================================= ============================



感谢您强调我不应该使用使用构造WCF代理类。但是MSDN文章不是文学最明确的书面有史以来一块,所以有快速的问题:我应该使用一个代理这样:

=========================================================================

thanks for highlighting that i shouldn't be using the using construct for WCF proxy classes. However the MSDN article isn't the most clearly written piece of literature ever, so one quick question: should i be using a proxy as such:

try
{
    client = new proxy.DataConnectorServiceClient();
    client.Open();
    //do work
    client.Close();
 }
 .................. //catch more specific exceptions
 catch(Exception e)
 {
    client.Abort();
 }


推荐答案

为每个调用创建新的代理对象。添加一些关于如何使用代理的代码。

How are you using proxy? Creating new proxy object for each call. Add some code regarding how you use proxy.

使用代理的所需方法是为您创建新代理的每个调用,并在完成后进行处理。您正在为打开的代理调用proxy.open()是错误的。它应该只被调用一次。

Desired way of using proxy is for each call you create new proxy and dispose it once completed. You are calling proxy.open() for opened proxy that is wrong. It should be just called once.

最后尝试使用下面的内容,因为wcf不处理失败的代理,它堆积起来。不确定它会帮助,但给它一个镜头。

Try using something like below in finally, as wcf does not dispose failed proxy and it piles up. Not sure it would help but give it a shot.

if (proxy.State == CommunicationState.Faulted)
{
    proxy.Abort();
}
else
{
    try
    {
        proxy.Close();
    }
    catch
    {
        proxy.Abort();
    }
}

为什么要这样做?
http://msdn.microsoft.com/en-us/library /aa355056.aspx

您在上面发布的代码可以正常工作,但是您将始终在吃异常。所以处理wcf相关的异常在单独的捕获和你的通用捕获与Excelion将中止然后抛出异常。

Code you posted above would work but you will alway be eating exception. So handle wcf related exception in seperate catch and your generic catch with Excelion would abort then throw exception.

try
{
    ...
    client.Close();
}
catch (CommunicationException e)
{
    ...
    client.Abort();
}
catch (TimeoutException e)
{
    ...
    client.Abort();
}
catch (Exception e)
{
    ...
    client.Abort();
    throw;
}

此外,如果您仍然想要使用方便的语句,方法,并在wcf错误的情况下使用abort方法处理。

Also if you still want to use convenience of using statement then you can override dispose method in your proxy and dispose with abort method in case of wcf error.

并且不需要调用.Open(),因为它会在第一次调用时打开。

And do not need to call .Open() as it will open when required with first call.

这篇关于C#WCF - 在代理类调用open()时抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆