WCF Castle 客户端代理保持通道打开

发布于 2024-12-19 18:27:49 字数 327 浏览 2 评论 0原文

我正在使用 Castle WCF 工具来使用以下配置初始化客户端代理:

  <component
 id="ComponentID"
 type="SomeType, SomeType"
 wcfEndpointConfiguration="WCFConfig"
 lifestyle="PerWebRequest" >
</component>

我遇到的问题是,在实时情况下,我最终会遇到大量内存泄漏。使用内存分析器对此进行分析后,我似乎在内存中留下了大量的 WCF 通道。我期望 Castle WCF 设施在网络请求结束时清理这个问题是否正确?

I am using Castle WCF Facility to initialise a client proxy using the following configuration:

  <component
 id="ComponentID"
 type="SomeType, SomeType"
 wcfEndpointConfiguration="WCFConfig"
 lifestyle="PerWebRequest" >
</component>

The problem I'm having is that in live I'm ending up with a large memory leak. Having analysed this with a memory profiler, I seem to have a large number of WCF Channels left in memory. Am I right to expect Castle WCF Facility to clean this up at the end of the web request?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天白云 2024-12-26 18:27:49

如果您使用 perwebrequest 生活方式,则需要确保在整个请求过程中不会重新创建 HttpContext。如果是,那么您需要传递 HttpContext.Items,因为 Castle http 模块使用它来跟踪组件。

例如

改变这个:

public CapturedViewResult Capture(ControllerContext controllerContext, Func<ActionResult> action)
    {
        CapturedViewResult capturedViewResult = new CapturedViewResult();

        HttpContext currentContext = HttpContext.Current;

        using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
        {
            HttpResponse response = new HttpResponse(stringWriter);
            HttpContext context = new HttpContext(currentContext.Request, response) { User = currentContext.User };
            context.Items["LocalizationContext"] = currentContext.Items["LocalizationContext"];

            HttpContext.Current = context;

            ViewResult result = action.Invoke() as ViewResult;

            if (result != null)
            {
                capturedViewResult.ViewName = result.ViewName;
                result.ExecuteResult(controllerContext);
            }
            else
            {
                throw new ArgumentException("Supplied controller action method did not return a ViewResult", "action");
            }

            HttpContext.Current = currentContext;

            capturedViewResult.CapturedHtml = stringWriter.ToString();
        }

        return capturedViewResult;
    }

到这个:

 public CapturedViewResult Capture(ControllerContext controllerContext, Func<ActionResult> action)
    {
        var capturedViewResult = new CapturedViewResult();

        var currentContext = HttpContext.Current;

        using (var stringWriter = new StringWriter(CultureInfo.InvariantCulture))
        {
            var response = new HttpResponse(stringWriter);
            var context = new HttpContext(currentContext.Request, response) { User = currentContext.User };

            foreach (var key in currentContext.Items.Keys)
            {
                context.Items.Add(key, currentContext.Items[key]);
            }

            HttpContext.Current = context;

            var result = action.Invoke() as ViewResult;

            if (result != null)
            {
                capturedViewResult.ViewName = result.ViewName;
                result.ExecuteResult(controllerContext);
            }
            else
            {
                throw new ArgumentException("Supplied controller action method did not return a ViewResult", "action");
            }

            foreach (var key in context.Items.Keys)
            {
                currentContext.Items[key] = context.Items[key];
            }

            HttpContext.Current = currentContext;

            capturedViewResult.CapturedHtml = stringWriter.ToString();
        }

        return capturedViewResult;
    }

If you use the lifestyle perwebrequest you need to ensure that HttpContext is not recreated throughout the request. If it is, then you need to pass HttpContext.Items around as Castle http modules use this to track the components.

e.g.

Changed this:

public CapturedViewResult Capture(ControllerContext controllerContext, Func<ActionResult> action)
    {
        CapturedViewResult capturedViewResult = new CapturedViewResult();

        HttpContext currentContext = HttpContext.Current;

        using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
        {
            HttpResponse response = new HttpResponse(stringWriter);
            HttpContext context = new HttpContext(currentContext.Request, response) { User = currentContext.User };
            context.Items["LocalizationContext"] = currentContext.Items["LocalizationContext"];

            HttpContext.Current = context;

            ViewResult result = action.Invoke() as ViewResult;

            if (result != null)
            {
                capturedViewResult.ViewName = result.ViewName;
                result.ExecuteResult(controllerContext);
            }
            else
            {
                throw new ArgumentException("Supplied controller action method did not return a ViewResult", "action");
            }

            HttpContext.Current = currentContext;

            capturedViewResult.CapturedHtml = stringWriter.ToString();
        }

        return capturedViewResult;
    }

To this:

 public CapturedViewResult Capture(ControllerContext controllerContext, Func<ActionResult> action)
    {
        var capturedViewResult = new CapturedViewResult();

        var currentContext = HttpContext.Current;

        using (var stringWriter = new StringWriter(CultureInfo.InvariantCulture))
        {
            var response = new HttpResponse(stringWriter);
            var context = new HttpContext(currentContext.Request, response) { User = currentContext.User };

            foreach (var key in currentContext.Items.Keys)
            {
                context.Items.Add(key, currentContext.Items[key]);
            }

            HttpContext.Current = context;

            var result = action.Invoke() as ViewResult;

            if (result != null)
            {
                capturedViewResult.ViewName = result.ViewName;
                result.ExecuteResult(controllerContext);
            }
            else
            {
                throw new ArgumentException("Supplied controller action method did not return a ViewResult", "action");
            }

            foreach (var key in context.Items.Keys)
            {
                currentContext.Items[key] = context.Items[key];
            }

            HttpContext.Current = currentContext;

            capturedViewResult.CapturedHtml = stringWriter.ToString();
        }

        return capturedViewResult;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文