Asp.net mvc 4 Autofac依赖项解析程序在解析服务时为参数传递相同的值
我正在ASP.NETMVC4应用程序中使用Autofac for IoC 我无法理解为什么依赖项解析程序在解析依赖项时为不同的参数传递相同的值 以下是我的注册方式:Asp.net mvc 4 Autofac依赖项解析程序在解析服务时为参数传递相同的值,asp.net-mvc-4,dependency-injection,inversion-of-control,autofac,dependency-resolver,Asp.net Mvc 4,Dependency Injection,Inversion Of Control,Autofac,Dependency Resolver,我正在ASP.NETMVC4应用程序中使用Autofac for IoC 我无法理解为什么依赖项解析程序在解析依赖项时为不同的参数传递相同的值 以下是我的注册方式: private void RegisterDependencyResolver() { var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.R
private void RegisterDependencyResolver()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.Register(x => new AESCryptographyService()).As<ICryptographyService>();
builder.RegisterType<AppContext>().As<IContext>();
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
这是我的
AppContext
:
var factory = _dependencyResolver.GetService<Func<string, string, string, IContext>>();
IContext context = factory(contextToken, hostUrl, request.Url.Authority);
internal class AppContext : IContext
{
public AppContext(string contextToken, string hostUrl, string appUrl)
{
AppUrl = appUrl;
HostUrl = hostUrl;
ContextToken = contextToken;
}
public string AppUrl { get; private set; }
public string ContextToken { get; private set; }
public string HostUrl { get; private set; }
}
请看一下这个截图。尽管
contextToken
、hostUrl
和request.Url.Authority
具有不同的值,但在AppContext
的构造函数中,所有值都设置为contextToken
的值
想明白了 我必须替换这个:
var factory = _dependencyResolver.GetService<Func<string, string, string, IContext>>();
IContext context = factory(contextToken, hostUrl, request.Url.Authority);
var factory=\u dependencyrolver.GetService();
IContext context=factory(contextToken、hostUrl、request.Url.Authority);
为此:
var context =
_dependencyResolver.RequestLifetimeScope.Resolve<IContext>(
new NamedParameter("contextToken", contextToken),
new NamedParameter("hostUrl", hostUrl),
new NamedParameter("appUrl", request.Url.Authority));
var上下文=
_dependencyResolver.RequestLifetimeScope.Resolve(
新命名参数(“contextToken”,contextToken),
新名称参数(“hostUrl”,hostUrl),
新的NamedParameter(“appUrl”,request.Url.Authority));
算出了
我必须替换这个:
var factory = _dependencyResolver.GetService<Func<string, string, string, IContext>>();
IContext context = factory(contextToken, hostUrl, request.Url.Authority);
var factory=\u dependencyrolver.GetService();
IContext context=factory(contextToken、hostUrl、request.Url.Authority);
为此:
var context =
_dependencyResolver.RequestLifetimeScope.Resolve<IContext>(
new NamedParameter("contextToken", contextToken),
new NamedParameter("hostUrl", hostUrl),
new NamedParameter("appUrl", request.Url.Authority));
var上下文=
_dependencyResolver.RequestLifetimeScope.Resolve(
新命名参数(“contextToken”,contextToken),
新名称参数(“hostUrl”,hostUrl),
新的NamedParameter(“appUrl”,request.Url.Authority));