使用Simple Injector

时间:2015-07-14 11:29:09

标签: c# restsharp simple-injector

我想使用Simple Injector注册IRestClient以将其注入服务:

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;
    }
}

但是当我尝试注册时:

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

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

   // code for simple injector mvc integration 
}

我收到错误

  

其他信息:使容器能够创建   ActiveCustomersService,它应该只包含一个public   构造函数,但它有2。

所以,我将服务改为拥有一个构造函数并注册IRestClient,分别如下:

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 
}

我意识到apiUrl对于我要注册的每项服务都需要有所不同。我怎么能这样做?

0 个答案:

没有答案