Google api更改,导致MONO错误:System.Net.Http.HttpClientHandler:无效IL
本文关键字:Net System Http HttpClientHandler IL 无效 错误 更改 api 导致 MONO | 更新日期: 2023-09-27 18:03:28
在旧的验证系统失效后,我不得不更改Google Analytics API背后的验证系统。作为一个SO帖子的结果,我的认证文件从:
protected override void Load(ContainerBuilder builder)
{
builder.Register(c =>
{
var service = new AnalyticsService("mrfreeman.com");
service.setUserCredentials("robot@mrfreeman.com", "jellyfish");
return service;
});
builder.RegisterType<AnalyticsNewsletterTrackingSource>().As<INewsletterTrackingSource>();
builder.RegisterType<AnalyticsSearchTrackingSource>().As<ISearchTrackingSource>();
builder.RegisterType<AnalyticsEmailLinkTracker>().As<IEmailLinkTracker>();
}
:
private const string ServiceAccountEmail = "blabla@developer.gserviceaccount.com";
private static readonly string KeyPath = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath ?? AppDomain.CurrentDomain.BaseDirectory, "Key.p12");
private static readonly X509Certificate2 Certificate = new X509Certificate2(KeyPath, "notasecret", X509KeyStorageFlags.Exportable);
private readonly ServiceAccountCredential _credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(ServiceAccountEmail)
{
Scopes = new[] { "https://www.googleapis.com/auth/analytics" }
}.FromCertificate(Certificate));
protected override void Load(ContainerBuilder builder)
{
builder.Register(c =>
{
_credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Wait();
var service = new AnalyticsService("mrfreeman.com");
var requestFactory = new GDataRequestFactory("shazbot2.0");
requestFactory.CustomHeaders.Add(string.Format("Authorization: Bearer {0}", _credential.Token.AccessToken));
service.RequestFactory = requestFactory;
return service;
});
builder.RegisterType<AnalyticsNewsletterTrackingSource>().As<INewsletterTrackingSource>();
builder.RegisterType<AnalyticsSearchTrackingSource>().As<ISearchTrackingSource>();
builder.RegisterType<AnalyticsEmailLinkTracker>().As<IEmailLinkTracker>();
}
将生成的开发人员电子邮件地址和P12密钥复制到项目中。
问题是这样的:它可以在Windows上工作,但是在mono上运行的服务会出现问题,并且依赖于Analytics模块。当前使用Mono 4.0.2版本。
我试过了:
- 使用mono - 3.10和4.2.0预发行版的旧版本和新版本来运行服务(它本身是由Visual Studio构建的)并检查是否产生异常。
- 导入System.Net.Http并复制到本地(当前未在服务中引用)-仍然产生相同的异常
在这个阶段,我被迫学习新的Google Analytics API,并从头开始重写所有内容,因为我试图使用旧的API,但几乎没有运气更新Auth API,但感觉这是一个钝工具类型的解决方案。
堆栈跟踪在任何版本上都是相同的,并且是:
FATAL Unhandled exception occurred.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidProgramException: Invalid IL code in System.Net.Http.HttpClientHandler:.ctor (): method body is empty.
at Google.Apis.Http.HttpClientFactory.CreateHandler (Google.Apis.Http.CreateHttpClientArgs args) <0x40280100 + 0x0003b> in <filename unknown>:0
at Google.Apis.Http.HttpClientFactory.CreateHttpClient (Google.Apis.Http.CreateHttpClientArgs args) <0x4027ff90 + 0x0002d> in <filename unknown>:0
at Google.Apis.Auth.OAuth2.ServiceAccountCredential..ctor (Google.Apis.Auth.OAuth2.Initializer initializer) <0x4027f6d0 + 0x00438> in <filename unknown>:0
at jellyfish.Analytics.AnalyticsModule..ctor () <0x4027ed00 + 0x00103> in <filename unknown>:0
at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) <0x7f8257b94780 + 0x0003f> in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) <0x7f8257b94780 + 0x00079> in <filename unknown>:0
at System.RuntimeType.CreateInstanceMono (Boolean nonPublic) <0x7f82579c1ec0 + 0x00116> in <filename unknown>:0
at System.RuntimeType.CreateInstanceSlow (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, System.Threading.StackCrawlMark& stackMark) <0x7f82579c1e70 + 0x00049> in <filename unknown>:0
at System.RuntimeType.CreateInstanceDefaultCtor (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, System.Threading.StackCrawlMark& stackMark) <0x7f82579c1c10 + 0x0005c> in <filename unknown>:0
at System.Activator.CreateInstance[T] () <0x7f82578dfb70 + 0x0008a> in <filename unknown>:0
at Autofac.ModuleRegistrationExtensions.RegisterModule[TModule] (IModuleRegistrar registrar) <0x40273470 + 0x0004f> in <filename unknown>:0
at Autofac.ModuleRegistrationExtensions.RegisterModule[TModule] (Autofac.ContainerBuilder builder) <0x40273330 + 0x0006b> in <filename unknown>:0
at jellyfish.Cron.Program.BuildContainer (ILog log) <0x402730e0 + 0x0005f> in <filename unknown>:0
at jellyfish.Cron.Program.ExecuteJob (System.Type jobType) <0x4021abe0 + 0x0007b> in <filename unknown>:0
Unknown errno: Protocol error
Unhandled Exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidProgramException: Invalid IL code in System.Net.Http.HttpClientHandler:.ctor (): method body is empty.
at Google.Apis.Http.HttpClientFactory.CreateHandler (Google.Apis.Http.CreateHttpClientArgs args) <0x40280100 + 0x0003b> in <filename unknown>:0
at Google.Apis.Http.HttpClientFactory.CreateHttpClient (Google.Apis.Http.CreateHttpClientArgs args) <0x4027ff90 + 0x0002d> in <filename unknown>:0
at Google.Apis.Auth.OAuth2.ServiceAccountCredential..ctor (Google.Apis.Auth.OAuth2.Initializer initializer) <0x4027f6d0 + 0x00438> in <filename unknown>:0
at jellyfish.Analytics.AnalyticsModule..ctor () <0x4027ed00 + 0x00103> in <filename unknown>:0
at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) <0x7f8257b94780 + 0x0003f> in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) <0x7f8257b94780 + 0x00079> in <filename unknown>:0
at System.RuntimeType.CreateInstanceMono (Boolean nonPublic) <0x7f82579c1ec0 + 0x00116> in <filename unknown>:0
at System.RuntimeType.CreateInstanceSlow (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, System.Threading.StackCrawlMark& stackMark) <0x7f82579c1e70 + 0x00049> in <filename unknown>:0
at System.RuntimeType.CreateInstanceDefaultCtor (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, System.Threading.StackCrawlMark& stackMark) <0x7f82579c1c10 + 0x0005c> in <filename unknown>:0
at System.Activator.CreateInstance[T] () <0x7f82578dfb70 + 0x0008a> in <filename unknown>:0
at Autofac.ModuleRegistrationExtensions.RegisterModule[TModule] (IModuleRegistrar registrar) <0x40273470 + 0x0004f> in <filename unknown>:0
at Autofac.ModuleRegistrationExtensions.RegisterModule[TModule] (Autofac.ContainerBuilder builder) <0x40273330 + 0x0006b> in <filename unknown>:0
at jellyfish.Cron.Program.BuildContainer (ILog log) <0x402730e0 + 0x0005f> in <filename unknown>:0
at jellyfish.Cron.Program.ExecuteJob (System.Type jobType) <0x4021abe0 + 0x0007b> in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidProgramException: Invalid IL code in System.Net.Http.HttpClientHandler:.ctor (): method body is empty.
at Google.Apis.Http.HttpClientFactory.CreateHandler (Google.Apis.Http.CreateHttpClientArgs args) <0x40280100 + 0x0003b> in <filename unknown>:0
at Google.Apis.Http.HttpClientFactory.CreateHttpClient (Google.Apis.Http.CreateHttpClientArgs args) <0x4027ff90 + 0x0002d> in <filename unknown>:0
at Google.Apis.Auth.OAuth2.ServiceAccountCredential..ctor (Google.Apis.Auth.OAuth2.Initializer initializer) <0x4027f6d0 + 0x00438> in <filename unknown>:0
at jellyfish.Analytics.AnalyticsModule..ctor () <0x4027ed00 + 0x00103> in <filename unknown>:0
at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) <0x7f8257b94780 + 0x0003f> in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) <0x7f8257b94780 + 0x00079> in <filename unknown>:0
at System.RuntimeType.CreateInstanceMono (Boolean nonPublic) <0x7f82579c1ec0 + 0x00116> in <filename unknown>:0
at System.RuntimeType.CreateInstanceSlow (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, System.Threading.StackCrawlMark& stackMark) <0x7f82579c1e70 + 0x00049> in <filename unknown>:0
at System.RuntimeType.CreateInstanceDefaultCtor (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, System.Threading.StackCrawlMark& stackMark) <0x7f82579c1c10 + 0x0005c> in <filename unknown>:0
at System.Activator.CreateInstance[T] () <0x7f82578dfb70 + 0x0008a> in <filename unknown>:0
at Autofac.ModuleRegistrationExtensions.RegisterModule[TModule] (IModuleRegistrar registrar) <0x40273470 + 0x0004f> in <filename unknown>:0
at Autofac.ModuleRegistrationExtensions.RegisterModule[TModule] (Autofac.ContainerBuilder builder) <0x40273330 + 0x0006b> in <filename unknown>:0
at jellyfish.Cron.Program.BuildContainer (ILog log) <0x402730e0 + 0x0005f> in <filename unknown>:0
at jellyfish.Cron.Program.ExecuteJob (System.Type jobType) <0x4021abe0 + 0x0007b> in <filename unknown>:0
我也有类似的问题,所以我想尝试删除并添加以下dll来修复它。
删除System.net.http, OkHttp, ModernClient。发现这个问题是在引用下添加的ModernClientHttp和OkHttp dll。
我已经删除了OkHttp和ModernClientHttp,并通过Nuget添加了ModernClientHttp,这对我来说很有效。如果有帮助的话,试一试。