Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

Swashbuckle requires that I write a class implementing a certain interface:

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.

When building the project normally, the above compiles and runs fine. However, when I reflect over the running assembly using assembly.GetTypes(),

var asm = System.Reflection.Assembly.GetExecutingAssembly();
var types = asm.GetTypes()

a ReflectionTypeLoadException is thrown, with the following loader exception details:

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:

        <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. The errors are thrown and displayed.

like image 381
Daniel Avatar asked Oct 25 '17 05:10

Daniel


Video Answer


1 Answers

Following up on my comments...

I just tested the sample project and this issue can be fixed by using 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.

like image 144
Helder Sepulveda Avatar answered Oct 14 '22 04:10

Helder Sepulveda