解析通过Simple Injector注册的ASP.NET Web窗体Image Control派生类

Ser*_*kov 3 c# asp.net dependency-injection webforms simple-injector

我准备将一个存储库实例注入一些Web.UI.WebControls.Image派生类型:

public class CustomImageControl : Image
{
    [Import]
    public ICachedNameRepository Repo { get; set; } // Null reference here

    private void DynamicImage_PreRender(object sender, EventArgs e)
    {
        ImageUrl = {some ICachedNameRepository usage}
    }
}
Run Code Online (Sandbox Code Playgroud)

这也是我为测试目的而实现的默认页面:

public partial class _Default : Page
{
    [Import]
    public ICachedNameRepository Repo { get; set; } // Totally ok here

    protected void Page_Load(object sender, EventArgs e)
    {
        {some ICachedNameRepository usage}
    }
}
Run Code Online (Sandbox Code Playgroud)

我根据官方指南实施了容器引导,关于使用Control注册而不是Page:

    private void BootStrapContainer()
    {
        var container = new Container();
        container.Options.PropertySelectionBehavior = new ImportAttributePropertySelectionBehavior();            

        container.Register<ICachedNameRepository, CachedNameRepository>();
        container.Register<CustomImageControl>(); // Also I have tried Control and Image types
        container.Register<Page>();
        var cc = container.GetInstance<CustomImageControl>(); // Correctly instantiated CachedNameRepository instance in Repo field in cc object

        container.Verify(); // OK here
        Global.Container = container;
    }
Run Code Online (Sandbox Code Playgroud)

我离开了ControlInitializerModule,ImportAttributePropertySelectionBehavior和InitializeHandler例程完全从前面提到的指南中复制了

在页面加载时,我最终得到了正确解析的默认页面实例,其中CachedNameRepository注入了正确的位置,但我的CustomImageControl遭受了空引用.

quj*_*jck 5

这可以通过挂钩InitComplete事件来完成Page.这是我用来证明这一点的代码.

我改为CustomImageControl继承自UserControl:

public partial class CustomImageControl : UserControl
{
    [Import]
    public ICachedNameRepository Repo { get; set; }

    private void DynamicImage_PreRender(object sender, EventArgs e)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

这是更新的 InitializeHandler

public class Global : HttpApplication
{
    private static Container container;

    public static void InitializeHandler(IHttpHandler handler)
    {
        if (handler is Page)
        {
            Global.InitializePage((Page)handler);
        }
    }

    private static void InitializePage(Page page)
    {
        container.GetRegistration(page.GetType(), true).Registration
            .InitializeInstance(page);

        page.InitComplete += delegate { Global.InitializeControl(page); };
    }

    private static void InitializeControl(Control control)
    {
        if (control is UserControl)
        {
            container.GetRegistration(control.GetType(), true).Registration
                .InitializeInstance(control);
        }
        foreach (Control child in control.Controls)
        {
            Global.InitializeControl(child);
        }
    }
Run Code Online (Sandbox Code Playgroud)

以及文档中的其他2个更改.一定要打电话给RegisterWebPagesAndControls你的引导程序

private static void RegisterWebPagesAndControls(Container container)
{
    var pageTypes =
        from assembly in BuildManager.GetReferencedAssemblies().Cast<Assembly>()
        where !assembly.IsDynamic
        where !assembly.GlobalAssemblyCache
        from type in assembly.GetExportedTypes()
        where type.IsSubclassOf(typeof(Page)) || type.IsSubclassOf(typeof(UserControl))
        where !type.IsAbstract && !type.IsGenericType
        select type;

    pageTypes.ToList().ForEach(container.Register);
}

class ImportAttributePropertySelectionBehavior : IPropertySelectionBehavior
{
    public bool SelectProperty(Type serviceType, PropertyInfo propertyInfo)
    {
        // Makes use of the System.ComponentModel.Composition assembly
        return (typeof(Page).IsAssignableFrom(serviceType) ||
            typeof(UserControl).IsAssignableFrom(serviceType)) &&
            propertyInfo.GetCustomAttributes<ImportAttribute>().Any();
    }
}
Run Code Online (Sandbox Code Playgroud)