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

标签 c# azure autofac asp.net-web-api2

我正在尝试在 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/

相关文章:

c# - 如何在运行时以编程方式创 build 置并在程序重新启动后加载这些设置。无需手动将它们写入设置文件 C#

java - 验证基于 Azure 表 SAS 的凭据

c# - 使用 Autofac 将对象注入(inject) MassTransit Saga

c# - 获取 Autofac 中接口(interface)的所有注册实现

c# - Autofac:如何在不绕过 IoC 容器的情况下限制 IDisposable 对象的生命周期

c# - 大于 10MB 的 Google 语音转文本文件

c# - 在 Windows 应用程序上验证代码签名证书

c# - MSBuild with Azure - PublishProfileSet 的计算结果为 ""

c# - 为什么 powershell 无法识别 Windows 服务调用的脚本中的 cmdlet?

azure - 如何在现有 Azure KeyVault 上启用软删除