3

我正在尝试在使用泛型的电话容器上注册服务。

public class JsonWebClient<TResult> : IJsonWebClient<TResult>

我是这样注册的:

protected override void Configure()
{
    _container = new PhoneContainer(RootFrame);

    _container.RegisterPhoneServices();
    _container.Singleton<MainPageViewModel>();
    _container.PerRequest<LoginViewModel>();


    _container.RegisterPerRequest(typeof(IJsonWebClient<>), "jsonwebclient", typeof(JsonWebClient<>));
}

然后我有一个服务(注册服务),我JsonWebClient在构造函数中注入

public SignupService(IJsonWebClient<UserDto> webClient)
{
    _webClient = webClient;
}

我的问题是它webClient始终为空。

4

1 回答 1

3

似乎SimpleContainerCaliburn.Micro 内部不支持开放泛型注册。

所以你需要IJonWebClient<T>为每个T

_container.RegisterPerRequest(
    typeof(IJsonWebClient<UserDto>),
    "jsonwebclientuser", 
    typeof(JsonWebClient<UserDto>));
_container.RegisterPerRequest(
    typeof(IJsonWebClient<OtherDto>), 
    "jsonwebclientother", 
    typeof(JsonWebClient<OtherDto>));

注意:如果您不按键解析,keynull调用RegisterPerRequest. 所以它应该是这样的:

_container.RegisterPerRequest(
    typeof(IJsonWebClient<UserDto>),
    null, 
    typeof(JsonWebClient<UserDto>));

或者您可以使用其他一些支持开放泛型的 IoC 容器,例如 Ninject 或 Autofac。

于 2012-08-21T12:08:30.790 回答