I'm trying to write a VB.NET application to pull site information from Google Analytics. (Currently we get them emailed to us, but that will be going away once UA becomes deprecated in favor of GA4 starting 1 July.) Initially everything seemed to work fine, but it suddenly stopped working and started generating a giant error message that didn't seem very helpful to me.
The error occurs at the following bit of code:
Dim Request As New RunReportRequest
With Request
.Property = "properties/" & iProperty.PropertyID
.Dimensions.Add(New Dimension With {.Name = "date"})
For Each Field As String In iProperty.Fields
.Metrics.Add(New Metric With {.Name = Field})
Next
.DateRanges.Add(New DateRange With {.StartDate = StartDate, .EndDate = EndDate})
End With
'Fetch the data
**Dim Client As BetaAnalyticsDataClient = Await BetaAnalyticsDataClient.CreateAsync(CancellationToken.None)**
Dim Response = Await Client.RunReportAsync(Request)
The error I got was:
System.TypeInitializationException: The type initializer for 'Grpc.Core.Internal.UserAgentStringProvider' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Grpc.Core.Internal.PlatformApis' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'Xamarin.iOS' or one of its dependencies. An argument was out of its legal range. (Exception from HRESULT: 0x80131502) ---> System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length
at System.String.Substring(Int32 startIndex, Int32 length)
at GoogleAnalyticsMailer.My.MyApplication.MyResolveEventHandler(Object sender, ResolveEventArgs args) in C:\__CODE FOLDER\CURRENT DEVELOPMENT\GoogleAnalyticsMailer\ApplicationEvents.vb:line 42
at System.AppDomain.OnAssemblyResolveEvent(RuntimeAssembly assembly, String assemblyFullName)
--- End of inner exception stack trace ---
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
at System.Type.GetType(String typeName)
at Grpc.Core.Internal.PlatformApis..cctor() in /var/local/git/grpc/src/csharp/Grpc.Core/Internal/PlatformApis.cs:line 87
--- End of inner exception stack trace ---
at Grpc.Core.Internal.PlatformApis.get_FrameworkDescription()
at Grpc.Core.Internal.UserAgentStringProvider..cctor() in /var/local/git/grpc/src/csharp/Grpc.Core/Internal/UserAgentStringProvider.cs:line 34
--- End of inner exception stack trace ---
at Grpc.Core.Internal.UserAgentStringProvider.get_DefaultInstance()
at Grpc.Core.Channel.EnsureUserAgentChannelOption(Dictionary`2 options) in /var/local/git/grpc/src/csharp/Grpc.Core/Channel.cs:line 321
at Grpc.Core.Channel..ctor(String target, ChannelCredentials credentials, IEnumerable`1 options) in /var/local/git/grpc/src/csharp/Grpc.Core/Channel.cs:line 69
at lambda_method(Closure , String , ChannelCredentials , GrpcChannelOptions )
at Google.Api.Gax.Grpc.GrpcCoreAdapter.CreateChannelImpl(ServiceMetadata serviceMetadata, String endpoint, ChannelCredentials credentials, GrpcChannelOptions options) in /_/Google.Api.Gax.Grpc/GrpcCoreAdapter.cs:line 56
at Google.Api.Gax.Grpc.GrpcAdapter.CreateChannel(ServiceMetadata serviceMetadata, String endpoint, ChannelCredentials credentials, GrpcChannelOptions options) in /_/Google.Api.Gax.Grpc/GrpcAdapter.cs:line 84
at Google.Api.Gax.Grpc.ChannelPool.GetChannel(GrpcAdapter grpcAdapter, String endpoint, GrpcChannelOptions channelOptions, ChannelCredentials credentials) in /_/Google.Api.Gax.Grpc/ChannelPool.cs:line 99
at Google.Api.Gax.Grpc.ChannelPool.<GetChannelAsync>d__9.MoveNext() in /_/Google.Api.Gax.Grpc/ChannelPool.cs:line 94
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Api.Gax.Grpc.ClientBuilderBase`1.<CreateCallInvokerAsync>d__78.MoveNext() in /_/Google.Api.Gax.Grpc/ClientBuilderBase.cs:line 393
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Analytics.Data.V1Beta.BetaAnalyticsDataClientBuilder.<BuildAsyncImpl>d__10.MoveNext() in /_/apis/Google.Analytics.Data.V1Beta/Google.Analytics.Data.V1Beta/BetaAnalyticsDataClient.g.cs:line 194
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at GoogleAnalyticsMailer.frmMain.VB$StateMachine_13_SendAnalytics.MoveNext() in C:\__CODE FOLDER\CURRENT DEVELOPMENT\GoogleAnalyticsMailer\frmMain.vb:line 224
What I find interesting is that it mentions Xamarin.iOS initially, which I am absolutely not coding for. I'm sure the problem is deeper and that's just where the error happens to bubble up.
I have the GOOGLE_APPLICATION_CREDENTIALS environment variable set up and pointing to the appropriate JSON file. As I said before, this bit of code was working fine until suddenly it was not.
Thanks in advance for any help you can provide!
I solved the issue. I had a class (ApplicationEvents.vb
) that would redirect any missing DLL calls to look in the BIN subfolder. It was unable to find the Xamarin.iOS
file (because I don't require it) and my code was choking as a result. A simple try
/catch
fixed the issue.