Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Castle windsor代理生成内存泄漏_C#_Asp.net Mvc_Memory Leaks_Castle Windsor_Dynamic Proxy - Fatal编程技术网

C# Castle windsor代理生成内存泄漏

C# Castle windsor代理生成内存泄漏,c#,asp.net-mvc,memory-leaks,castle-windsor,dynamic-proxy,C#,Asp.net Mvc,Memory Leaks,Castle Windsor,Dynamic Proxy,Castle windsor用于MVC应用程序,如下所述: 在我的应用程序中,有一个区别,那就是方法AddControllerLogging功能: var controller = ((IController)container.Kernel.Resolve(controllerType)).AddControllerLoggingFunctionality(); 此方法在记录器类中: [DebuggerStepThrough] public static class Logger {

Castle windsor用于MVC应用程序,如下所述:

在我的应用程序中,有一个区别,那就是方法AddControllerLogging功能:

var controller = ((IController)container.Kernel.Resolve(controllerType)).AddControllerLoggingFunctionality();
此方法在记录器类中:

[DebuggerStepThrough]
public static class Logger
{
    private static readonly Castle.DynamicProxy.ProxyGenerator proxyGenerator;
    static Logger()
    {
        proxyGenerator = new Castle.DynamicProxy.ProxyGenerator();
        Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add(
            typeof(ServiceContractAttribute));
    }

    public static TInterface AddControllerLoggingFunctionality<TInterface>(this TInterface implementation)
        where TInterface : class
    {
        if (implementation == null)
        {
            throw new ArgumentNullException("implementation");
        }

        if (!typeof(TInterface).IsInterface)
        {
            throw new Exception("Type of 'TInterface' must be interface.");
        }

        Castle.DynamicProxy.ProxyGenerationOptions options =
            new Castle.DynamicProxy.ProxyGenerationOptions();

        var origAttribs = implementation.GetType().GetCustomAttributesData();
        if (origAttribs != null)
        {
            foreach (var origAttrib in origAttribs)
            {
                options.AdditionalAttributes.Add(
                    AttributeUtil.CreateBuilder(origAttrib));
            }
        }

        return (TInterface)proxyGenerator.CreateInterfaceProxyWithTarget<TInterface>(
            implementation,
            options,
            new ControllerLoggingInterceptor(implementation.GetType()));
    }
}
[DebuggerStepThrough]
公共静态类记录器
{
私有静态只读Castle.DynamicProxy.ProxyGenerator ProxyGenerator;
静态记录器()
{
proxyGenerator=newcastle.dynamicProxyGenerator.proxyGenerator();
Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add(
类型(ServiceContractAttribute));
}
公共静态TInterface AddControllerLogging功能(此TInterface实现)
TInterface的位置:类
{
if(实现==null)
{
抛出新的异常(“实现”);
}
if(!typeof(TInterface).i接口)
{
抛出新异常(“TInterface的类型必须是接口”);
}
Castle.DynamicProxy.ProxyGeneration选项=
newcastle.DynamicProxy.ProxyGenerationOptions();
var origAttribs=implementation.GetType().GetCustomAttributesData();
if(origAttribs!=null)
{
foreach(origAttrib中的var origAttrib)
{
options.AdditionalAttributes.Add(
创建生成器(origAttrib));
}
}
返回(TInterface)proxyGenerator.CreateInterfaceProxyWithTarget(
实施
选项,
新的ControllerLoggingInterceptor(implementation.GetType());
}
}
及 有人能解释一下吗?为什么IController可以调用AddControllerLogging功能,它的作用是什么

由于此更改,此控制器永远不会从内存中释放(当 container.Kernel.ReleaseComponent(controller);被调用)和内存泄漏。 “发布策略跟踪的对象”计数器一直在增加。
如果我删除AddControllerLogging功能,则每次调用ReleaseComponent时,“由发布策略跟踪的对象”计数器都会减少,并且不会发生内存泄漏。

您不是在控制器上调用
release()
,而是在手动创建的代理上调用它,所以
release()
对于Windsor来说只是一个禁忌,因为它不知道该对象,所以一直跟踪控制器组件

如果您使用Windsor的,您不必担心这个问题,因为Windsor将知道在传递其自己的内部托管代理时如何处置组件

如果要在更改为Windsor的内置支持之前测试此功能,请将代理强制转换为
Castle.DynamicProxy.IProxyTargetAccessor
,并调用
DynProxyGetTarget()
,以获取需要传递到
Release()
的控制器实例


(这个答案是从Castle用户邮件列表中复制的)

您不是在控制器上调用
Release()
,而是在您手动创建的代理上调用它,因此
Release()
对于Windsor来说只是一个否定的选项,因为它不知道该对象,因此一直跟踪控制器组件

如果您使用Windsor的,您不必担心这个问题,因为Windsor将知道在传递其自己的内部托管代理时如何处置组件

如果要在更改为Windsor的内置支持之前测试此功能,请将代理强制转换为
Castle.DynamicProxy.IProxyTargetAccessor
,并调用
DynProxyGetTarget()
,以获取需要传递到
Release()
的控制器实例


(此答案复制自Castle用户邮件列表中的我们的答案)

AddControllerLogging功能是做什么的,您能提供源代码吗?抱歉,我附加了错误的方法。现在我已经包括了正确的一个。我不确定它实际上做什么-我不熟悉动态代理。现在,如果我知道如何处置控制器(释放内存),那就太好了。AddControllerLogging功能做什么,你能提供源代码吗?对不起,我附加了错误的方法。现在我已经包括了正确的一个。我不确定它实际上做什么-我不熟悉动态代理。就目前而言,如果我知道如何处置控制器(释放内存),那就太好了。