locked
Code generated from Odata v4 Service T4 template for client proxy does not add If-None-Match header to query request RRS feed

  • Question

  • I made WebAPI 2 service with OData v4 protocol by using ODataController classes and supporting concurrency checking in Entity Framework. Code for client side is generated by using Odata Client T4 template. This code generates proxy classes and container class derived from DataServiceContext which handles entity tracking, parses LINQ query in Odata URL's etc. When tracked entity (which containes property which is checked for concurrency) is updated, container class on calling SaveChangesAsync methos, generates HTTP PATCH request which contains If-Match header with value received from last query for that entity and that works fine.

    Example request catched by Fiddler:

    PATCH /Users(2) HTTP/1.1

    Cache

    If-Match: W/"J1Z1a2FzaWU3Bhc2IjJw=="

    But in case container makes query for entity, it generates correct HTTP GET request

    (example from request:

    GET /Animals(8) HTTP/1.1)

    but without If-None-Match header, even though in last query, service send etag value in response

    (example from response:

    @odata.etag=W/"J0MxRjIn")

    Is it possible to add If-None-Match header by setting container context configuration requestpipeline properties or on some other way? Just to add remark - in case service gets etag value in If-None-Match header of GET request, it should return status code 304 NotModified in case etag value matches (meaning entity is not changed), otherwise service sends entity properties in response body.

    UPDATE:

    I tried to add If-None-Match by handling SendingRequest2, it enables me to add If-None-Match header manually, but in that case, when service receive request and reads etag from it, if entity is not changed (etag matches), answer would be statuscode 304 - NotModified and container (instance of derived DataServiceContext class) will not be able to handle that response because it expects entity in body message so it will generate exception. I can handle exception and use it to confirm that entity has not been changed but this is not optimized way to do programming. I need a way to get into request and response pipeline events. In request pipeline I would add etag to single query request and in response pipeline I would handle received status code 304 - NotModified.

    The list of context.Configuration.RequestPipeline is:

    OnEntityReferenceLink(Action action);

    OnEntryEnding(Action action);

    OnEntryStarting(Action action);

    OnMessageWriterSettingsCreated(Action args);

    OnNavigationLinkEnding(Action action);

    OnNavigationLinkStarting(Action action);

    and the list of context.Configuration.ResponsePipeline is:

    OnEntityMaterialized(Action action);

    OnEntryEnded(Action action);

    OnEntryStarted(Action action);

    OnFeedEnded(Action action);

    OnFeedStarted(Action action);

    OnMessageReaderSettingsCreated(Action messageReaderSettingsAction);

    OnNavigationLinkEnded(Action action);

    OnNavigationLinkStarted(Action action);

    Friday, October 10, 2014 1:06 PM