0

I am attempting to make a new project that is dependent on some old libraries. Those libraries are using Unity for dependency injection. They are also using the Property Injection with the DependencyAttribute. My new project is a ASP.NET Core 2.1 application using the provided Angular Template. I have configured it for unity doing the following:

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
    var container = /* calls internal method to get container */;

    return WebHost.CreateDefaultBuilder(args)
        .UseUnityServiceProvider(container)
        .UseStartup<Startup>();
}

The container is created in an internal library and works fine everywhere else.

When I use Constructor Injections the services are injected correctly, however using the Property Injection does not work. All properties injected in this way end up NULL. Any ideas how to get Property Injection to work?

Constructor Injection (works):

private readonly IServiceA ServiceA;
public ControllerA(IServiceA serviceA)
{
    this.ServiceA = serviceA;
}

Property Injection (doesn't work):

[Dependency]
public IServiceA ServiceA { get; set; }
Joe
  • 2,649
  • 21
  • 33
  • is the `UseUnityServiceProvider` extension 3rd part or custom local? – Nkosi Oct 22 '18 at 20:33
  • 1
    Check this answer and see if it helps https://stackoverflow.com/a/39173346/5233410 – Nkosi Oct 22 '18 at 20:35
  • `UseUnityServiceProvider` comes with the Unity Nuget Package – Adam Vincent Oct 22 '18 at 20:38
  • here's another link that may help:: https://github.com/unitycontainer/examples/tree/master/src/AspNetCoreExample but I like the Unity service provider interface commented above, it looks like it will work better for your purposes. – Adam Vincent Oct 22 '18 at 20:38
  • Don't use property injection. You are not supposed to do that. Either inject services into the controller or into controller-endpoints using `[FromServices]` dataattribute. – alsami Oct 23 '18 at 07:09
  • This ended up being something completely different. The error message was just confusing. The property injection is working in my older libraries there was just a services somewhere down the chain that was not in the container. Thanks for all the help though! – Joe Oct 23 '18 at 16:35

0 Answers0