0

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

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

i try to edit global.asax to manage UserControl in this way:

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?

1 Answer 1

1

To be able to get this working, you will have to hook onto the PreLoad event of the page during initialization. During PreLoad you can walk the page's control hierarchy and initialize all controls like you do with the page itself.

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. Take a look here.

Sign up to request clarification or add additional context in comments.

8 Comments

i saw that code but where i have to put it? I honestly thought it was part of simple injector library...
@gt.guybrush: There is no integration package Web Forms, that's why we documented how to integrate with Web Forms instead. You will have to rewrite this code and copy the interesting part (you might not need master page initialization for instance). I would say you will have to call line 132 (and related code) at the place where you now call Global.InitializeHandler(handler);. But you'll have to tweak the code a bit.
@gt.guybrush: Always use the tool that suits your needs best. Good luck.
@Steven - I am using WebForms + Simple Injector, and it's working great. I keep running into road blocks for more advanced configuration, like this User Control setup. And, I have seen you reference this link ( github.com/simpleinjector/SimpleInjector/blob/master/src/… ) in numerous posts on Stack Overflow, but in every instance I get a 404. Is there a new place to find this? It'd be super helpful.
@emkayultra: Change 'master' to an older branch: github.com/simpleinjector/SimpleInjector/blob/v3.3.x/src/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.