Reputation: 384
My solution has two projects one ASP.NET MVC (as UI only) and ASP.NET Web API (as Restful service). I want to send the uploaded files from MVC project to Web API and on deployment each application will be hosted on different server so I need all files to be centralized in Web API server. I need help to compare between two options (performance aspect) for doing that:
[HttpPost]
public ActionResult UploadProfilePicture(HttpPostedFileBase file)
{
using (var client = new HttpClient())
{
using (var content = new MultipartFormDataContent())
{
byte[] Bytes = new byte[file.InputStream.Length + 1];
file.InputStream.Read(Bytes, 0, Bytes.Length);
var fileContent = new ByteArrayContent(Bytes);
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = file.FileName };
content.Add(fileContent);
var requestUri = "http://localhost:1963/api/upload";
var result = client.PostAsync(requestUri, content).Result;
if (result.StatusCode == System.Net.HttpStatusCode.Created)
{
List<string> m = result.Content.ReadAsAsync<List<string>>().Result;
ViewBag.Success = m.FirstOrDefault();
}
else
{
ViewBag.Failed = "Failed !" + result.Content.ToString();
}
}
}
return View();
}
public HttpResponseMessage Post()
{
HttpResponseMessage result = null;
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
var docfiles = new List<string>();
foreach (string file in httpRequest.Files)
{
var postedFile = httpRequest.Files[file];
var filePath = HttpContext.Current.Server.MapPath("~/" + postedFile.FileName);
postedFile.SaveAs(filePath);
docfiles.Add(filePath);
}
result = Request.CreateResponse(HttpStatusCode.Created, docfiles);
}
else
{
result = Request.CreateResponse(HttpStatusCode.BadRequest);
}
return result;
}
System.IO.File.Copy("sourcePath in MVC server", "\\machinename\share folder path");
What is the best performance?
Upvotes: 1
Views: 2872
Reputation: 4365
In option 1, you are uploading the file two times: From client to the server where the MVC application it's hosted, then from there to your Web API.
In Option 2 seems best in performance only with respect of the uploading, as you are calling directly the Web Api from the client.
But as always it depends on what you want to achieve.
You should use the browser (firefox or chrome) profiler to compare the time that each option takes.
Whenever you have doubts about performance you should profile your application. Also remember that performance is a feature.
You can use this tools for different cases of profiling [1]:
Discover hot spots in the code
Discover allocations in the code
Do both
Isolate a function and iterate improvements
[1] Tooling proposed by Ben Adams
Upvotes: 3
Reputation: 22456
I suspect that option 2 with direct file access should provide a better performance, because the protocol that is used is optimized for transferring binary files whereas the HTTP protocol serves different purposes.
However, performance should not be the sole criteria on choosing the solution. If you choose between the options, you also need to evaluate other implications.
From an architectural and security point of view, option 2 is worse than option 1, because:
For these reasons, I'd opt for option 1 as long as the performance is good enough for your requirements.
As an alternative, you could also upload the file from the client to the Web API directly and therefore reduce the number of uploads you have to do.
Upvotes: 4