Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Migrating System.Net.Http dependency to ASP .Net Core

$
0
0

We have an existing Web application build on top of Owin/Katana based on .Net framework 4.5.2. We are trying to migrate the same to ASP .Net Core. Once migrated, we would like to run the Application in Docker using the microsoft/dotnet base docker image on linux.

Now, in our existing application, we use .NETFramework\v4.5.2\System.Net.Http.dll for making HTTP requests similar to as given below

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
            httpWebRequest.ProtocolVersion = HttpVersion.Version11;
            httpWebRequest.Method = "POST";
            httpWebRequest.Headers.Add("xxx", "yyy");
            httpWebRequest.ContentType = "application/json";

            // Populate httpWebRequest.GetRequestStream() with input

            var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            int statusCode = (int)httpWebResponse.StatusCode;

            if (statusCode != 200 && statusCode != 204)
            {
               throw new HttpResponseException(response); // HttpWebResponse is converted to HttpResponseMessage
            }

            // De-serialize response to a JSON object
            using (var sr = new StreamReader(httpWebResponse.GetResponseStream()))
            {
                using (var outw = new Newtonsoft.Json.JsonTextReader(sr))
                {
                    output = serializer.Deserialize<TOut>(outw);
                }
            }

We also use MultipartFormDataStreamProvider from Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll in Controllers which are used to upload files. See below for an example

            if (Request.Content.IsMimeMultipartContent())
            {
            	// download the files locally in temp directory
            	string root = Path.GetTempPath();
            	var provider = new MultipartFormDataStreamProvider(root);
            	await Request.Content.ReadAsMultipartAsync(provider);
            }

            foreach (MultipartFileData file in provider.FileData)
            {
                // Read file and upload to database
            }

Now, the question is how do we get these working in Asp .Net Core?

I saw the following NuGet packages for Asp .Net Core
"Microsoft.AspNetCore.Http.Abstractions": "1.0.0",
"Microsoft.AspNetCore.Http.Features": "1.0.0",
"Microsoft.AspNetCore.Http.Extensions": "1.0.0"

And I see that Microsoft.AspNetCore.Http.Abstractions and its implementation Microsoft.AspNetCore.Http are used by Microsoft.AspNetCore.Mvc.Core. Like the HttpRequest and HttpResponse in ControllerBase.

So, is it that I need to use these packages instead of the System.Net.Http package used in .Net 452?

For the MultipartFormDataStreamProvider, I can get it from "System.Net.Http.Formatting.Extension": "5.2.3". However I cannot read the Request content as MultiPart(Request.Content.ReadAsMultipartAsync(provider)) like in the above .Net 452 case since Microsoft.AspNetCore.Http.HttpRequest(from Microsoft.AspNetCore.Http.Abstractions) does not contain the Content property.

Please let me know how I should go ahead. Also, note that I need to get this application working in linux(Using docker) and hence would like to use the packages that work fine in both Windows as well as Linux.

Thanks in advance,
Ranjith


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>