C# 将文件从xamarin上载到Web api服务器-处理请求花费的时间太长

C# 将文件从xamarin上载到Web api服务器-处理请求花费的时间太长,c#,file-upload,asp.net-web-api,xamarin,C#,File Upload,Asp.net Web Api,Xamarin,我正在使用HttpClient向Azure托管的服务器发布一个二进制文件。对于大小不太大的文件,请求需要花费0长时间才能完成。有时,当客户端由于超时而取消任务时,服务器会收到请求。我正在使用以下代码异步上载数据: public async Task<HttpResponseMessage> UploadFile(byte[] file) { videoThumbName = Guid.NewGuid().ToString(); var progress = new S

我正在使用HttpClient向Azure托管的服务器发布一个二进制文件。对于大小不太大的文件,请求需要花费0长时间才能完成。有时,当客户端由于超时而取消任务时,服务器会收到请求。我正在使用以下代码异步上载数据:

public async Task<HttpResponseMessage> UploadFile(byte[] file)
{
    videoThumbName = Guid.NewGuid().ToString();
    var progress = new System.Net.Http.Handlers.ProgressMessageHandler();

    progress.HttpSendProgress += progress_HttpSendProgress;

    using (var client = HttpClientFactory.Create(progress))
    {
        client.BaseAddress = new Uri(GlobalVariables.host);

        // Set the Accept header for BSON.
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/bson"));

        var request = new uploadFileModel { data = file, dateCreated = DateTime.Now, fileName = fileName, username = loggedUser, VideoThumbName = videoThumbName};

        // POST using the BSON formatter.
        MediaTypeFormatter bsonFormatter = new BsonMediaTypeFormatter();
        var m = client.MaxResponseContentBufferSize;
        var result = await client.PostAsync("api/media/upload", request, bsonFormatter);

        return result.EnsureSuccessStatusCode();
    }
}
公共异步任务上载文件(字节[]文件)
{
videoThumbName=Guid.NewGuid().ToString();
var progress=new System.Net.Http.Handlers.ProgressMessageHandler();
progress.HttpSendProgress+=进度\u HttpSendProgress;
使用(var client=HttpClientFactory.Create(progress))
{
client.BaseAddress=新Uri(GlobalVariables.host);
//为BSON设置接受标头。
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
具有QualityHeaderValue(“应用程序/bson”)的新媒体类型;
var request=newuploadfilemodel{data=file,dateCreated=DateTime.Now,fileName=fileName,username=loggedUser,VideoThumbName=VideoThumbName};
//使用BSON格式化程序进行POST。
MediaTypeFormatter bsonFormatter=新的BsonMediaTypeFormatter();
var m=client.MaxResponseContentBufferSize;
var result=wait client.PostAsync(“api/media/upload”,请求,bsonFormatter);
返回结果。EnsureSuccessStatusCode();
}
}
服务器端代码如下所示(为了简洁起见,我留下了一些代码):

[HttpPost]
[路由(“上传”)]
公共异步任务上载(uploadFileModel模型)
{
var结果=新的HttpResponseMessage(HttpStatusCode.OK);
if(ModelState.IsValid)
{
字符串thumbname=model.VideoThumbName;
字符串tempPath=HttpContext.Current.Server.MapPath(“~/video”);
字符串sourceFilePath=Path.Combine(HttpContext.Current.Server.MapPath(“~/video”)、model.fileName);
字符串pathToThumbs=Path.Combine(HttpContext.Current.Server.MapPath(“~/contents/member/”+model.username+“/thumbs”)、thumbname+“.jpg”);
字符串finalPath=Path.Combine(HttpContext.Current.Server.MapPath(“~/contents/member/”+model.username+“/flv”)、model.fileName);
字符串thumbPath=HttpContext.Current.Server.MapPath(“~/contents/member/”+model.username+“/thumbs”);
字符串vidDuration=“”;
字符串videoDurationSec=“”;
int maxWidth=380;
int maxHeight=360;
尝试
{
File.writealBytes(sourceFilePath,model.data);
}
捕获(例外e)
{
控制台写入线(e.Message);
}
var ffMpegThumb=新的NReco.VideoConverter.FFMpegConverter();
GetVideo缩略图(源文件路径、路径到缩略图);
var ffmpegVid=新的NReco.VideoConverter.FFMpegConverter();
ConvertMedia(sourceFilePath,Format.mp4,finalPath,Format.mp4,new ConvertSettings(){VideoCodec=“h264”});
返回结果;
}
其他的
{
抛出新的HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable,“此请求未正确格式化”);
}
}
有没有可能是因为我的网络连接请求传输得太晚?我发送的文件甚至没有那么大,它们的大小大约为1-2MB。我想知道大文件需要多长时间。
是否还有其他方法可以改进此过程?

这可能无法解决您当前的问题,但使用ModernHttpClient可能会大大缩短网络呼叫响应时间。通过Postman(或类似工具)手动发布BSON内容的时间比较如何?当我使用fiddler发布时,服务器端会立即收到请求。我会让ModernHttpClient试一试
[HttpPost]
[Route("upload")]
public async Task<HttpResponseMessage> Upload(uploadFileModel model)
{
    var result = new HttpResponseMessage(HttpStatusCode.OK);

    if (ModelState.IsValid)
    {
        string thumbname = model.VideoThumbName;
        string tempPath = HttpContext.Current.Server.MapPath("~/video");
        string sourceFilePath = Path.Combine(HttpContext.Current.Server.MapPath("~/video"), model.fileName);
        string pathToThumbs = Path.Combine(HttpContext.Current.Server.MapPath("~/contents/member/" + model.username + "/thumbs"), thumbname + ".jpg");
        string finalPath = Path.Combine(HttpContext.Current.Server.MapPath("~/contents/member/" + model.username + "/flv"), model.fileName);
        string thumbPath = HttpContext.Current.Server.MapPath("~/contents/member/" + model.username + "/thumbs");
        string vidDuration = "";
        string videoDurationSec = "";

        int maxWidth = 380;
        int maxHeight = 360;

        try
        {
            File.WriteAllBytes(sourceFilePath, model.data);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        var ffMpegThumb = new NReco.VideoConverter.FFMpegConverter();

        ffMpegThumb.GetVideoThumbnail(sourceFilePath, pathToThumbs);

        var ffmpegVid = new NReco.VideoConverter.FFMpegConverter();

        ffmpegVid.ConvertMedia(sourceFilePath, Format.mp4, finalPath, Format.mp4, new ConvertSettings() { VideoCodec = "h264" });

        return result;
    }
    else
    {
        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
    }

}