问题
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