简体   繁体   English

asp.web表单中的简单注入器用户自定义控件

[英]Simple injector in asp.web form user custom control

I follow official guide to use simple injector to inject object in web form and it works, but now i cant make it works in custon control 我遵循官方指南使用简单的注入器以Web形式注入对象,并且它可以工作,但是现在我无法使其在custon控制中工作

this is what i do: 这就是我的工作:

public partial class GestioneAttivita_cnStruttureSocieta : System.Web.UI.UserControl
{
    [Import]
    public IUnitOfWork iuow { get; set; }

    public Domain.Entity.Attivita attivitaGestita {get; set;}

    protected void Page_Load(object sender, EventArgs e)
    {
        using (iuow)
        {       
            attivitaGestita = iuow.Attivita.Read(attivitaGestita.IdAttivita);
        }
   }

} }

but i get null reference exception since iuow is null 但是我得到空引用异常,因为iuow为空

i try to edit global.asax to manage UserControl in this way: 我尝试编辑global.asax以这种方式管理UserControl:

private static void RegisterWebPages(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;

            foreach (Type type in pageTypes)
            {
                var registration = Lifestyle.Transient.CreateRegistration(type, container);
                registration.SuppressDiagnosticWarning(
                    DiagnosticType.DisposableTransientComponent,
                    "ASP.NET creates and disposes page classes for us.");
                container.AddRegistration(type, registration);
            }
        }
    }

         class ImportAttributePropertySelectionBehavior : IPropertySelectionBehavior {
            public bool SelectProperty(Type serviceType, PropertyInfo propertyInfo) {
                // Makes use of the System.ComponentModel.Composition assembly

                bool _return = false;

                _return = (typeof(Page).IsAssignableFrom(serviceType) &&
                    propertyInfo.GetCustomAttributes<ImportAttribute>().Any())
                    **||
                    (typeof(UserControl).IsAssignableFrom(serviceType) &&
                    propertyInfo.GetCustomAttributes<ImportAttribute>().Any());**

                return _return;
            }
        }

but i get same error 但我得到同样的错误

is this doable? 这可行吗?

To be able to get this working, you will have to hook onto the PreLoad event of the page during initialization. 为了使此工作正常进行,您必须在初始化期间挂接到页面的PreLoad事件。 During PreLoad you can walk the page's control hierarchy and initialize all controls like you do with the page itself. PreLoad期间,您可以遍历页面的控件层次结构,并像对页面本身一样初始化所有控件。

There's actually example code in the Simple Injector repository (that never made it to an official package) that shows you how to do this. 实际上,Simple Injector存储库中有示例代码(从未将其放入官方软件包中)向您展示了如何执行此操作。 Take a look here . 在这里看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM