2

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 ?

6
  • so JQuery is not an option ? Commented Oct 19, 2016 at 10:43
  • no I need to do that using C#, Create custom event for that Commented 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. Commented Oct 19, 2016 at 10:56
  • @AlexK. No within my application, I edit the question Commented Oct 19, 2016 at 10:58
  • Build the event handling into the class that makes the web requests, is that an option? Commented Oct 19, 2016 at 11:00

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

That is, if you have no control over the application which is requesting and the service itself.
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 ??
OK. As you have full control over your application, you should try something like [stackoverflow.com/questions/18924996/… instead. As it will not litter your code with anything proxy-related.
For HttpClient ?
1

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

0

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.

2 Comments

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
@meJustAndrew I think he wants something similar to Ajax-global-events in JQuery, (api.jquery.com/category/ajax/global-ajax-event-handlers) where he can catch them all in one place. - but without jquery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.