WCF城堡客户端代理使通道打开

本文关键字:通道 代理 城堡 客户端 WCF | 更新日期: 2023-09-27 18:16:50

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

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

我遇到的问题是,在生活中,我结束了一个大的内存泄漏。在用内存分析器分析了这一点后,我似乎在内存中留下了大量的WCF通道。我期待城堡WCF设施在web请求结束时清理这个是对的吗?

WCF城堡客户端代理使通道打开

如果你使用生活方式perwebrequest,你需要确保HttpContext不会在整个请求中被重新创建。如果是,那么你需要传递HttpContext。项目周围的城堡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;
    }