MapsterMapper / Mapster

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

Mappings cant be generated if one of mapped entity have external dependency. #595

Closed Fennixx closed 1 year ago

Fennixx commented 1 year ago

Hello, I am facing issue that mapper cant be generated when at least one of the mapped entities have any using, so any external dependency. We are corrently using Mapster:7.4.0-pre06 with MapsterTools:8.4.0-pre.6 due to .NET7 need. We have all the project in .NET7. Corrently we found one workaround and that is that we need to manually copy all the required dependencies to bin folder. Then it works.

We have following error for each dependency: (replace <myDependency> with any dependency you can imagine)

  Tool 'mapster.tool' (version '8.4.0-pre06') was restored. Available commands: dotnet-mapster

  Restore was successful.
  Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly '<myDependency>, Version=2.0.1.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

  File name: '<myDependency>, Version=2.0.1.0, Culture=neutral, PublicKeyToken=null'
     at System.ModuleHandle.ResolveType(QCallModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
     at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
     at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
     at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctorWithParameters, Boolean& isVarArg)
     at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
     at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
     at System.Attribute.GetCustomAttributes(MemberInfo element, Type attributeType, Boolean inherit)
     at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
     at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo element)
     at Mapster.Tool.Program.GenerateMappers(MapperOptions opt) in C:\Projects\Mapster\src\Mapster.Tool\Program.cs:line 71
     at CommandLine.ParserResultExtensions.WithParsed[T](ParserResult`1 result, Action`1 action)
     at Mapster.Tool.Program.Main(String[] args) in C:\Projects\Mapster\src\Mapster.Tool\Program.cs:line 18
jvmlet commented 1 year ago

What kind of project is this ? Class library or console application?

Fennixx commented 1 year ago

What kind of project is this ? Class library or console application?

Hello, it is class library.

jvmlet commented 1 year ago

Try to add msbuild property that copies all dependencies to output folder :

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

Fennixx commented 1 year ago

Thanks, yes I confirm, that fix my issue.

jvmlet commented 1 year ago

I'm not sure this is what you want to do... You MIGHT have it as -p property when building assembly to generate the mapping BEFORE building your project, but definitely NOT as property in your main project.

Fennixx commented 1 year ago

So how should this config looks like? We are using it now with this configuration:

<Target Name="Mapster">
    <Exec WorkingDirectory="$(ProjectDir)" Command="dotnet build" />
    <Exec WorkingDirectory="$(ProjectDir)" Command="dotnet tool restore" />
    <Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster model -a &quot;$(TargetDir)$(ProjectName).dll&quot;" />
    <Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster extension -a &quot;$(TargetDir)$(ProjectName).dll&quot;" />
    <Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster mapper -a &quot;$(TargetDir)$(ProjectName).dll&quot;" />
</Target>
jvmlet commented 1 year ago

Something like this I think :

 <Exec WorkingDirectory="$(ProjectDir)" Command="dotnet build -p:CopyLocalLockFileAssemblies=true" />