问题
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