引用旧版本 nuget 依赖项的 C# 库导致程序集反射失败
C# library which references older version of nuget dependency causes assembly reflection to fail
我项目中的一个 nuget 依赖项 (Swashbuckle) 需要 System.Web.Http
库 (4.0.0.0) 的版本早于项目其余部分所需的版本 ( 5.2.3.0).
Swashbuckle 要求我写一个 class 实现某个接口:
public class OperationFilter : Swashbuckle.Swagger.IOperationFilter
{
public void Apply(Swashbuckle.Swagger.Operation operation, Swashbuckle.Swagger.SchemaRegistry schemaRegistry, System.Web.Http.Description.ApiDescription apiDescription)
{
}
}
上面重要的部分是Apply
的apiDescription
参数。
正常构建项目时,上面编译运行正常。但是,当我使用 assembly.GetTypes()
、
反思 运行 程序集时
var asm = System.Reflection.Assembly.GetExecutingAssembly();
var types = asm.GetTypes()
a ReflectionTypeLoadException
被抛出,具有以下加载程序异常详细信息:
Method 'Apply' in type 'OperationFilter' from assembly 'MyAssembly,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an
implementation.
this question 引用了上述异常,但 none 提出的解决方案似乎有效。
我试图通过将 bindingRedirect 添加到 Web.config:
来解决问题
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
然而,这似乎没有任何作用。
如何才能正确加载此类型?
编辑:我创建了 a minimal reproduction of the issue。 BuildTask.targets
中的构建任务加载项目程序集,然后尝试加载所有类型。抛出并显示错误。
正在跟进我的评论...
我刚刚测试了示例项目,可以使用 Swagger-Net 解决此问题。
那是我的 Swashbuckle 分支,我已将所有依赖项升级到最新版本并使用最新的 Swagger-UI (3.x),这与老哥,说说你的喜好吧。
我项目中的一个 nuget 依赖项 (Swashbuckle) 需要 System.Web.Http
库 (4.0.0.0) 的版本早于项目其余部分所需的版本 ( 5.2.3.0).
Swashbuckle 要求我写一个 class 实现某个接口:
public class OperationFilter : Swashbuckle.Swagger.IOperationFilter
{
public void Apply(Swashbuckle.Swagger.Operation operation, Swashbuckle.Swagger.SchemaRegistry schemaRegistry, System.Web.Http.Description.ApiDescription apiDescription)
{
}
}
上面重要的部分是Apply
的apiDescription
参数。
正常构建项目时,上面编译运行正常。但是,当我使用 assembly.GetTypes()
、
var asm = System.Reflection.Assembly.GetExecutingAssembly();
var types = asm.GetTypes()
a ReflectionTypeLoadException
被抛出,具有以下加载程序异常详细信息:
Method 'Apply' in type 'OperationFilter' from assembly 'MyAssembly,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an
implementation.
this question 引用了上述异常,但 none 提出的解决方案似乎有效。 我试图通过将 bindingRedirect 添加到 Web.config:
来解决问题 <dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
然而,这似乎没有任何作用。
如何才能正确加载此类型?
编辑:我创建了 a minimal reproduction of the issue。 BuildTask.targets
中的构建任务加载项目程序集,然后尝试加载所有类型。抛出并显示错误。
正在跟进我的评论...
我刚刚测试了示例项目,可以使用 Swagger-Net 解决此问题。
那是我的 Swashbuckle 分支,我已将所有依赖项升级到最新版本并使用最新的 Swagger-UI (3.x),这与老哥,说说你的喜好吧。