本文介绍了Google.Apis.Email_Migration_v2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图从每个UploadAsync方法调用中检索HttpStatusCode。我需要状态码才能正确执行指数退避算法以重试失败的上载,并在不重试上载时向用户显示错误消息并报告上传成功。我不在乎它是如何被接收的,只要它是干净的,而不是从像下面提供的链接中提到的Tor Jonsson那样的Exception.Message(string)属性进行解析。 为了强制Bad Request Error [400],我只是在MailResource.InsertMediaUpload的构造函数中提供了一个无效的用户密钥(email)。 例如MailResource.InsertMediaUpload(mailItem,[email protected],stream,message / rfc822) strong> GoogleApiException.HttpStatusCode始终为0(不可用)。即使Exception.Message似乎包含括号中的状态码。例如 [400] 2)无法找到GoogleApiRequestException。 问题 1)执行指数退避算法的最佳方法是什么? 2)在这种情况下,这是该属性的预期行为吗? 3)是否仍然存在GoogleApiRequestException,如果有的话? 注意: 我还注意到,GoogleApiRequestException类不再与Go​​ogleApiException类相同。它被移动到另一个命名空间还是被删除?因为我想尝试捕获一个GoogleApiRequestException对象并获取其RequestError对象。 为了我的意思,我添加了两个diff的链接: 之前: http://code.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis/GoogleApiException.cs?r=a8e27790f8769c1d6aaae030bb46c79daa7cdbad 之后: http://code.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis/GoogleApiException.cs?r=d6f06e92d90b635c179013e2c287b42b82909c09 来源 我使用NuGet的最新二进制文件(1.6.0.8-beta) 唯一的问题我发现与我的问题相关:(只能发布两个链接...继承人原始) stackoverflow.com/questions/18985306/httpstatuscode-not-set-in-exceptions-when-using- google-net-apis 代码:(使用自定义记录器写入debugview) public int Index; //用于标识进程 private void TryUpload(MailResource.InsertMediaUpload upload,out IUploadProgress uploadProgress,out bool retryUpload) { uploadProgress = null; retryUpload = false; CancellationToken令牌; 尝试 { uploadProgress = upload.UploadAsync(token).Result; if(uploadProgress.Exception!= null) { _logger.WriteTrace(EXCEPTION !!! Type:{0},uploadProgress.Exception.GetType()。的ToString()); //移除: // *)处理所有各种异常 if(uploadProgress.Exception是JsonReaderException) { JsonReaderException jreEx = uploadProgress.Exception as JsonReaderException; _logger.WriteTrace(JsonReaderException->消息:{0},jreEx.Message); } if(uploadProgress.Exception为TokenResponseException) { TokenErrorResponse trEx = uploadProgress as TokenErrorResponse; _logger.WriteTrace(TokenErrorResponse-> Message:{0},trEx.Error); } if(uploadProgress.Exception是HttpRequestValidationException) { HttpRequestValidationException hrvEx = uploadProgress.Exception as HttpRequestValidationException; _logger.WriteTrace(HttpRequestValidationException->消息:{0},hrvEx.Message); _logger.WriteTrace(HttpRequestValidationException - >状态码:{0},hrvEx.GetHttpCode()); } if(uploadProgress.Exception为GoogleApiException) { GoogleApiException gApiEx = uploadProgress.Exception as GoogleApiException; _logger.WriteTrace(GoogleApiException->消息:{0},gApiEx.Message); _logger.WriteTrace(GoogleApiException->状态码:{0},gApiEx.HttpStatusCode); $ b catch(Exception ex) { _logger.WriteTrace(例如,上传时发生异常...) ; } finally { if(uploadProgress!= null) _logger.WriteTrace(Upload Completed ... Status:{0} Exception ?: {1 }, uploadProgress.Status,(uploadProgress.Exception == null)?None:uploadProgress.Exception.ToString()); else _logger.WriteTrace(上传中止...已退出但未返回状态!); } } 输出摘要 > [5224](T101)VSLLC:EXCEPTION !!!类型:Google.GoogleApiException [5224](T101)VSLLC:GoogleApiException->消息:Google.Apis.Requests.RequestError [5224]错误的请求[400] [5224]错误[ [5224]消息[Bad Request]位置[ - ]原因[badRequest]域名[global] [5224]] [5224](T101)VSLLC:GoogleApiException-> 状态码:0 [5224](T101)VSLLC:Upload Completed ...状态:失败异常?:服务管理员引发了一个异常:Google.GoogleApiException:Google.Apis.Requests.RequestError [5224]错误的请求[400] [5224]错误[ [5224]消息[Bad Request]位置[ - ]原因[badRequest]域名[global] [5224]] [5224] [5224]在Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) [5224]在Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(任务任务) [5224]在Microsoft.Runtime.CompilerServices .askAwaiter.ValidateEnd(任务任务) [5224] at Google.Apis.Upload.ResumableUpload`1.d__0.MoveNext()in c:\code\google.com\google-api -dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\output\default\Src\GoogleApis\Apis [媒体] \Upload\ResumableUpload.cs:第373行 对于广泛的帖子,抱歉!感谢您的时间! 解决方案 库已经支持503响应的指数退避。在400(不好的请求)的情况下,你不应该重试,因为你会一遍又一遍地得到相同的响应。 查看服务初始化程序参数 DefaultExponentialBackOffPolicy 您也可以查看我们的 ExponentialBackOff implementation 。 BackOffHandler 封装逻辑并实现不成功的响应处理程序和异常处理程序。 GoogleApiRequest不再存在。 看起来我们没有正确设置状态代码,因为您可以找到 here 。我在问题跟踪器中发布了一个新问题,可在此处找到 - https://code.google.com/p/google-api-dotnet-client/issues/detail?id=425 。随意添加更多内容。 I am attempting to retrieve the HttpStatusCode from every UploadAsync method call. I need the status code as to properly perform an exponential back-off algorithm to retry a failed upload, display an error message to the user when not retrying the upload and to report success of the upload. I do not care how it is received, so long as it is clean and not being parsed from the Exception.Message (string) property like Tor Jonsson suggested in the link provided below.To force the "Bad Request Error [400]" I simply provided an invalid userkey (email) in the constructor for MailResource.InsertMediaUpload.e.g. MailResource.InsertMediaUpload(mailItem, "[email protected]", stream, "message/rfc822")Problem1) GoogleApiException.HttpStatusCode is always 0 (unavailable). Even when Exception.Message appears to contain a status code in brackets. e.g. [400]2) Cannot find GoogleApiRequestException.Questions1) What is the best way to perform the exponential back-off algorithm???2) Is this the expected behaviour for this property in this case?3) Does GoogleApiRequestException still exist, if so where?Side Note:I also noticed that the GoogleApiRequestException class is no longer in the same file as GoogleApiException class. Has it been moved to another namespace or deleted? Because I would like to attempt to catch a GoogleApiRequestException object and grab its RequestError object.I added links to the two diffs for what I mean:Before: http://code.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis/GoogleApiException.cs?r=a8e27790f8769c1d6aaae030bb46c79daa7cdbadAfter: http://code.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis/GoogleApiException.cs?r=d6f06e92d90b635c179013e2c287b42b82909c09SourcesI'm using the latest binaries from NuGet (1.6.0.8-beta)The only question I found related to my problem: (Can only post two links... heres the raw)stackoverflow.com/questions/18985306/httpstatuscode-not-set-in-exceptions-when-using-google-net-apisCode: (Using a custom logger to write to debugview) public int Index; // Used to Id the process private void TryUpload(MailResource.InsertMediaUpload upload, out IUploadProgress uploadProgress, out bool retryUpload) { uploadProgress = null; retryUpload = false; CancellationToken token; try { uploadProgress = upload.UploadAsync(token).Result; if (uploadProgress.Exception != null) { _logger.WriteTrace("EXCEPTION!!! Type: {0}", uploadProgress.Exception.GetType().ToString()); // Remove: // *) Handle all of the various exceptions if (uploadProgress.Exception is JsonReaderException) { JsonReaderException jreEx = uploadProgress.Exception as JsonReaderException; _logger.WriteTrace("JsonReaderException-> Message: {0}", jreEx.Message); } if (uploadProgress.Exception is TokenResponseException) { TokenErrorResponse trEx = uploadProgress as TokenErrorResponse; _logger.WriteTrace("TokenErrorResponse-> Message: {0}", trEx.Error); } if (uploadProgress.Exception is HttpRequestValidationException) { HttpRequestValidationException hrvEx = uploadProgress.Exception as HttpRequestValidationException; _logger.WriteTrace("HttpRequestValidationException-> Message: {0}", hrvEx.Message); _logger.WriteTrace("HttpRequestValidationException-> Status Code: {0}", hrvEx.GetHttpCode()); } if (uploadProgress.Exception is GoogleApiException) { GoogleApiException gApiEx = uploadProgress.Exception as GoogleApiException; _logger.WriteTrace("GoogleApiException-> Message: {0}", gApiEx.Message); _logger.WriteTrace("GoogleApiException-> Status Code: {0}", gApiEx.HttpStatusCode); } } } catch (Exception ex) { _logger.WriteTrace(ex, "An exception occured while uploading..."); } finally { if (uploadProgress != null) _logger.WriteTrace("Upload Completed... Status: {0} Exception?: {1}", uploadProgress.Status, (uploadProgress.Exception == null) ? "None" : uploadProgress.Exception.ToString()); else _logger.WriteTrace("Upload Aborted... Exited without returning a status!"); } }Output Snippet[5224] (T101) VSLLC: EXCEPTION!!! Type: Google.GoogleApiException[5224] (T101) VSLLC: GoogleApiException-> Message: Google.Apis.Requests.RequestError[5224] Bad Request [400][5224] Errors [[5224] Message[Bad Request] Location[ - ] Reason[badRequest] Domain[global][5224] ][5224] (T101) VSLLC: GoogleApiException-> Status Code: 0[5224] (T101) VSLLC: Upload Completed... Status: Failed Exception?: The service admin has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError[5224] Bad Request [400][5224] Errors [[5224] Message[Bad Request] Location[ - ] Reason[badRequest] Domain[global][5224] ][5224][5224] at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)[5224] at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)[5224] at Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)[5224] at Google.Apis.Upload.ResumableUpload`1.d__0.MoveNext() in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\output\default\Src\GoogleApis\Apis[Media]\Upload\ResumableUpload.cs:line 373Sorry for the extensive post! Thanks for your time! 解决方案The library already supports exponential back-off for 503 responses. In case of 400 (bad request) you should not retry, because you will get the same response over and over again.Take a look in the service initializer parameter DefaultExponentialBackOffPolicyYou can also take a look in our ExponentialBackOff implementation. BackOffHandler wraps the logic and implements unsuccessful response handler and exception handler.GoogleApiRequest doesn't exists anymore.It looks like we are not setting the status code properly, as you can find here. I open a new issue in our issue tracker, available here - https://code.google.com/p/google-api-dotnet-client/issues/detail?id=425. Feel free to add more content to it. 这篇关于Google.Apis.Email_Migration_v2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-20 15:28