本文介绍了wcf OperationContextScope 处置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 c# 应用程序,它使用 OperationContextScope scope = new OperationContextScope(i.InnerChannel); 调用 wcf 服务;

I have a c# app that calls a wcf serivce using OperationContextScope scope = new OperationContextScope(i.InnerChannel);

我需要让连接保持打开状态,因此我无法使用 Using 语句处理 OperationContextScope.但是,在查看内存分析器时,我看到了数百个 OperationContextScope.我需要处理范围,但是当我调用 .Dispose() 时,我收到一条错误消息,说它不正常.我不知道为什么我不能处置范围.

I need to leave the connections open so I cannot dispose the OperationContextScope with the Using statement. However when looking at the memory profiler I am seeing hundreds of OperationContextScope's. I need to dispose the scope but when I call .Dispose() I get an error saying its out of order. I have no idea why I cannot dispose the scope.

有谁知道如何正确处理 OperationContextScope ?以下是我的部分代码.

Does anyone know how to correctly dispose OperationContextScope ? Below is part of my code.

                BasicHttpBinding wsbinding = null;                  
                 OperationContextScope scope  = null;

                    wsbinding = new BasicHttpBinding(BasicHttpSecurityMode.None);
                    wsbinding.MaxBufferSize = 2147483647;
                    wsbinding.MaxReceivedMessageSize = 2147483647;
                    wsbinding.Name = "BasicHttpBinding_Iretail";



                i = new IretailClient(wsbinding, new EndpointAddress(commonStuff.EndpointAddress));

                scope = new OperationContextScope(i.InnerChannel);

推荐答案

来自 http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontextscope.aspx:

创建 OperationContextScope 时,当前 OperationContext被存储并且新的 OperationContext 成为由目前的财产.当 OperationContextScope 被释放时,原始 OperationContext 恢复.

显然,它们必须按照创建时的相反顺序进行处理.

Clearly, they must be disposed in the reverse order they were created.

scope.Dispose();

这篇关于wcf OperationContextScope 处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:28