首页 文章

在一定数量的API调用之后,Azure服务器抛出异常

提问于
浏览
1

我已经创建了一个API(MVC)并在Azure服务器中托管它 . API一次接收1个图像和1个文本 . 它适用于单个呼叫或第一次呼叫但在某些数量的API呼叫低于错误之后 .

After installing Microsoft.Azure.DocumentDB :

错误:

===预绑定状态信息===日志:DisplayName = Microsoft.Azure.Documents.Client,Version = 1.11.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35(完全指定)LOG:Appbase = file:// / D:/ Project / 002 MVC API / Cherish / API / Cherish.Api / LOG:Initial PrivatePath = D:\ Project \ 002 MVC API \ Cherish \ API \ Cherish.Api \ bin调用程序集:Cherish.Domain,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null . ===日志:此绑定在默认加载上下文中启动 . 日志:使用应用程序配置文件:D:\ Project \ 002 MVC API \ Cherish \ API \ Cherish.Api \ web.config日志:使用主机配置文件:C:\ Users \ Yudiz \ Documents \ IISExpress \ config \ aspnet.config日志:使用C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ config \ machine.config中的计算机配置文件 . 日志:政策后参考:Microsoft.Azure.Documents.Client,Version = 1.11.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35日志:尝试下载新的URL文件:/// C:/ Users / Yudiz / AppData / Local / Temp / Temporary ASP.NET Files / vs / 7b6de7f7 / 4f48effb / Microsoft.Azure.Documents.Client.DLL . 日志:尝试下载新的URL文件:/// C:/ Users / Yudiz / AppData / Local / Temp / Temporary ASP.NET Files / vs / 7b6de7f7 / 4f48effb / Microsoft.Azure.Documents.Client / Microsoft.Azure.Documents .Client.DLL . 日志:尝试下载新的URL文件:/// D:/ Project / 002 MVC API / Cherish / API / Cherish.Api / bin / Microsoft.Azure.Documents.Client.DLL . 警告:比较程序集名称导致不匹配:主要版本错误:无法完成程序集的设置(hr = 0x80131040) . 探测终止 .

这是我的代码片段:

var blobHelper = new BlobHelper();
var response = new UploadMediaResponse();
for (var i = 0; i<provider.Files.Count; i++)
{
    Guid itemId;
    var fileData = await provider.Files[i].ReadAsByteArrayAsync();
    if (mediaType == MediaType.Thumbnail)
    {
        // MediaId is mandatory for Audio & Video media
        if (String.IsNullOrWhiteSpace(provider.FormData["mediaId"]))
        // ReSharper disable once NotResolvedInText
            return ErrorResponse(new ArgumentNullException("mediaId", "MediaId was not provided"));
        itemId = new Guid(provider.FormData["mediaId"]);
        await blobHelper.Upload(mediaType, accountId, itemId, fileData, timelineEvent.ChildId);
        var mediaItem = timelineEvent.Media.Single(m => m.Id == itemId);
        mediaItem.HasThumbnail = true;
        mediaItem.LastUpdated = DateTime.UtcNow;
        await timelineEventRepository.UpdateAsync(timelineEvent, entityDoc.SelfLink);
    }
    else
    {
        itemId = await blobHelper.Upload(mediaType, accountId, fileData, timelineEvent.ChildId);
        var mediaItem = new TimelineMediaItem
        {
            Id = itemId,
            Available = true,
            FileSize = fileData.Length,
            Removed = false,
            Type = mediaType.ToUpper(),
            TaggedChildren = new List<TaggedChild>(),
            Created = DateTime.UtcNow,
            LastUpdated = DateTime.UtcNow
        };
        if (fileLengthsRequired)
            mediaItem.Length = fileLengths[i];
        timelineEvent.Media.Add(mediaItem);
        await timelineEventRepository.UpdateAsync(timelineEvent, entityDoc.SelfLink);
    }
    response.MediaIds.Add(itemId);
}

DocumentClient声明:

public DocumentRepository()
{
    Client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["DocumentDbEndpointUrl"]), ConfigurationManager.AppSettings["DocumentDbAuthKey"]);
    DatabaseName = ConfigurationManager.AppSettings["DocumentDbDatabaseName"];
    PageSize = Int16.Parse(ConfigurationManager.AppSettings["DefaultPageSize"]);
    var _database = ReadOrCreateDatabase();
    var collection = InitialiseCollection(_database.SelfLink, EntityName);
    DocumentsLink = collection.DocumentsLink;
    SelfLink = collection.SelfLink;
}

这是我的错误:

