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; }
6
  • is the UseUnityServiceProvider extension 3rd part or custom local? Commented Oct 22, 2018 at 20:33
  • 1
    Check this answer and see if it helps stackoverflow.com/a/39173346/5233410 Commented Oct 22, 2018 at 20:35
  • UseUnityServiceProvider comes with the Unity Nuget Package Commented Oct 22, 2018 at 20:38
  • here's another link that may help:: github.com/unitycontainer/examples/tree/master/src/… but I like the Unity service provider interface commented above, it looks like it will work better for your purposes. Commented Oct 22, 2018 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. Commented Oct 23, 2018 at 7:09

0

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.