gpt4 book ai didi

c# - autofac 不适用于 azure 网站上的 WebAPI 2

转载 作者:行者123 更新时间:2023-12-03 03:16:40 25 4
gpt4 key购买 nike

我正在尝试在 azure 上设置我的 Web api,一切都在本地工作文件,但是当我部署到 azure 时,我收到以下错误消息:

An error occurred when trying to create a controller of type 'DestinationController'. Make sure that the controller has a parameterless public constructor.

该网站是一个普通的 azure 网站,而不是工作规则。

这是我的 Autofac Bootstrap 类:

var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerRequest();
builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerRequest();
builder.RegisterAssemblyTypes(typeof(DestinationRepository).Assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces().InstancePerRequest();
builder.RegisterAssemblyTypes(typeof(DestinationService).Assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerRequest();

builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
IContainer container = builder.Build();

// Create the depenedency resolver.
var resolver = new AutofacWebApiDependencyResolver(container);

// Configure Web API with the dependency resolver.
GlobalConfiguration.Configuration.DependencyResolver = resolver;

还有我的 Controller :

public class DestinationController : ApiController
{
private readonly IDestinationService _destinationService;

public DestinationController(IDestinationService destinationService)
{
this._destinationService = destinationService;
}}

感谢您的帮助。

最佳答案

打开自定义错误消息后,我发现如果注入(inject)的依赖项之一抛出异常,您将收到上面的错误消息。但原始错误消息将位于内部异常中,您可以通过从 web.config 关闭自定义错误来看到该消息

关于c# - autofac 不适用于 azure 网站上的 WebAPI 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24490040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com