I have a .Net application (console or web), I need to create a Helper class contains an event in C# to fire automatically And log request and response when I call a restful service (Within my application not external application) I prefer using a native framework not a library. Is that possible ?
-
so JQuery is not an option ?Stavm– Stavm2016-10-19 10:43:08 +00:00Commented Oct 19, 2016 at 10:43
-
no I need to do that using C#, Create custom event for thatAhmed– Ahmed2016-10-19 10:43:53 +00:00Commented Oct 19, 2016 at 10:43
-
Are you asking how to intercept HTTP traffic from an external application you have no control over? Presumably its not your application issuing the requests you want to log? Please clarify.Alex K.– Alex K.2016-10-19 10:56:23 +00:00Commented Oct 19, 2016 at 10:56
-
@AlexK. No within my application, I edit the questionAhmed– Ahmed2016-10-19 10:58:30 +00:00Commented Oct 19, 2016 at 10:58
-
Build the event handling into the class that makes the web requests, is that an option?Lasse V. Karlsen– Lasse V. Karlsen2016-10-19 11:00:10 +00:00Commented Oct 19, 2016 at 11:00
3 Answers
If you have no control over the application and service, you could setup a proxy server in C# and route that other application traffic through it (either using that application or global windows/linux/wtw settings). In C# it could be done using HttpListener (for listening) and HttpWebRequest to forward the requests. An example of full - featured proxy server: Titanium Web Proxy.
If you have control over either of them, something like this could be used.
4 Comments
HttpClient ?You can create a middleware to intercept the HTTP processing. I know it should be fairly straight forward in ASP.NET Core and I just found something along those lines for .NET.
https://www.azurefromthetrenches.com/capturing-and-tracing-all-http-requests-in-c-and-net/
In the link above you will find how to listen to events in .NET. You test the event type and if they are of the desired type you will be able to do your custom processing, capturing, tracking etc.
This question is 3 years old so it might not help you but someone else that lands here like me. Also, it would be nice if you could share what worked for you.
Comments
You can create your own event and trigger it on the method calls to the service. So yes, it is possible. You will have to create a service caller and your own event. On each method from the service caller, trigger the event. I am not going to add the code or samples on how to create an event. You should be doing the research yourself.