以下是我的应用程序中的确切场景。
我已经使用 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/