1

I have the following asp.net c# code

{      
    public class Test
    {

    ISomeService _someService;

    public Test()
    {
    }

    public void DoSomething()
    {
    _someService.Do();
    }
}

I need to provide ISomeService to Test class, and I dont know how to do it. I am not allowed to add additional construction which would make entire problem go away, for example

public Test(ISomeService someService)
{
    _someService = someService;
}

I tried using setter injection or method injection but that didnt do the trick.

Implementation of ISomeService in SomeService class also uses constructor injection, such as

public SomeService(IService1 service1, Iservice2 service2)

Not sure what to do here.

HERE IS A COMPLETE CODE

 public class Startup
    {
        private IService _service;

        public Startup()
        {

        }

        public Startup(IService service)
        {
            _service = service;
        }


        public void Configuration(IAppBuilder app)
        {
            HttpConfiguration config = new HttpConfiguration();

            var container = new UnityContainer();            
            container.RegisterType<IService, Service>();
            config.DependencyResolver = new UnityDependencyResolver(container);

            app.UseWebApi(config);

            _service.DoSomething());

        }
    }

_service is null

mko
  • 6,638
  • 12
  • 67
  • 118
  • 1
    Why can't you add parameters to your Constructor? – MindSwipe Feb 12 '19 at 15:02
  • @MindSwipe it is an owin startup class – mko Feb 12 '19 at 15:03
  • 1
    Which dependency injection framework do you use? – Magnus Feb 12 '19 at 15:04
  • If it's the owin startup class, then you can't even use DI, as your container isn't producing it. Unless owin itself has methodology for DI. – willaien Feb 12 '19 at 15:04
  • Related question [here](https://stackoverflow.com/q/19781970/9363973) – MindSwipe Feb 12 '19 at 15:05
  • https://stackoverflow.com/questions/19781970/how-to-use-di-container-when-owinstartup see here – willaien Feb 12 '19 at 15:05
  • @Magnus I am using unity – mko Feb 12 '19 at 15:05
  • I have seen that question, since it is from 2013 i thought there is a more elegant solution – mko Feb 12 '19 at 15:08
  • You still have to set up and register your container before the startup class gets created by owin. – willaien Feb 12 '19 at 15:11
  • I am confused since everything is regstered in UnityConfig.RegisterComponents all before it hits startup.cs file – mko Feb 12 '19 at 15:19
  • It would be helpful to explain why you can't add a new constructor. Also Why didn't property and method injection "not do the trick"? – Storm Muller Feb 12 '19 at 15:21
  • @StormMuller well additional constructor in startup.cs doesnt do a thing – mko Feb 12 '19 at 15:34
  • @mko This appears to be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). The question in its current state is also incomplete and therefore unclear. Read [ask] and then edit the question to provide a [mcve] that can be used to reproduce the problem, allowing a better understanding of what is being asked. – Nkosi Feb 12 '19 at 16:00
  • What part of my question is unclear to you? – mko Feb 12 '19 at 16:04

2 Answers2

0

I would suggest you use a factory to create your object. That would have an instance of ISomeService injected on the constructor.

Then in a CreateTest() method on your factory set the ISomeService property directly.

public class Factory
{
    private readonly ISomeService someService;

    public Factory(ISomeService someService)
    {
        this.someService = someService ?? throw new ArgumentNullException(nameof(someService));
    }
    public TestClass CreateTestClass()
    {
        var instance = new TestClass();
        instance.SomeService = this.someService;
        return instance;
    }

}

You should note that most DI providers have built in functionality to allow factory semantics without the need to create your own factories.

Bigtoe
  • 3,372
  • 1
  • 31
  • 47
0

What I ended up doing is this

var serviceCollection = new ServiceCollection();

serviceCollection.AddTransient<IService, Service>();

// create service provider
var serviceProvider = serviceCollection.BuildServiceProvider();

_service = ActivatorUtilities.CreateInstance<Service>(serviceProvider);

_service.DoSomething();

Thanks to this answer Dependency Injection with classes other than a Controller class

mko
  • 6,638
  • 12
  • 67
  • 118