C# &引用;指数超出范围”;wait Request.Content.ReadAsMultipartAsync(提供程序)时出错

C# &引用;指数超出范围”;wait Request.Content.ReadAsMultipartAsync(提供程序)时出错,c#,asp.net-web-api,oauth-2.0,azure-storage-blobs,bearer-token,C#,Asp.net Web Api,Oauth 2.0,Azure Storage Blobs,Bearer Token,接下来我将从Azure Blob存储上传/下载Blob 在我实现承载令牌身份验证(OAuth)之前,该代码工作得非常好 我从邮递员上传文件时出错。下面是错误描述 { “消息”:“发生错误。详细信息:索引超出范围。必须为非负数且小于索引的大小。” 集合。\r\n参数名称:索引“} 但是,这些文件已成功上载到我的Blob帐户中。但是,我仍然不断出现错误 我已附上调试时获得的错误详细信息的图像 我在上载控制器中提示错误的区域 try { aw

接下来我将从Azure Blob存储上传/下载Blob

在我实现承载令牌身份验证(OAuth)之前,该代码工作得非常好

我从邮递员上传文件时出错。下面是错误描述

{ “消息”:“发生错误。详细信息:索引超出范围。必须为非负数且小于索引的大小。” 集合。\r\n参数名称:索引“}

但是,这些文件已成功上载到我的Blob帐户中。但是,我仍然不断出现错误

我已附上调试时获得的错误详细信息的图像

我在上载控制器中提示错误的区域

 try
            {
                await Request.Content.ReadAsMultipartAsync(provider);

            }
            catch (Exception ex)
            {
                return BadRequest($"An error has occured. Details: {ex.Message}");
            }
My AzureStorageMultipartFormDataStreamProvider类,该类从MultipartFormDataStreamProvider继承

public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
        {


                if (parent == null) throw new ArgumentNullException(nameof(parent));
                if (headers == null) throw new ArgumentNullException(nameof(headers));


            // Generate a new filename for every new blob

            var fileName = Guid.NewGuid().ToString();
            CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(fileName);
            headers.ContentLength = 0;

            if (headers.ContentType != null)
                {
                    // Set appropriate content type for your uploaded file
                    blob.Properties.ContentType = headers.ContentType.MediaType;
                }

                this.FileData.Add(new MultipartFileData(headers, blob.Name));

                return blob.OpenWrite();         
        }
为了实现Oauth2.0身份验证,我刚刚在我的项目中添加了Startup.cs和Startup.Auth.cs(带有所需的NuGet包Owin)

这是我的追踪

位于System.Collections.Generic.List的System.ThrowHelper.ThrowarGumentOutoforangeException(ExceptionArgument参数,ExceptionResource资源)处的System.Net.Http.MultipartFormDataStreamProvider.d_0.MoveNext()处的get_项(Int32索引) ---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务任务)在System.Net.Http.HttpContentMultipartExtensions.d_u0处引发异常的上一个位置的堆栈结束跟踪 ---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()上引发异常的上一个位置的堆栈结束跟踪 在DemoAzureStorage.Controllers.UploadController.d_u1.MoveNext()中


根据您的描述,我已经创建了一个测试演示(所有安装的软件包都是最新的),它运行良好

我使用了asp.net web api和google登录以及AzureStorageMultipartFormDataStreamProvider作为您的演示

我的上传文件控制器代码如下:

public class UploadController : ApiController
{
    private const string Container = "mycontainer";

    [HttpPost]
    public async Task<IHttpActionResult> UploadFile()
    {
        if (!Request.Content.IsMimeMultipartContent("form-data"))
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }


        var storageAccount = CloudStorageAccount.Parse("connection string");
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        CloudBlobContainer imagesContainer = blobClient.GetContainerReference(Container);
        var provider = new AzureStorageMultipartFormDataStreamProvider(imagesContainer);

        try
        {
            await Request.Content.ReadAsMultipartAsync(provider);
        }
        catch (Exception ex)
        {
            return BadRequest($"An error has occured. Details: {ex.Message}");
        }

