locked
DelegatingHandler question? RRS feed

  • Question

  • User-1104215994 posted

    Hello,

    I wonder if I want to log request/responses to <g class="gr_ gr_13 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="13" data-gr-id="13">database</g>;

    1. where should I put the sample code below in my web <g class="gr_ gr_12 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="12" data-gr-id="12">api</g>?
    2. should I have to register <g class="gr_ gr_11 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="11" data-gr-id="11">handler</g> in web config?
    3. Do I need to add something in my controller/methods?
    4. What is the best practice writing requests/responses into database inside of <g class="gr_ gr_62 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="62" data-gr-id="62">delegatinghandler</g>?
    public class LogRequestAndResponseHandler : DelegatingHandler
    {
        protected override async Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // log request body
            string requestBody = await request.Content.ReadAsStringAsync();
            Trace.WriteLine(requestBody);
    
            // let other handlers process the request
            var result = await base.SendAsync(request, cancellationToken);
    
            if (result.Content != null)
            {
                // once response body is ready, log it
                var responseBody = await result.Content.ReadAsStringAsync();
                Trace.WriteLine(responseBody);
            }
    
            return result;
        }
    }
    config.MessageHandlers.Add(new LogRequestAndResponseHandler());

    Trace.WriteLine will be replaced.

    Tuesday, February 5, 2019 11:23 AM

All replies

  • User475983607 posted

    Web API message handling reference.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/http-message-handlers

    Tuesday, February 5, 2019 2:37 PM
  • User283571144 posted

    Hi cenk1536,

    where should I put the sample code below in my web api?

    You could create a new class to put the sample code below in your web api application

    should I have to register handler in web config?

    No, you just need to add below codes in the app_start folder's WebApiConfig.cs file.

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MessageHandlers.Add(new MessageHandler1());
            config.MessageHandlers.Add(new MessageHandler2());
    
            // Other code not shown...
        }
    }

    Do I need to add something in my controller/methods?

    No.

    Best Regards,

    Brando

    Wednesday, February 6, 2019 4:35 AM
  • User-1104215994 posted

    Hi Brando,

    I mean should I put this sample class inside of model? How can I implement <g class="gr_ gr_211 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-del replaceWithoutSep" id="211" data-gr-id="211">a logic</g> in order to store request/response into <g class="gr_ gr_352 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="352" data-gr-id="352">database</g> rather than using trace?

    Best Regards.

    Wednesday, February 6, 2019 5:10 AM
  • User475983607 posted

    I mean should I put this sample class inside of model?

    The handler is used in the HTTP Pipeline not a model. A Models are used to pass data around.  

    How can I implement a logic in order to store request/response into database rather than using trace?

    Replace the trace code with code that writes to the database.

    Wednesday, February 6, 2019 11:55 AM
  • User-1104215994 posted

    Is it a good idea to insert request/response in Delegating Handler? Inserting request/response can be inserted <g class="gr_ gr_29 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="29" data-gr-id="29">in to</g> <g class="gr_ gr_28 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="28" data-gr-id="28">database</g> in controller either. Can I use entity framework?

    Monday, February 11, 2019 8:46 AM
  • User283571144 posted

    Hi cenk1536,

    Is it a good idea to insert request/response in Delegating Handler? Inserting request/response can be inserted in to database in controller either.

    Do you mean you want to call current web api application's method in the Delegating Handler?

    In my opinion, this is not a good idea. You will fired sending request again and again in the Delegating Handler, since the Delegating Handler is fired before the inner handler, if new request come in it will be fired.

    If you send the request to another web api application to add the database, it will work well.

    Can I use entity framework?

    Yes, you could use entity framework in the web api method. 

    Best Regards,

    Brando

    Tuesday, February 12, 2019 2:42 AM
  • User-1104215994 posted

    I want to save request/response messages into DB. I wonder where is the right place? Controller, DelegatingHandler or elsewhere?

    Tuesday, February 12, 2019 2:38 PM
  • User475983607 posted

    I want to save request/response messages into DB. I wonder where is the right place? Controller, DelegatingHandler or elsewhere?

    This has been explained several times in your previous threads -> HTTP Pipeline.

    Not sure what else we can do for you.

    Tuesday, February 12, 2019 2:43 PM
  • User-1104215994 posted

    I gave up inserting request/response in DelegatingHandler.

    Wednesday, February 20, 2019 8:38 AM