postsharp / PostSharp.IL

PostSharp.IL is an aspect-oriented framework based on IL rewriting. Metalama is a Roslyn-based meta-programming framework. Use this repo to report bugs or ask questions.
2 stars 1 forks source link

PostSharp No Longer Works with Blazor Web Assembly in .NET 8 #6

Closed PaulSorauer-IPG closed 6 months ago

PaulSorauer-IPG commented 6 months ago

Hi there

I've created an issue in the Samples repo as I feel the issue is better demonstrated there.

However, it seems that PostSharp no longer works in Blazor after the Web Assembly app can been upgraded to .NET 8.

Here's the issue I posted in the Samples repo:

PostSharp No Longer Works with Blazor Web Assembly in .NET 8

addabis commented 6 months ago

I can confirm that there is some problem with MSBuild integration that was not captured by our automated tests.

We will be working on a fix.

(internal id of this ticket is #34771)

addabis commented 6 months ago

Note that I have been able to update our sample repo to work on .NET 8.0. If any problem persists in your application, it means that it is a different problem than we've encountered in samples and we should continue the diagnosis.

PaulSorauer-IPG commented 6 months ago

Hi @addabis

I've carried on the conversation in the other thread, but I am indeed still having issues although I do feel it may still be the same underlying problem.

addabis commented 6 months ago

What I've done is to take an exact copy of the "AutoRetryAttribute" class from your example, pasted it into my solution and applied the attribute to a service where I'm forcing an exception. I've done many full Visual Studio solution cleans and rebuilds, and quite often clear my entire browser cache, not just the cached storage as per the thread you mentioned.

It's still possible that PostSharp still does not integrate properly with MSBuild.

I've run in both DEBGUG and RELEASE modes. and simply cannot get the aspect to apply.

I know you've suggested that there may be a different issue at play, but I'm not sure what that could be - my understanding of AOP is quite limited. It definitely presents like the same issue when I couldn't get your sample working.

First, if you have multiple projects, you should check that all projects use PostSharp's build assets from the NuGet package. This is because by default NuGet does not transitively propagate "build" assets to referencing projects, causing PostSharp not to run.

Therefore, you need either have a reference to PostSharp package in every project or use the following form of PackageReference in the project that contains the reference <PackageReference Include="PostSharp" Version="2024.05" PrivateAssets="none" />.

Second, if you should verify that PostSharp transforms the assembly. Define something quite simple that would force PostSharp to fail:

[PSerializable]
public struct S 
{ 
    private readonly int X = 42;
    public S() { } 
}

PostSharp cannot generate serializer for the above type and should fail with error error SR0004: Field 'BlazorApp2.S.X' must not be read-only because its declaring type is [PSerializable]..

Third, you should check your aspect usage. Primarily, aspects are applied using attributes like [AutoRetry] in the sample. If you are attempting some more complex way (programmatic/set inclusion/exclusion), please first verify that applying aspects using attributes on the target declaration is working.

PaulSorauer-IPG commented 6 months ago

Great! It works! 😍

Thanks again @addabis for such a speedy response.

As per the other thread, I did perform a clean at the repo / git level instead of just Visual Studio, I do note that I never had to switch .NET versions in my repo like we did for your Samples repo - I was already at .NET 8.0.x and simply added PostSharp via NuGet to the existing solution.

I think the issue was that I was lacking the PrivateAssets="none" in the PackageReference, but it was also really helpful to know how to make the build break by creating something that PostSharp couldn't build as a way of testing that PostSharp is indeed intercepting the build process.

Thanks for your help on this! I really appreciate it.

Cheers, Paul