我在Service Class中通过这个示例POST方法处理请求的上下文中理解为什么我的Manager类为null时遇到了一些麻烦。 我注册了管理器和它的依赖关系,就像这个例子,它似乎正在注册。 只有这样我才能让它工作,如果我通过apphost引用告诉属性去解析通过容器。 谢谢!
public static class TemplateContainer
{
public const string DomainName = "Template";
public static Funq.Container Configure(Funq.Container container)
{
container.Register<IUOWMongo<TemplateMongoContext>>(DomainName, c =>
new UOWMongo<TemplateMongoContext>(
c.ResolveNamed<string>(Constants.NamedString)
)
).ReusedWithin(Funq.ReuseScope.Request);
container.Register<IMongoTemplateRepository>(DomainName, c =>
new MongoTemplateRepository<TemplateMongoContext>(
c.ResolveNamed<IUOWMongo<TemplateMongoContext>>(DomainName)
)
).ReusedWithin(Funq.ReuseScope.Request);
container.Register<ITemplateDataManager>(DomainName, c =>
new TemplateDataManager(c.ResolveNamed<IMongoTemplateRepository>(DomainName))
).ReusedWithin(Funq.ReuseScope.Default);
return container;
}
}
public class TemplateService : ServiceStack.ServiceInterface.Service
{
public ITemplateDataManager Manager { get; set; }
public GetTemplateResponse Post(GetTemplateRequest request)
{
var response = new GetTemplateResponse();
**// Manager is null here!!!
// Only way I can get it to work is if I do this:
Manager = AppHostBase.Instance.Container.ResolveNamed<ITemplateDataManager>("Template");**
return response;
}
}
答案 0 :(得分:1)
Funq中的AutoWiring不会查看命名依赖项。您可以将其更改为注册为普通依赖项:
container.Register<ITemplateDataManager>(c =>
new TemplateDataManager(c.ResolveNamed<IMongoTemplateRepository>(DomainName))
);