MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.29k stars 327 forks source link

Mapster.Tool issue with 'Microsoft.Extensions.Primitives' v7.0.0 #582

Closed bhavens17 closed 1 year ago

bhavens17 commented 1 year ago

Love what I've seen so far, but I've found that the Mapster.Tool is throwing an error in projects with a unique set of criteria. I've stripped everything away from the project and it seems to boil down to these items being in place:

  1. ASP .Net Core MVC project (.Net 6.0)
  2. Mapster v7.3
  3. Microsoft.Extensions.Primitives v7.0
  4. Controller with async method
  5. Accessing indexed value from HttpContext.Request.Form directly ('HttpContext.Request.Form["foo"]' without any sort of 'ToString()', 'ToList()', etc modifier)

Given all of these criteria, I get an error message when building the project and trigger a Mapster.Tool rebuild:

1>MapsterError -> C:\Users...\source\repos\MapsterError\bin\Debug\net6.0\MapsterError.dll 1>Tool 'mapster.tool' (version '8.3.0') was restored. Available commands: dotnet-mapster 1> 1>Restore was successful. 1>Cannot find assembly path: Microsoft.Extensions.Primitives (type=package, version=7.0.0) 1>exception: Could not load file or assembly 'Microsoft.Extensions.Primitives, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. 1>Unhandled exception. System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. 1>Could not load file or assembly 'Microsoft.Extensions.Primitives, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. 1> at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) 1> at System.Reflection.Assembly.GetTypes() 1> at Mapster.Tool.Extensions.Scan(CodeGenerationConfig config, Assembly assembly) in C:\Projects\Mapster\src\Mapster.Tool\Extensions.cs:line 177 1> at Mapster.Tool.Program.GenerateModels(ModelOptions opt) in C:\Projects\Mapster\src\Mapster.Tool\Program.cs:line 146 1> at CommandLine.ParserResultExtensions.WithParsed[T](ParserResult1 result, Action1 action) 1> at Mapster.Tool.Program.Main(String[] args) in C:\Projects\Mapster\src\Mapster.Tool\Program.cs:line 17 1>System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.Primitives, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. 1>File name: 'Microsoft.Extensions.Primitives, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' 1>C:\Users...\source\repos\MapsterError\MapsterError.csproj(29,5): error MSB3073: The command "dotnet mapster model -a "C:\Users...\source\repos\MapsterError\bin\Debug\net6.0\MapsterError.dll"" exited with code -532462766.

Trying different things, some resolve the issue:

  1. Using an earlier version of Microsoft.Extensions.Primitives (e.g. v6.0) works
  2. Changing the controller method to non-async works
  3. Removing the reference to the HttpContext.Request.Form[] works
  4. Adding some kind of modified to the HttpContext.Request.Form[] command (e.g. '.ToString()', '.ToList()') works

I've attached a sample project for this. Thanks!

Mapster.Tool Error.zip

stagep commented 1 year ago

I was able to compile your sample project using a pre-release version of Mapster.Tool.

At the Developer Command Prompt:

dotnet tool uninstall Mapster.Tool dotnet tool install Mapster.Tool --version 8.4.0-pre05

However, without any mapping definitions defined in your sample project, this is just avoiding the compilation error and not showing that the tooling is working. It will probably be necessary to also update Mapster to pre-release version 7.4.0-pre05.

andrerav commented 1 year ago

@bhavens17 Let me know if you are still having issues after trying the pre-release version.

kescherCode commented 1 year ago

I am currently running into this on 7.4.0-pre05 and 7.4.0-pre06.

kescherCode commented 1 year ago

Adding

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

to the affected project currently remedies the issue. However, I would consider this to merely be a workaround, not a fix, since a lot of (unneeded) DLLs are copied to the build output.

andrerav commented 1 year ago

@kescherCode While I can see this from your point of view, you might want to consider refactoring your code to minimize the amount of third party dependencies from the classes you are trying to map. Consider using partial classes or extension methods in separate projects to keep your classes as clean as possible.

kescherCode commented 1 year ago

@andrerav Mapster.Tool is only used to generate Dtos for our own entities that are actually stored separately - and they are partial, too. When using Primitives v6.x, this doesn't happen, as OP stated.