C# 调用多个WCF服务返回错误-已处置对象可以';不可用

C# 调用多个WCF服务返回错误-已处置对象可以';不可用,c#,wcf,C#,Wcf,我的WCF服务从不同的端点提供多种服务。 目前我的客户端应用程序独立调用这些方法,如下所示: object result1 = null; object result2 = null; using (OperationContextScope contextScope = new OperationContextScope((IContextChannel)ServiceInstance)) { MyService.AddHeaders(); result1 = ServiceI

我的WCF服务从不同的端点提供多种服务。 目前我的客户端应用程序独立调用这些方法,如下所示:

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()
}
现在,为了提高性能,我尝试了以下操作:

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
但这失败了,错误是:“disposed object can't used”,在内部异常中,disposed object是ChannelService

有人能帮助我如何在一个OperationContextScope下组合多个WCF服务调用吗

谢谢
Aravind

我不确定,但这可能是因为您的服务可能是单次呼叫(和/或不支持会话)。您可能需要调整服务行为以使其正常工作。

如果只想在单个
OperationContextScope
中进行一次以上的调用,您可以使用服务的
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());
        }


你好,Vinay,谢谢你的回复。是的,你说得对。服务类定义具有以下属性:[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single,ConcurrencyMode=ConcurrencyMode.Multiple)],但我没有;我不理解这里的问题是什么-当服务对象只创建一次时,它应该允许在同一OperationContextScope中进行多个调用。是不是不对?Aravind,恐怕我也不知道为什么。InstaceContextMode.Single意味着singleton,即跨调用使用相同的对象(我考虑的是每次调用服务)。但是,请理解,您不需要使用相同的服务对象。您正在使用的同一对象是服务对象的客户端代理。也许现在,您可以尝试找出异常是来自服务端还是来自客户端代理,这可能有助于解决问题。