本文介绍了使用Autofac的Servicestack无法解析IRequestContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Service Stack的Cache功能.这些可以通过RequestContext,由IOC在您的Service中注入.

I am trying to use the Cache facilities of Service Stack. These are accessed through theRequestContext, which is injected by the IOC in your Service.

如果使用默认的Funq IOC,此方法将按预期工作,当您挂接AutoFac,RequestContext为null且我不确定如何配置autofac来构建它时,它将无法正常工作.这里有什么线索吗?我的AutoFac配置:

This works as expected if you are using the default Funq IOC, it does not work when you hook AutoFac, RequestContext is null and I am not sure how to configure autofac to build it. Any clues here? My AutoFac configuration:

var builder = new ContainerBuilder();

//Now register all dependencies to your custom IoC container

builder.RegisterAssemblyTypes(new[] { typeof(AppHost).Assembly })
       .PropertiesAutowired(PropertyWiringFlags.AllowCircularDependencies)
       .AsImplementedInterfaces()
       .SingleInstance();

container.Register<ICacheClient>(new MemoryCacheClient());

IContainerAdapter adapter = new AutofacIocAdapter(builder.Build());
container.Adapter = adapter;

我的服务已经扩展了ServiceStack.ServiceInterface.Service:

My Service already extends ServiceStack.ServiceInterface.Service:

public class UserDetailsService : ServiceStack.ServiceInterface.Service

实现IRequiresRequestContext的

,RequestContext为null.如果我删除了autofac,那么它将按预期工作.如果使用Autofac,则RequestContext为空

which implements IRequiresRequestContext, RequestContext is null. If I remove autofac then it works as expected. With Autofac RequestContext is null

推荐答案

RequestContext并不是要由IOC注入的,它是ServiceStack设置的特殊属性,如果您的Service通过实现IRequiresRequestContext接口进行请求的话.例如.

RequestContext is not meant to be injected by an IOC, it's a special property that is set by ServiceStack if your Service requests it by implementing the IRequiresRequestContext interface. E.g.

public class MyClass : IService, IRequiresRequestContext {
    //injected by ServiceStack at run-time (per request)
    public IRequestContext RequestContext { get; set; } 
}

这与在便捷的默认 ServiceStack中的Service 基类.

This is the same mechanism how the RequestContext property gets populated in the convenient default Service base class in ServiceStack.

这篇关于使用Autofac的Servicestack无法解析IRequestContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 20:06