Servicestack with Autofac not resolving IRequestContext

旧巷老猫 提交于 2019-12-13 17:17:29

问题


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

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;

EDIT:

My Service already extends ServiceStack.ServiceInterface.Service:

public class UserDetailsService : ServiceStack.ServiceInterface.Service

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


回答1:


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

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



来源:https://stackoverflow.com/questions/14494256/servicestack-with-autofac-not-resolving-irequestcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!