        // Retrieve the filename of the file you have uploaded
        var filename = provider.FileData.FirstOrDefault()?.LocalFileName;
        if (string.IsNullOrEmpty(filename))
        {
            return BadRequest("An error has occured while uploading your file. Please try again.");
        }

        return Ok($"File: {filename} has successfully uploaded");
    }
}
结果如下图所示:

如果可能,您可以创建一个测试项目,并将其推送到github或一个驱动器,以便我们重现您的问题

Packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.4.1.9004" targetFramework="net46" />
  <package id="bootstrap" version="3.0.0" targetFramework="net46" />
  <package id="EntityFramework" version="6.1.3" targetFramework="net46" />
  <package id="jQuery" version="1.10.2" targetFramework="net46" />
  <package id="Knockout.Validation" version="1.0.1" targetFramework="net46" />
  <package id="knockoutjs" version="2.3.0" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights" version="2.2.0" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.0.6" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.2.0" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.2.0" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights.Web" version="2.2.0" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights.WindowsServer" version="2.2.0" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.2.0" targetFramework="net46" />
  <package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net46" />
  <package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net46" />
  <package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net46" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net46" />
  <package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net46" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.5" targetFramework="net46" />
  <package id="Microsoft.Data.Edm" version="5.8.2" targetFramework="net46" />
  <package id="Microsoft.Data.OData" version="5.8.2" targetFramework="net46" />
  <package id="Microsoft.Data.Services.Client" version="5.8.2" targetFramework="net46" />
  <package id="Microsoft.Net.Compilers" version="2.1.0" targetFramework="net46" developmentDependency="true" />
  <package id="Microsoft.Owin" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.Facebook" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.Google" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.MicrosoftAccount" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.Twitter" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net46" />
  <package id="Modernizr" version="2.6.2" targetFramework="net46" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net46" />
  <package id="Owin" version="1.0" targetFramework="net46" />
  <package id="Respond" version="1.2.0" targetFramework="net46" />
  <package id="Sammy.js" version="0.7.4" targetFramework="net46" />
  <package id="System.ComponentModel.EventBasedAsync" version="4.0.11" targetFramework="net46" />
  <package id="System.Dynamic.Runtime" version="4.0.0" targetFramework="net46" />
  <package id="System.Linq.Queryable" version="4.0.0" targetFramework="net46" />
  <package id="System.Net.Requests" version="4.0.11" targetFramework="net46" />
  <package id="System.Spatial" version="5.8.2" targetFramework="net46" />
  <package id="WebGrease" version="1.5.2" targetFramework="net46" />
  <package id="WindowsAzure.Storage" version="8.5.0" targetFramework="net46" />
</packages>

根据您的描述,我已经在我这边创建了一个测试演示(所有安装的软件包都是最新的),它运行良好

我使用了asp.net web api和google登录以及AzureStorageMultipartFormDataStreamProvider作为您的演示

我的上传文件控制器代码如下:

public class UploadController : ApiController
{
    private const string Container = "mycontainer";

    [HttpPost]
    public async Task<IHttpActionResult> UploadFile()
    {
        if (!Request.Content.IsMimeMultipartContent("form-data"))
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }


        var storageAccount = CloudStorageAccount.Parse("connection string");
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        CloudBlobContainer imagesContainer = blobClient.GetContainerReference(Container);
        var provider = new AzureStorageMultipartFormDataStreamProvider(imagesContainer);

        try
        {
            await Request.Content.ReadAsMultipartAsync(provider);
        }
        catch (Exception ex)
        {
            return BadRequest($"An error has occured. Details: {ex.Message}");
        }

        // Retrieve the filename of the file you have uploaded
        var filename = provider.FileData.FirstOrDefault()?.LocalFileName;
        if (string.IsNullOrEmpty(filename))
        {
            return BadRequest("An error has occured while uploading your file. Please try again.");
        }

        return Ok($"File: {filename} has successfully uploaded");
    }
}
结果如下图所示:

如果可能,您可以创建一个测试项目,并将其推送到github或一个驱动器,以便我们重现您的问题

Packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.4.1.9004" targetFramework="net46" />
  <package id="bootstrap" version="3.0.0" targetFramework="net46" />
  <package id="EntityFramework" version="6.1.3" targetFramework="net46" />
  <package id="jQuery" version="1.10.2" targetFramework="net46" />
  <package id="Knockout.Validation" version="1.0.1" targetFramework="net46" />
  <package id="knockoutjs" version="2.3.0" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights" version="2.2.0" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.0.6" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.2.0" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.2.0" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights.Web" version="2.2.0" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights.WindowsServer" version="2.2.0" targetFramework="net46" />
  <package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.2.0" targetFramework="net46" />
  <package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net46" />
  <package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net46" />
  <package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net46" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net46" />
  <package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net46" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.5" targetFramework="net46" />
  <package id="Microsoft.Data.Edm" version="5.8.2" targetFramework="net46" />
  <package id="Microsoft.Data.OData" version="5.8.2" targetFramework="net46" />
  <package id="Microsoft.Data.Services.Client" version="5.8.2" targetFramework="net46" />
  <package id="Microsoft.Net.Compilers" version="2.1.0" targetFramework="net46" developmentDependency="true" />
  <package id="Microsoft.Owin" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.Facebook" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.Google" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.MicrosoftAccount" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Owin.Security.Twitter" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net46" />
  <package id="Modernizr" version="2.6.2" targetFramework="net46" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net46" />
  <package id="Owin" version="1.0" targetFramework="net46" />
  <package id="Respond" version="1.2.0" targetFramework="net46" />
  <package id="Sammy.js" version="0.7.4" targetFramework="net46" />
  <package id="System.ComponentModel.EventBasedAsync" version="4.0.11" targetFramework="net46" />
  <package id="System.Dynamic.Runtime" version="4.0.0" targetFramework="net46" />
  <package id="System.Linq.Queryable" version="4.0.0" targetFramework="net46" />
  <package id="System.Net.Requests" version="4.0.11" targetFramework="net46" />
  <package id="System.Spatial" version="5.8.2" targetFramework="net46" />
  <package id="WebGrease" version="1.5.2" targetFramework="net46" />
  <package id="WindowsAzure.Storage" version="8.5.0" targetFramework="net46" />
</packages>


共享调用堆栈/堆栈跟踪。@rene更新了堆栈跟踪共享调用堆栈/堆栈跟踪。@rene更新了堆栈跟踪我已在Github上发布了我的项目。我使用了密码授予类型。因此请使用下面的访问令牌。{“访问令牌”:4.在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,p6lu7dYFw,“令牌类型”:持票人,“到期日”:60899,“刷新令牌”:“a868cb78-4773-4fbf-888e-38a6b0a11700”}我无法运行你的应用程序。你可以共享一个完整的构建应用程序吗?此外,我发现你的Owin版本是2.1,我建议你可以更新到3.1并重试。更改Owin版本无效。请稍候,我将与你共享一个完整的构建应用程序复制你的问题后,我找到了原因。我找到了你的Microsoft.AspNet.WebAp我的版本是5.0.0。我想这可能与MultipartFormDataStreamProvider类有问题。我建议您可以尝试将Microsoft.AspNet.WebApi版本上载到5.2.3。它会很好地工作。我已经在Github上发布了我的项目。我使用了密码授予类型。因此请使用下面的访问令牌。{“访问令牌”:2.在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在北京,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在一一个中国,在一个中国,在一个中国,一个中国,在一个LU7DYFW,“令牌类型”:持票人,“到期日”:60899,“刷新令牌”:“a868cb78-4773-4fbf-888e-38a6b0a11700”}我无法运行你的应用程序。你可以共享一个完整的构建应用程序吗?此外,我发现你的Owin版本是2.1,我建议你可以更新到3.1并重试。更改Owin版本无效。请稍候,我将与你共享一个完整的构建应用程序复制你的问题后,我找到了原因。我找到了你的Microsoft.AspNet.WebAp我的版本是5.0.0。我想这可能与MultipartFormDataStreamProvider类有问题。我建议您可以尝试将Microsoft.AspNet.WebApi版本上载到5.2.3。它将正常工作。