tags:

views:

43

answers:

2

Hi,

My WCF service provides multiple services from different end points. Currently my client app calls these methods independently as shown below:

object result1 = null;
object result2 = null;

using (OperationContextScope contextScope = new OperationContextScope((IContextChannel)ServiceInstance))
{
   MyService.AddHeaders();
   result1 =  ServiceInstance.Method1()
}

//some other processing depending on the value of result1

using (OperationContextScope contextScope = new OperationContextScope((IContextChannel)ServiceInstance))
{
   MyService.AddHeaders();
   result2 =  ServiceInstance.Method2()
}

Now, to improve performance, I tried doing the following:

using (OperationContextScope contextScope = new OperationContextScope((IContextChannel)ServiceInstance))
{
   MyService.AddHeaders();
   result1 =  ServiceInstance.Method1()
   result2 =  ServiceInstance.Method2()
}

//some other processing depending on the value of result1 && result2

But this is failing with the error: "disposed object can't be used" and in the inner exception the disposed object is ChannelService.

Can someone help me how can I combine multiple WCF service calls under one OperationContextScope?

Thanks Aravind

A: 

I am not certain but this may be because your service might be single call (and/or does not support sessions). You may want to tweak with service behaviors to get this working.

VinayC
Hi Vinay,Thanks for your response.Yes, you are correct. The service class definition has following attributes:[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]But I don;t understand what the problem is here - when the service object is created only once, it should allow multiple calls in the same OperationContextScope. Is it not correct?
Aravind
Aravind, I am afraid that I also don't exactly know the why part. InstaceContextMode.Single means singleton i.e. same object to be used across calls (I was thinking about Per Call services). However, understand that you need not be using same service object. The *same* object that you are using is a client side proxy for the service object. Perhaps now, you can try to find out if the exception is coming from service side or from client side proxy that may help in solving the issue.
VinayC
A: 

Babai,

If the intention is only to make more than 1 call within the single OperationContextScope, you may use Innerchannel of the service.

using (OperationContextScope contextScope = new OperationContextScope((IContextChannel)abc.InnerChannel))


using (OperationContextScope contextScope = new OperationContextScope((IContextChannel)abc.InnerChannel)) {

            /* Add Headers */
                    MessageHeader header
            = MessageHeader.CreateHeader(
            "Service-Bound-CustomHeader",
            "http://Microsoft.WCF.Documentation",
            "Custom Happy Value."
            );

                    header = MessageHeader.CreateHeader(
                "Service-Bound-OneWayHeader",
                "http://Microsoft.WCF.Documentation",
                "Different Happy Value."
              );

            OperationContext.Current.OutgoingMessageHeaders.Add(header);




            Console.WriteLine(abc.GetData(100));
            Console.WriteLine(abc.GetDataUsingDataContract(new ServiceReference1.CompositeType()).ToString());
        }

Kotis