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

标签 c# servicestack inversion-of-control autofac ioc-container

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

我已经使用 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”方法。

关于c# - ResolutionException - 获取 "Required dependency of type *********** could not be resolved",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44048658/

相关文章:

java - 如何在 android 中创建这个嵌套的 JSON 对象并在 c# 中对其进行解码?

c# - 跨 AppDomain 实现引用计数的最佳方式是什么?

c# - 按类型访问 ServiceStack requestDto 对象

dependency-injection - 如何重构静态类以使用依赖注入(inject)?

c# - 在 ASP.NET MVC 3 中在哪里初始化 ObjectFactory?

c# - 查询 documentdb 中的子字段

c# - 在 ASP.NET Core 6 Program.cs 中配置 EF

c# - 在异常过滤器中获取 WebApi 异常响应

c# - ServiceStack 4 是否保留收到的 HTTP 请求的日志?

c# - 使用 IoC 将依赖项注入(inject)基类和子类?