Register IRestClient with different API URLs with Simple Injector

笑着哭i 提交于 2020-01-17 05:52:12

问题


I would like to register IRestClient with Simple Injector to inject it into a service:

public class ActiveCustomersService : IActiveCustomersService {
    private readonly Uri apiUri = new Uri(string.Format("{0}/{1}", ApiHelper.GetUrl(), thisApiUrl));
    private IRestClient _client;

    public ActiveCustomersService() {
        _client = new RestClient(apiUri);
    }

    public ActiveCustomersService(IRestClient client) {
        _client = client;
    }
}

However when I am trying to register it:

private void ManualServiceRegistration() {
    var container = new Container();

    container.Register<IActiveCustomersService, ActiveCustomersService>();
    container.Register<IColorPerformanceService, ColorPerformanceService>();

   // code for simple injector mvc integration 
}

I get the error

Additional information: For the container to be able to create ActiveCustomersService, it should contain exactly one public constructor, but it has 2.

So, I change the service to have a single constructor and register IRestClient, like this respectively:

public class ActiveCustomersService : IActiveCustomersService {
    private readonly Uri apiUri = new Uri(string.Format("{0}/{1}", ApiHelper.GetUrl(), thisApiUrl));
    private IRestClient _client;

    public ActiveCustomersService(IRestClient client) {
        _client = client;
    }
}

private void ManualServiceRegistration() {
    var container = new Container();

    container.RegisterSingle<IRestClient>(() => new RestClient(apiUrl))
    container.Register<IActiveCustomersService, ActiveCustomersService>();
    container.Register<IColorPerformanceService, ColorPerformanceService>();

   // code for simple injector mvc integration 
}

I realize that the apiUrl needs to be different for each of the services that I will be registering. How can I do this?

来源:https://stackoverflow.com/questions/31405336/register-irestclient-with-different-api-urls-with-simple-injector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!