ArxOne / MrAdvice

.NET aspect weaver (build task under NuGet package)
MIT License
306 stars 45 forks source link

Compilation error in vs2022 #201

Open hzzcmaj7 opened 6 months ago

hzzcmaj7 commented 6 months ago

hi, I have encountered a troublesome problem,I referenced MrAdvice through NuGet in the class library project, version 2.14.0. However, during compilation, I received the following error message:

Error MSB3073 command “dotnet "C:\Users\myusername\.nuget\packages\mradvice\2.14.0\build..\tools\net6.0/MrAdvice.Weaver.dll" AssemblyPath="obj\Debug\net6.0\myprojectname.dll" ReferencePath="@obj\Debug\net6.0\myprojectname.dll.MrAdvice.ReferencePath.txt" ReferenceCopyLocalPaths="@obj\Debug\net6.0\myprojectname.dll.MrAdvice.LocalReferencePath.txt" AssemblyOriginatorKeyFile="" SignAssembly="false"” exited,code is 2。 myprojectname C:\Users\myusername\.nuget\packages\mradvice\2.14.0\build\MrAdvice.targets 16

I deleted the MrAdvice. targets file in path "C:\Users\myusername\.nuget\packages\mradvice\2.14.0\build" and compiled it successfully. However, when running the code, it directly ran the business method and did not enter the Advise method.

[AttributeUsage(validOn: AttributeTargets.Method, AllowMultiple = true)] public class MyProudAdvice : Attribute, IMethodAsyncAdvice { public async Task Advise(MethodAsyncAdviceContext context) { // do things you want here context.Proceed(); // this calls the original method // do other things here } }

[MyProudAdvice] public void MyProudMethod() { }

I see a similar problem here,https://github.com/ArxOne/MrAdvice/issues/140 but my project is not a framework, I am using VS2022 and .NET6, I'm not sure if there is need for additional configuration.

Is there any solution? Thank you.

picrap commented 6 months ago

Could you upgrade to 2.15? There is an update to support more PDB (debug info) file formats.

hzzcmaj7 commented 6 months ago

hi picrap,

Thanks. The current solution compilation does not pass. How can I view valid information from the PDB, because there is an error in compilation and the project cannot be started.

I have tried upgrading to version 2.15.0, but the error message is the same. In addition, I have a new discovery that the solution has created many class library projects, each library project has already referenced MrAdvice, but only one of them has a compilation error. I tried to remove the reference of the error project to MrAdvice, and the compilation passed. However, I did not find any difference in this class library project. I also created a new class library project and copied the files from the previous project into the new project. The new project also compiled an error, I guess, may some code in this project have had an impact? I don't know where to start dealing with this issue.

picrap commented 6 months ago

Could you build in verbose mode and give me more details about the error?

hzzcmaj7 commented 6 months ago

hi picrap,

Thanks. I have found the following information and I am not sure if it is helpful for troubleshooting,

5>MrAdvice 2.15+c60086a4a967e33b58ed9c2dd7854a934aa67d8c/.NET 6.0 weaved module 'MyPorjectName.ServicesNew, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' (targeting framework .NET 6.0) in 451ms 5>Unhandled exception: dnlib.DotNet.Writer.ModuleWriterException: Expected a null constant. Error occurred after metadata event BeginWriteMethodBodies. 5> at dnlib.DotNet.DummyLogger.Log(Object sender, LoggerEvent loggerEvent, String format, Object[] args) 5> at dnlib.DotNet.Writer.ModuleWriterBase.dnlib.DotNet.ILogger.Log(Object sender, LoggerEvent loggerEvent, String format, Object[] args) 5> at dnlib.DotNet.Writer.Metadata.Error(String message, Object[] args) 5> at dnlib.DotNet.Writer.Metadata.dnlib.DotNet.Writer.IWriterError.Error(String message) 5> at dnlib.DotNet.Pdb.Portable.LocalConstantSigBlobWriter.Write(DataWriter writer, TypeSig type, Object value) 5> at dnlib.DotNet.Pdb.Portable.LocalConstantSigBlobWriter.Write(IWriterError helper, Metadata systemMetadata, DataWriter writer, TypeSig type, Object value) 5> at dnlib.DotNet.Writer.Metadata.AddLocalConstant(PdbConstant constant) 5> at dnlib.DotNet.Writer.Metadata.WriteMethodBodies() 5> at dnlib.DotNet.Writer.Metadata.Create() 5> at dnlib.DotNet.Writer.Metadata.CreateTables() 5> at dnlib.DotNet.Writer.ModuleWriter.WriteImpl() 5> at dnlib.DotNet.Writer.ModuleWriterBase.Write(Stream dest) 5> at dnlib.DotNet.Writer.ModuleWriterBase.Write(String fileName) 5> at dnlib.DotNet.ModuleDef.Write(String filename, ModuleWriterOptions options) 5> at StitcherBoy.Reflection.ModuleManager.WriteModule(ModuleWriterOptionsBase moduleWriterOptionsBase) 5> at StitcherBoy.Reflection.ModuleManager.Write(String assemblyOriginatorKeyFile) 5> at StitcherBoy.Weaving.Build.AssemblyStitcher.Process(IDictionary`2 parameters, String entryAssemblyPath) 5> at StitcherBoy.Stitcher.Run[TStitcher](String[] args)

picrap commented 6 months ago

Could disable advices injections and reenable them one by one until it breaks again? There may be a weaving error in a very specific context that we need to isolate.

hzzcmaj7 commented 6 months ago

hi picrap, Thanks, I think this is a great suggestion. I have located the code below and commented it out, then it can be compiled successfully. However, I don't understand why they have an impact. Also, do I need to implement the commented part of the code in a different way?

public List GetAllCacheKeys() { //const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; //var coherentState = _memoryCache.GetType().GetField("_coherentState", flags); //var entries = coherentState.GetType().GetField("_entries", flags).GetValue(coherentState); //var cacheItems = entries as IDictionary; var keys = new List(); //if (cacheItems == null) return keys; //foreach (DictionaryEntry cacheItem in cacheItems) //{ // keys.Add(cacheItem.Key.ToString()); //} return keys; }

picrap commented 6 months ago

What advices do you apply in this assembly? Is this at assembly level? Or type/method level? And in what chain is this code applied? In an advice or in some other code?

hzzcmaj7 commented 6 months ago

hi picrap, Thanks. I followed the steps below to reproduce this compilation error,

Create a new class library project, reference MrAdvice(2.15.0), create a class and method with the following line of code, compile it, and the issue is reproduced,

public class TestService { public void Test() { const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; } }

Then I removed the const keyword and the compilation was successful. I think it's because of const. This is code written by someone else, and I don't know what their intention is. Also, I would like to know why const affects the compilation process of MrAdvice. Additionally, I found that this error only occurs when the const keyword is used internally within a method, but declaring a const constant at the class level does not result in this error.

Thank you very much.

picrap commented 6 months ago

Nice! I’ll look at it.