[英]C# library which references older version of nuget dependency causes assembly reflection to fail
One of the nuget dependencies in my project ( Swashbuckle ) requires a version of the System.Web.Http
library (4.0.0.0) that is older than the version required by the rest of the project (5.2.3.0). 我的项目中的一个nuget依赖项( Swashbuckle )需要一个版本的System.Web.Http
库(4.0.0.0),它比项目其余部分(5.2.3.0)所需的版本旧。
Swashbuckle requires that I write a class implementing a certain interface: Swashbuckle要求我编写一个实现某个接口的类:
public class OperationFilter : Swashbuckle.Swagger.IOperationFilter
{
public void Apply(Swashbuckle.Swagger.Operation operation, Swashbuckle.Swagger.SchemaRegistry schemaRegistry, System.Web.Http.Description.ApiDescription apiDescription)
{
}
}
The important part above is the apiDescription
parameter of Apply
. 上面的重要部分是Apply
的apiDescription
参数。
When building the project normally, the above compiles and runs fine. 正常构建项目时,上面编译并运行正常。 However, when I reflect over the running assembly using assembly.GetTypes()
, 但是,当我使用assembly.GetTypes()
反映正在运行的程序assembly.GetTypes()
,
var asm = System.Reflection.Assembly.GetExecutingAssembly();
var types = asm.GetTypes()
a ReflectionTypeLoadException
is thrown, with the following loader exception details: 抛出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 references the above exception, but none of the solutions posed seem to work. 这个问题引用了上述例外情况,但没有一个解决方案似乎有效。 I tried to solve the issue by adding a bindingRedirect to Web.config: 我尝试通过向Web.config添加bindingRedirect来解决此问题:
<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>
However, that didn't seem to do anything. 但是,这似乎没有做任何事情。
How can I get this type to load properly? 如何才能正确加载此类型?
EDIT: I've created a minimal reproduction of the issue . 编辑:我已经创建了一个最小的问题再现 。 A build task in BuildTask.targets
loads the project assembly and then tries to load all the types. BuildTask.targets
的构建任务加载项目程序集,然后尝试加载所有类型。 The errors are thrown and displayed. 抛出并显示错误。
Following up on my comments... 跟进我的评论......
I just tested the sample project and this issue can be fixed by using Swagger-Net . 我刚刚测试了示例项目,可以使用Swagger-Net修复此问题。
That is my fork of Swashbuckle, I've upgraded all dependencies to the latest and using also latest of the Swagger-UI (3.x), it is quite a drastic change from the old one, let me know how you like it. 这是我的Swashbuckle的分支,我已将所有依赖项升级到最新版本并使用最新的Swagger-UI(3.x),它与旧版本相比发生了巨大变化,让我知道你喜欢它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.