Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# wcf - exception thrown when calling open() on proxy class

Tags:

c#

exception

wcf

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:

"The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be modified while it is in the Opened state."

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

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?

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 :-)

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

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();
 }

1 Answers

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

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.

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();
    }
}

Why to do this? http://msdn.microsoft.com/en-us/library/aa355056.aspx

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;
}

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.

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

like image 134
mamu Avatar answered Mar 07 '26 19:03

mamu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!