错误字符串:=>可选({“消息”:“发生了错误 . ”,“ExceptionMessage”:“发生了一个或多个错误 . ”,“ExceptionType”:“System.AggregateException”,“StackTrace”:“at系统.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)\ r \ n在System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)\ r \ n在System.Threading.Tasks.Task1.get_Result()\ r \在System.Linq.Enumerable.ToArray [TSource]的System.Linq.Buffer1..ctor(IEnumerable1 source)\ r \ n的Microsoft.Azure.Documents.Linq.DocumentQuery1.GetEnumerator()\ r \ n中的IE(IEnumerable1 source) )在Cherish.Api.Helpers.BlobHelper的Cherish.Domain上的Cherish.Domplements.Implementation.DocumentRepository1.ReadOrCreateDatabase()\ r \ n,在Cherish.Domain.Repositories.Implementation.DocumentRepository1..ctor()\ r \ n .AccountHasEnoughStorageCapacity(Guid accountId,Int32 fileSize)\ r \ n在Cherish.Api.Helpers.BlobHelper.d__4.MoveNext()\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪--- \ r \ n \呐t System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Cherish.Api.Helpers.BlobHelper.d__3.MoveNext() \ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束---在System.Runtime.CompilerServices上的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n中的\ r \ n . SystemAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务)\ r \ n在Cherish.Api.Controllers.TimelineController.d__15.MoveNext()\ r \ n ---从抛出异常的先前位置开始的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n处于System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中的\ r \ n \ r \ n在Cherish.Api.Controllers.TimelineController.d__11.MoveNext()“,”InnerException“:{”消息“:”一个e rror已经发生 . “,”ExceptionMessage“:”发送请求时发生错误 . “,”ExceptionType“:”System.Net.Http.HttpRequestException“,”StackTrace“:”at Microsoft.Azure.Documents.BackoffRetryUtility1.d__0 .MoveNext()\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束---在系统中的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n中的\ r \ n . Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n atMicrosoft.Azure.Documents.Linq.DocumentQueryExecutionContext.d__7.MoveNext()\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.Azure.Documents.Linq.DocumentQuery1.d__10.MoveNext()“,”InnerException“:{”消息“:”发生错误 . “,”ExceptionMessage“:”无法连接到远程服务器“,”ExceptionType“:”System.Net.WebException“,”StackTrace“:”在System.Net.HttpWebRequest.EndGetRequestStream (IAsyncResult asyncResult,TransportContext&context)\ r \ n在System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)“,”InnerException“:{”Message“:”发生错误 . “,”ExceptionMessage“:”An尝试以其访问权限“,”ExceptionType“:”System.Net.Sockets.SocketException“,”StackTrace“:”在Syst禁止的方式访问套接字System.Net.Sockets.Socket.BeginConnectEx(EndPoint)上System.Net.Sockets.Socket.InternalBind(EndPoint localEP)\ r \ n的em.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress socketAddress)\ r \ n remoteEP,Boolean flowContext,AsyncCallback回调,对象状态)\ r \ n在System.Net.Sockets.Socket.UnsafeBeginConnect(EndPoint remoteEP,AsyncCallback回调,对象状态)\ r \ n在System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure) ,Socket s4,Socket s6,Socket&socket,IPAddress&address,ConnectSocketState state,IAsyncResult asyncResult,Exception&exception)“}}}})

先感谢您 .

2 回答

  • 0

    根据您的问题,我假设在一定数量的API调用之后,客户端和documentdb endpoints 之间存在连接问题 . 您可以尝试创建 DocumentClient 的静态实例,并将重试策略添加到 DocumentRepository 类,如下所示:

    public class DocumentRepository
    {
        private static readonly DocumentClient _client;
        static DocumentRepository()
        {
            var connectionPolicy = new ConnectionPolicy
            {
                RetryOptions = new RetryOptions()
                {
                    MaxRetryAttemptsOnThrottledRequests = 5,
                    MaxRetryWaitTimeInSeconds = 5
                }
            };
            _client = new DocumentClient(
                new Uri(ConfigurationManager.AppSettings["DocumentDbEndpointUrl"]),
                ConfigurationManager.AppSettings["DocumentDbAuthKey"],
                connectionPolicy);
        }
    
        public DocumentRepository()
        {
            DatabaseName = ConfigurationManager.AppSettings["DocumentDbDatabaseName"];
            PageSize = Int16.Parse(ConfigurationManager.AppSettings["DefaultPageSize"]);
            var _database = ReadOrCreateDatabase();
            var collection = InitialiseCollection(_database.SelfLink, EntityName);
            DocumentsLink = collection.DocumentsLink;
            SelfLink = collection.SelfLink;
        }
    
        public async Task UpdateAsync(TimelineEvent timelineEvent, string selfLink)
        {
            //TODO:
            await _client.UpsertDocumentAsync("{documentCollectionUri}", timelineEvent);
        }
    }
    

    此外,有一个官方blog谈论Azure DocumentDB的性能提示,你可以参考它 .

  • 1

    接受的答案实际上不是上述问题的解决方案 . 添加重试信息确实隐藏了一些症状,但它不会阻止它在将来再次发生 .

    如果没有正确定义 DocumentClient AS A Singleton 那么可能应用程序将用完 TCP connections ,那就是当你得到 socket error 时 .

    有关更多信息,请参阅this link on MSDN .

    谢谢,我希望这有助于其他人 .

相关问题