Read httpContext Request, Response, TraceIdentifier, httpRequestCookies for all the endpoint in the service - asp.net-core

Requirement:
We have like more than 100 endpoints in our service/Api. Do the following:
--> track all the data like the request body, request url, response from our server and cookies.
--> upload that data to blob which would be further processed to insert in Storage for tracking/auditing purpose.
We are using Web Api .net core project.
Need suggestions:
We were thinking to use middleware in .net core to read the request and response object. But found that the httpContext has request/response in stream objects. And would need memory stream and stream reader to read the data. After adding that code in the middleware, it got bit fuzzy. Concern is if there are any issues in this new code, it might break all Api calls. We can wrap the code in try and catch but then was thinking if there is some better way to do this?
Any suggestions are greatly appreciated. Thanking in advance.

Related

Is Serilog DiagnosticContext thread safe?

I am working on a prototype logging solution using Serilog in ASP.NET Core 3.1 and one of my requirements is the ability to log the entire HTTP request and response payload. To do this, I am writing my own middleware and instead of creating a whole new log entry for this data, I would like to associate the request and response payload with the current log context via Serilog's IDiagnosticContext / DiagnosticContext in https://github.com/serilog/serilog-extensions-hosting
I've been studying the code a bit and it appears it is thread-safe by way of the AmbientDiagnosticContextCollector class and the System.Threading.AsyncLocal that it utilizes.
Is my understanding of this correct or am I missing something that I should be accounting for?

MonoTouch, WCF, Large XML response, how to capture progress

I'm developing an app using MonoTouch and have to get some data from a 3rd party web service. I have a WCF binding to do such.
The particular web service method I am calling could potentially return an XML string in the range of several hundred megabytes, which could take a while to download to a mobile device.
I'm looking for some way to capture how many bytes have been read from the network at the system level, with the end-game being to display a progress indicator to the user. Is there some way I can achieve this using Behaviors?
Note, I don't have any way to modify the web service code to return a Stream object, which is what most of the articles I have found require doing.
Any help or direction would be much appreciated. Thanks
As a last resort, I can always fall back on using an NSURLConnection to do this, instead of WCF, because I know there are NSURLConnectionDelegate methods to hook into that will provide this. I wanted to avoid NSURLConnection, so that I will be able to easily drop this code into an Android project in the future.
Is there a reason to use WCF instead of the plain HttpWebRequest?
WCF is not exactly efficient at parsing data, it will keep multiple copies in memory to deserialize the various chunks of information.
There is no system level API that can provide this information, the Stream that you get back from the HttpWebRequest is the best value that you will get.

Response object not usable in WCF?

I'm creating a silverlight-based email system, I use WCF to read emails, then I pass my data to SL app, I've used following codes in another test web project to save a byte array into a file on client system (email attachments), it works fine, but when I want to use them in my WCF (myservice.svc.cs), I get this error: "The name 'Response' does not exist in the current context", what is going wrong here? is it possible to use Response object in a service?
Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", messages[i].Attachments[j].FileName));
Response.BinaryWrite(messages[i].Attachments[j].FileData);
how can I save my attachments?
No, you typically would not use an HttpResponse object in WCF (though I am not 100% sure if you could use it in REST services or not). If you want to send a file to the client, you'll need to implement a service operation that returns a byte array or a file stream. This post might help you with that.

Any benefits to using WCF for this?

I need to receive XML data from HttpPost requests. Currently I use HttpWebRequest to send the request and I convert the request to xml with StreamReader and XDocument.Parse.
Are there any benefits to switching over to WCF? Thanks!
If you don't plan to dramatically extend your application and only want to switch to WCF so that you are using it, no. :-)
WCF will give you some more flexibility - you could for example consume data in other data formats or from other transport formats (Named Pipes, ...)
I hope i understood your question correctly !!!
The use of WCF lies where you know that both the sending as well as receiving end share the same data contract.
I think in your case, using WCF will benefit if both are MS application and the contract is not supposed to change very frequently.

Best practices for streaming response of WCF webservice

I'm trying to pull a large amount of data out of a WCF web service. The request is fairly small and the response message will be very big. Currently the web service is throwing SystemOutOfMemory exceptions due a limitation on IIS6 for the memory it can allocated (~1.4GB).
I have read in some blogs that implementing streaming will solve my problem.
Can anybody share their experiences with this topic? I'm most interested in any sample client-side & service-side code that can be shared or any recommendations/best practices. MemoryStream vs FileStream? Return type should be Stream, Message, Byte[]?
My operation looks like the following: (typically it will return a big number of elements in the response array, ~200K elements)
MediumSizeResponseClass[] GetData(SmallSizeRequestClass request)
If you want to stream back only the response, then use transferMode=streamedResponse in your binding configuration. This makes sure only the returned response will be streamed.
The return value of a streaming function must be a Stream. You can then read from that stream and do whatever you need to do with it (store it, analyse it, whatever).
So basically you'd have a service contract something like this:
[ServiceContract]
interface IYourService
{
[OperationContract]
Stream GetData(SmallSizeRequestClass request);
}
On the server, you basically just write to a stream, while on the client, you read from the stream.
Have you consulted the MSDN docs on WCF Streaming?