Dou*_*son 4 c# asp.net-web-api
我的团队最近对Web API服务进行了重构,以将一些重复代码移入静态方法。一种方法与从请求中提取上载的文件有关。该方法可用于单元测试,但在负载下会引发异常。在SO帖子中找到了部分代码,但总的来说,我担心我们没有正确使用它。这是代码:
internal static string ExtractFile(HttpRequestMessage request)
{
if (request.Content.IsMimeMultipartContent())
{
string uploadRoot = ServiceHelper.GetUploadDirectoryPath();
var provider = new MultipartFormDataStreamProvider(uploadRoot);
try
{
Task.Factory
.StartNew(() => provider = request.Content.ReadAsMultipartAsync(provider).Result,
CancellationToken.None,
TaskCreationOptions.LongRunning, // guarantees separate thread
TaskScheduler.Default)
.Wait();
}
catch(System.AggregateException ae)
{
if(log.IsErrorEnabled)
{
foreach(var ex in ae.InnerExceptions)
{
log.Error("ReadAsMultipartAsync task error.", ex);
}
}
var errorResponse = request.CreateErrorResponse(HttpStatusCode.InternalServerError, "An error occurred while extracting the uploaded file from the request.");
throw new HttpResponseException(errorResponse);
}
var fileData = provider.FileData.First();
var localName = fileData.LocalFileName;
var content = File.ReadAllText(localName);
if (log.IsDebugEnabled)
{
var embeddedName = fileData.Headers.ContentDisposition.FileName;
log.DebugFormat("File {0} was successfully uploaded as '{1}'.", embeddedName, localName);
}
return content;
}
else
{
log.Error("Invalid request received. Request must be in a multipart/form-data request.");
var errorResponse = request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Request must be a multipart/form-data request and contain one file.");
throw new HttpResponseException(errorResponse);
}
}
Run Code Online (Sandbox Code Playgroud)
漫步日志,我看到这样的错误:
System.IO.IOException:读取MIME多部分正文部分时出错。---> System.IO.IOException ---> System.Net.HttpListenerException:由于线程退出或应用程序请求,I / O操作已中止
HttpListenerRequest处置
System.IO.IOException:读取MIME多部分正文部分时出错。---> System.IO.IOException ---> System.Net.HttpListenerException:尝试对不存在的网络连接进行操作
该Web服务作为自托管的OWIN Windows服务运行。文件上传很小(3k至4k)。
我无法通过一次上传来重新创建该问题。与服务对话的客户端使用任务来发布文件,但通常不会同时运行4个或5个以上的任务。我和我的团队相对不熟悉.NET任务。其中一位开发人员想知道TaskCreationOptions.LongRunning参数是否确实会带来更大的伤害。有什么建议么?
更新:
我尝试用以下方法关闭Task.Factory代码:
var task = Task.Run(async () => await request.Content.ReadAsMultipartAsync(provider));
task.Wait();
provider = task.Result;
Run Code Online (Sandbox Code Playgroud)
我仍然有一些问题,但这似乎更好。虽然不知道为什么。
Kha*_*321 15
我有同样的问题,可以通过在web.config中添加以下代码来解决
<system.web>
<httpRuntime maxRequestLength="30000000" />
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="30000000" />
</requestFiltering>
</security>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
来源:http : //stackoverflow.com/questions/20942636/webapi-cannot-parse-multipart-form-data-post
让我声明,这不是代码失败原因的答案,而是我的一个问题,如果有一些空间来显示一些格式化的代码,就更容易提出这个问题。如果这不提供任何帮助/方向,我将/可以删除此答案。
我假设您的方法是从传入的 Web api 请求调用的。你有什么理由不让这个“一直异步”吗?这是构造它的最简单的方法,并且以后由于糟糕/有缺陷的代码而死锁的可能性最小(如果您刚刚开始使用 Tasks 和 async/await ,这真的很容易做到)。
// new signature (I changed the name and prefixed Async which is a common convention)
internal static async Task<string> ExtractFileAsync(HttpRequestMessage request)
{
if (request.Content.IsMimeMultipartContent())
{
string uploadRoot = ServiceHelper.GetUploadDirectoryPath();
var provider = new MultipartFormDataStreamProvider(uploadRoot);
await request.Content.ReadAsMultipartAsync(provider);
/* rest of code */
}
}
// calling method example from some Web API Controller
public async Task<IHttpActionResult> Post(CancellationToken token)
{
var result = await ExtractFileAsync(Request).ConfigureAwait(true);
/*some other code*/
}
Run Code Online (Sandbox Code Playgroud)