2017-05-02 113 views
1

在我的web服务中,我设置了批量请求。我包装DefaultODataBatchHandler类只是为了添加中断点。它正在成功处理请求,并且CreateResponseMessageAsync方法正在创建200 OK响应。但客户收到以下内容:OData v4 DefaultODataBatchHandler NotImplementedException

{ 
    "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); 
    } 
} 

回答

0

我通过让我打电话到注册的HttpConfiguration一个新实例,而不是重新使用一个从之前(我已经连接好Autofac上)解决了这个。如果您尝试使用GlobalConfiguration.Configuration进行连线,我想你可能会遇到同样的问题。

诀窍是在新的HttpConfiguration实例上设置WebApi。