【问题标题】:Azure runtime error from Swagger and CastleWindsor来自 Swagger 和 CastleWindsor 的 Azure 运行时错误
【发布时间】:2018-06-24 21:13:14
【问题描述】:

我在部署应用程序时遇到问题。我会尽力提供尽可能多的信息。

该 api 项目通过 Swashbuckle 和 Castle Windsor 使用了 Swagger。

当 Swagger 在 SwaggerConfig.cs 中通过 [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] 我们得到以下异常: Method not found: 'Void Swashbuckle.Application.SwaggerDocsConfig.RootUrl(System.Func`2<System.Net.Http.HttpRequestMessage,System.String>)'.

这是完整的注册电话,以防相关

  GlobalConfiguration.Configuration
        .EnableSwagger(c =>
            {
                c.RootUrl(req => req.RequestUri.GetLeftPart(UriPartial.Authority) + "/api");
                c.MultipleApiVersions((api, v) => true,
                vc =>
                {
                    vc.Version("mobile", "Mobile");
                    vc.Version("web", "Web");
                });
                c.UseFullTypeNameInSchemaIds();
                c.DescribeAllEnumsAsStrings();
                c.DocumentFilter<VersionFilter>();
            })
        .EnableSwaggerUi(c =>
            {
                c.EnableDiscoveryUrlSelector();
            });

如果我们禁用 swagger(通过删除程序集属性),则应用程序在 Castle Windsor 启动时开始失败,并出现以下错误:

Method 'Create' in Type 'XXX.XXX.XXX.CastleWindsor.WindsorCompositionRoot' from assembly 'xxx.xxx.Backend, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.'

这是 WindsorConfig.cs 的 Configure() 方法

public static void Configure()
    {
        WindsorConfig.Container = new WindsorContainer();
        WindsorConfig.Container.Install(FromAssembly.This());

        System.Web.Http.GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new WindsorCompositionRoot(WindsorConfig.Container));
    }

当我们将解决方案中的共享库从 PCL 升级到 .net 标准 2 时,出现了问题。这反过来意味着我们必须将解决方案中的所有其他项目升级到 .net 4.7.1(解决方案中有一个 UWP 应用程序,这是我们最初的升级目标,这意味着我们必须升级库,但我离题了) ,总而言之,这个问题发生在 4.7.1 升级上。

应用程序通过带有 msbuild 和 msdeploy 的 Bamboo 部署到 Azure。我们确信二进制文件和 web.config 与开发同步,因为我们已经通过 FTP 手动部署它们以确保它们。 我在重新上传之前通过 FTP 删除了所有二进制文件,以确保没有遗留程序集。

我已验证 Kudu 中的 Azure 应用程序显示正确的版本以支持 4.7.1,如下所述:https://github.com/Azure/app-service-announcements/issues/63

未运行的应用部署到 Azure 中应用服务的虚拟目录 (/api)。主网站(在 / 上)是静态的,使用 Angular 访问 API。

我已确认 / 和 /api 项目中的 web.config 正在显示 <compilation targetFramework="4.7.1" /> <httpRuntime targetFramework="4.7.1" />

包版本如下(我们在目标框架升级后进行了nuget重新安装): <package id="Castle.Core" version="3.3.0" targetFramework="net471" /> <package id="Castle.Windsor" version="3.3.0" targetFramework="net471" /> <package id="Castle.Windsor.Lifestyles" version="0.4.0" targetFramework="net471" /> <package id="Swashbuckle" version="5.3.2" targetFramework="net471" /> <package id="Swashbuckle.Core" version="5.3.2" targetFramework="net471" />

当我尝试升级到 4.7.1(对于另一个依赖项)时,我之前在开发环境中遇到过这个问题,但这次开发工作没有错误,只是现在部署到 Azure 时才失败。 此应用服务的 azure 门户中的 .net 框架版本显示 4.7

现在,我认为问题实际上不是我们用来初始化 Swagger 和/或 Castle Windsor 的代码,它更可能与框架版本或不匹配的程序集版本有关,因为该应用程序在我的开发机器没有错误,但我已经没有下一步要尝试的想法了。

【问题讨论】:

    标签: c# .net azure target-framework


    【解决方案1】:

    我通过搜索 Windsor 错误发现了这个问题https://forums.asp.net/t/2137242.aspxSystem.Web.Http.Dispatcher.IHttpControllerActivator 未能实例化错误Method 'Create' in type 'MyType' from assembly 'MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.

    这导致我检查已部署的System.Net.Http 版本。 System.Net.Http 在本地是 4.2.0.0,并且此 DLL 已部署到 Azure,但看起来运行时实际上正在使用不同的版本。

    奇怪的是,当我从 bin 文件夹中删除 System.Net.Http 的部署版本时,错误说:'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 所以这是 Azure 提供的程序集版本,无论 bin 中的版本如何文件夹(也是 4.2.0.0)。

    我为 System.Net.Http 添加了绑定重定向,应用程序现在可以正常启动

    <dependentAssembly> <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0"/> </dependentAssembly>

    【讨论】:

      猜你喜欢
      • 2023-01-19
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      相关资源
      最近更新 更多