1

ASP에서 IoC 용 Autofac을 사용하고 있습니다 .Net MVC 4 app.Autofac 종속성 해결 프로그램은 서비스를 확인할 때 매개 변수에 동일한 값을 전달합니다.

종속성을 확인할 때 종속성 분석기가 다른 매개 변수에 대해 동일한 값을 전달하는 이유를 알아낼 수 없습니다.

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)); 
} 
다음

나는 IContext를 해결하고 방법 : 여기

내가 등록하고 어떻게

var factory = _dependencyResolver.GetService<Func<string, string, string, IContext>>(); 
IContext context = factory(contextToken, hostUrl, request.Url.Authority); 
여기

은 내 AppContext :

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, hostUrlrequest.Url.Authority이 다른 값을 가지더라도 AppContext의 생성자에서 모든 값은 contextToken의 값으로 설정됩니다.


enter image description here

답변

1

그것을 알아 냈어! 이와

var factory = _dependencyResolver.GetService<Func<string, string, string, IContext>>(); 
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)); 

나는이 대체했다