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 Oct 19 '16 at 10:43
-
no I need to do that using C#, Create custom event for that – Ahmed Oct 19 '16 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. Oct 19 '16 at 10:56
-
@AlexK. No within my application, I edit the question – Ahmed Oct 19 '16 at 10:58
-
Build the event handling into the class that makes the web requests, is that an option? – Lasse V. Karlsen Oct 19 '16 at 11:00
-
Perhaps look at a Trace Listener, e.g. http://stackoverflow.com/questions/1049442/system-net-httpwebrequest-tracing-without-using-files-or-app-config – Alex K. Oct 19 '16 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.

- 1
- 1

- 76
- 3
-
That is, if you have no control over the application which is requesting and the service itself. – V. Kasparavičius Oct 19 '16 at 10:52
-
what do you mean no control? .. It is my application, I need to create a Helper class contains an event fire automatically to capture and log all requests and responses in my application .. I still need a proxy ?? – Ahmed Oct 19 '16 at 10:54
-
OK. As you have full control over your application, you should try something like [http://stackoverflow.com/questions/18924996/logging-request-response-messages-when-using-httpclient instead. As it will not litter your code with anything proxy-related. – V. Kasparavičius Oct 19 '16 at 10:59
-
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.

- 111
- 1
- 5
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.

- 6,011
- 8
- 50
- 76
-
I need a custom and dynamic event for that, to fire automatically with any web service call, I don't want to end up adding code every where in my application – Ahmed Oct 19 '16 at 10:48
-
@meJustAndrew I think he wants something similar to Ajax-global-events in JQuery, (https://api.jquery.com/category/ajax/global-ajax-event-handlers/) where he can catch them all in one place. - but without jquery. – Stavm Oct 19 '16 at 10:50