OData v4 DefaultODataBatchHandler NotImplementedException

В моем веб-сервисе я настроил пакетные запросы. Я обернул класс DefaultODataBatchHandler только для того, чтобы добавить точки останова. Он успешно обрабатывает запросы, и метод CreateResponseMessageAsync создает ответ 200 OK. Но клиент получает следующее:

{
  "Message": "An error has occurred.",
  "ExceptionMessage": "The method or operation is not implemented.",
  "ExceptionType": "System.NotImplementedException",
  "StackTrace": "   at System.Web.HttpContextBase.get_Response()\r\n   at System.Web.UI.Util.GetUrlWithApplicationPath(HttpContextBase context, String url)\r\n   at System.Web.Routing.RouteCollection.NormalizeVirtualPath(RequestContext requestContext, String virtualPath)\r\n   at System.Web.Routing.RouteCollection.GetVirtualPath(RequestContext requestContext, String name, RouteValueDictionary values)\r\n   at System.Web.Http.WebHost.Routing.HostedHttpRouteCollection.GetVirtualPath(HttpRequestMessage request, String name, IDictionary`2 values)\r\n   at System.Web.Http.Routing.UrlHelper.GetVirtualPath(HttpRequestMessage request, String routeName, IDictionary`2 routeValues)\r\n   at System.Web.Http.Routing.UrlHelper.Route(String routeName, IDictionary`2 routeValues)\r\n   at System.Web.Http.Routing.UrlHelper.Link(String routeName, IDictionary`2 routeValues)\r\n   at System.Web.OData.Extensions.UrlHelperExtensions.CreateODataLink(UrlHelper urlHelper, String routeName, IODataPathHandler pathHandler, IList`1 segments)\r\n   at System.Web.OData.Extensions.UrlHelperExtensions.CreateODataLink(UrlHelper urlHelper, IList`1 segments)\r\n   at System.Web.OData.Formatter.ODataMediaTypeFormatter.GetDefaultBaseAddress(HttpRequestMessage request)\r\n   at System.Web.OData.Formatter.ODataMediaTypeFormatter.GetBaseAddressInternal(HttpRequestMessage request)\r\n   at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders)\r\n   at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Net.Http.HttpContent.<CopyToAsyncCore>d__44.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.OData.Batch.ODataBatchResponseItem.<WriteMessageAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.OData.Batch.ODataBatchContent.<SerializeToStreamAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__49.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Owin.HttpMessageHandlerAdapter.<BufferResponseContentAsync>d__13.MoveNext()"
}

Я понятия не имею, почему и откуда это происходит.

ODataConfig.cs

    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        var builder = new ODataConventionModelBuilder();

        var tags = builder.EntitySet<EventTag>("EventTags");
        tags.HasOptionalBinding(x => x.Parent, tags);

        var events = builder.EntitySet<Event>("Events");
        events.HasManyBinding(x => x.Tags, tags);

        var batchHandler = new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer);

        config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel(), batchHandler);
    }
}

person Paul - Soura Tech LLC    schedule 02.05.2017    source источник


Ответы (1)


Я решил эту проблему, убедившись, что вызываю Register с новым экземпляром HttpConfiguration, а не повторно использую один из предыдущих (к которому я подключил Autofac). Я думаю, у вас могут возникнуть те же проблемы, если вы попытаетесь подключить это, используя GlobalConfiguration.Configuration.

Хитрость заключается в настройке WebApi на новом экземпляре HttpConfiguration.

person James Love    schedule 14.12.2017