googleapis / google-cloud-dotnet

Google Cloud Client Libraries for .NET
https://cloud.google.com/dotnet/docs/reference/
Apache License 2.0
941 stars 367 forks source link

Weird MissingMethodException with AddGoogleDiagnosticsForAspNetCore when Google.Cloud.PubSub.V1 is used #9360

Closed christopheblin closed 1 year ago

christopheblin commented 1 year ago

I'm trying to add <PackageReference Include="Google.Cloud.Diagnostics.AspNetCore" Version="4.4.0" /> and to enable it during startup of my asp.net core

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGoogleDiagnosticsForAspNetCore(new AspNetCoreTraceOptions(), new LoggingServiceOptions {  Options = LoggingOptions.Create(logLevel: LogLevel.Debug)});

I receive a very weird exception, which looks like there is a problem with the way nuget packages are resolved

Unhandled exception. System.MissingMethodException: Method not found: 'Void Google.Api.Gax.Grpc.ClientBuilderBase`1..ctor()'.
   at Google.Cloud.Trace.V1.TraceServiceClientBuilder..ctor()
   at Google.Cloud.Diagnostics.Common.TraceExtensions.AddGoogleTrace(IServiceCollection services, TraceServiceOptions options)
   at Google.Cloud.Diagnostics.AspNetCore.AspNetCoreTraceExtensions.AddGoogleTraceForAspNetCore(IServiceCollection services, Boolean registerMiddleware, AspNetCoreTraceOptions options)
   at Google.Cloud.Diagnostics.AspNetCore.AspNetCoreGoogleDiagnosticsExtensions.AddGoogleDiagnosticsForAspNetCore(IServiceCollection services, AspNetCoreTraceOptions traceOptions, LoggingServiceOptions loggingOptions, ErrorReportingServiceOptions errorReportingOptions)

My csproj looks like


   <ItemGroup>
       <PackageReference Include="Google.Cloud.Diagnostics.AspNetCore" Version="4.4.0" />
    <PackageReference Include="Google.Cloud.PubSub.V1" Version="3.2.0" />
    <PackageReference Include="Google.Cloud.Run.V2" Version="2.0.0-beta02" />
    <PackageReference Include="Nanoid" Version="2.1.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.11" />
  </ItemGroup>

If I remove <PackageReference Include="Google.Cloud.PubSub.V1" Version="3.2.0" />, it works (well the AddGoogleDiagnosticsForAspNetCore still fails but not with the weird exception)

=> could someone tell me how I am supposed to resolve the problem ?

To me, it looks like a conflict between

But I have no clue which version I can use ...

jskeet commented 1 year ago

The Google.Cloud.Diagnostics.AspNetCore package targets ASP.NET Core 2.1, which is no longer supported by Microsoft - and cannot be used with the most up-to-date client libraries. (The version mismatch is with Google.Api.Gax.Grpc, not actually Grpc.Core itself.) A final version was released in January 2022, but we do not expect to release a new version that targets the latest version of Google.Api.Gax.Grpc.

Please update to Google.Cloud.Diagnostics.AspNetCore3, which should work for ASP.NET Core 3.1, 6 and 7. (We'd have liked to stick to one package, but there were some incompatibilities in ASP.NET Core 3.1 which meant we needed a separate package.)

If you really want to stick with Google.Cloud.Diagnostics.AspNetCore, you could use Google.Cloud.PubSub.V1 version 2.10.0, which is the latest release that targets the same version of GAX. But I'd strongly advise you to update to the AspNetCore3 package instead.

christopheblin commented 1 year ago

@jskeet thank you very much for the timely and accurate answer 👍

this is explained in https://cloud.google.com/trace/docs/setup/aspnetcore, which I completely overlooked since I was directed to https://cloud.google.com/trace/docs/setup/aspnet with my google search "https://www.google.com/search?q=.net+gcp+trace" :)