ResolutionException - 获取 "Required dependency of type *********** could not be resolved"

ResolutionException - Getting "Required dependency of type *********** could not be resolved"

以下是我的应用程序中的确切场景。

我已经使用ServiceStack 3.9.48和AutoFac 4.6.0开发了REST服务。

以下是继承自AppHostBase的AppHost的代码

    public AppHost()
        :base("My Service Host", typeof(NotificationService).Assembly)
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<ConfigurationProvider>().As<IConfigurationProvider>();
        builder.RegisterType<Logging>().As<ILogging>();

        IoCContainer = builder.Build();            
    }

    public override void Configure(Container container)
    {
        using (var scope = IoCContainer.BeginLifetimeScope())
        {
            var _logging = scope.Resolve<ILogging>();

            JsConfig.EmitCamelCaseNames = true;

            base.RequestFilters.Add(delegate (IHttpRequest req, IHttpResponse res, object dto)
            {
                HandleUncaughtExceptionDelegate uncaughtExceptionDelegate = null;
                if (DateTime.Now.Year <= 2019)
                {
                    if (uncaughtExceptionDelegate == null)
                    {
                        uncaughtExceptionDelegate = delegate (IHttpRequest request, IHttpResponse response, string operationName, Exception ex)
                        {
                            res.StatusCode = 0x191;
                            res.Write("Error: This service is unavailable till 2019: " + operationName);
                        };
                    }
                    base.ExceptionHandler = uncaughtExceptionDelegate;
                    HttpResponse originalResponse = res.OriginalResponse as HttpResponse;
                    originalResponse.SuppressFormsAuthenticationRedirect = false;
                    res.End();
                }
            });

            base.ServiceExceptionHandler = delegate (object request, Exception exception)
            {
                _logging.Log(exception);
                return DtoUtils.HandleException(this, request, exception);
            };
        }
    }

我可以看到这段代码运行良好,如果不满足条件,则会记录异常。

但是,当我尝试调用调用以下内容的 API 端点时出现问题:

    public class NotificationService: Service
{
    private IConfigurationProvider _configurationProvider;
    public NotificationService(IConfigurationProvider _configurationProvider)
    {
        _configurationProvider = configurationProvider;
    }

    public object Post(SendEventNotification request)
    {
        return new SendEventNotificationResponse { SentStatus = SendNotification(_configurationProvider.GetValue("EncId")) };
    }
}

它给我一个错误说 -

Required dependency of type IConfigurationProvider could not be resolved.

任何人都可以建议这是什么原因吗?我相信在 AppHost 期间初始化的实例没有被保留。

我确定,缺少一些东西,但无法弄清楚。

如有任何帮助,我们将不胜感激。

感谢和问候,

尼尔曼

我发现它只是 ServiceStack 的问题。没有必要使用 Autofac,因为 Servicestack 本身提供了 DI 解析。另外,我不得不使用 ServiceStack 的 Container 对象的 "RegisterAutoWiredAs" 方法。