繁体   English   中英

使用Owin和WebApi的OData v4在本地工作,但不在Azure中工作

[英]OData v4 using Owin and WebApi works locally but not in Azure

我正在使用以下软件包(节选):

<package id="Microsoft.AspNet.OData" version="6.1.0" targetFramework="net461" />
<package id="Microsoft.AspNet.OData.Versioning" version="2.2.0" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi" version="5.2.4" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.4" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.4" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.OData" version="5.7.0" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.4" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Tracing" version="5.2.4" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Versioning" version="2.3.0" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.4" targetFramework="net461" />
<package id="Microsoft.OData.Core" version="7.4.4" targetFramework="net461" />
<package id="Microsoft.OData.Edm" version="7.4.4" targetFramework="net461" />
<package id="Microsoft.Owin" version="4.0.0" targetFramework="net461" />
<package id="Microsoft.Owin.Cors" version="4.0.0" targetFramework="net461" />

我使用以下配置代码(也是摘录):

IEnumerable<ODataMediaTypeFormatter> formatters = ODataMediaTypeFormatters.Create();
httpConfiguration.Formatters.InsertRange(0, formatters);
httpConfiguration.MapHttpAttributeRoutes();
httpConfiguration.MapODataServiceRoute("OData", "v1.0", modelBuilder.GetEdmModels().First());

app.UseWebApi(httpServer);

请注意,我禁用了版本化的API部分,因为我认为这可能是原因,但事实并非如此。 错误仍然相同。 通常,对于我的所有配置,我都会使用

HttpServer httpServer = GlobalConfiguration.DefaultServer;
HttpConfiguration httpConfiguration = httpServer.Configuration;

我总是使用相同的httpConfiguration变量。 无论我调用哪个URL,可能是对集合的简单GET还是本地的OData动作(IIS)都可以正常工作。 我记得起初我在使用格式化程序时遇到了麻烦,但是使用当前版本在本地一切都很好。

将代码上传到Azure到暂存环境后,所有与OData相关的请求都将停止工作。 这是痕迹:

2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Type='HttpError', formatters=[MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer]', Operation=PerRequestContentNegotiator.Negotiate
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Obtaining formatter of type 'ODataMediaTypeFormatter' for type='HttpError', mediaType='application/json'', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Will use new 'ODataMediaTypeFormatter' formatter', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Obtaining formatter of type 'ODataMediaTypeFormatter' for type='HttpError', mediaType='application/json'', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Will use new 'ODataMediaTypeFormatter' formatter', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Obtaining formatter of type 'ODataMediaTypeFormatter' for type='HttpError', mediaType='application/json'', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Will use new 'ODataMediaTypeFormatter' formatter', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Obtaining formatter of type 'ODataMediaTypeFormatter' for type='HttpError', mediaType='application/json'', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Will use new 'ODataMediaTypeFormatter' formatter', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Obtaining formatter of type 'ODataMediaTypeFormatter' for type='HttpError', mediaType='application/json'', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Will use new 'ODataMediaTypeFormatter' formatter', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Obtaining formatter of type 'ODataMediaTypeFormatter' for type='HttpError', mediaType='application/json'', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Will use new 'ODataMediaTypeFormatter' formatter', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Obtaining formatter of type 'ODataMediaTypeFormatter' for type='HttpError', mediaType='application/json'', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Will use new 'ODataMediaTypeFormatter' formatter', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Obtaining formatter of type 'ODataMediaTypeFormatter' for type='HttpError', mediaType='application/json'', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Will use new 'ODataMediaTypeFormatter' formatter', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Obtaining formatter of type 'ODataMediaTypeFormatter' for type='HttpError', mediaType='application/json'', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Will use new 'ODataMediaTypeFormatter' formatter', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Obtaining formatter of type 'ODataMediaTypeFormatter' for type='HttpError', mediaType='application/json'', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Will use new 'ODataMediaTypeFormatter' formatter', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Obtaining formatter of type 'FormUrlEncodedMediaTypeFormatter' for type='HttpError', mediaType='application/json'', Operation=FormUrlEncodedMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Will use same 'FormUrlEncodedMediaTypeFormatter' formatter', Operation=FormUrlEncodedMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Obtaining formatter of type 'JQueryMvcFormUrlEncodedFormatter' for type='HttpError', mediaType='application/json'', Operation=JQueryMvcFormUrlEncodedFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Will use same 'JQueryMvcFormUrlEncodedFormatter' formatter', Operation=JQueryMvcFormUrlEncodedFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Type='HttpError', formatters=[MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, MediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer]', Operation=DefaultContentNegotiator.Negotiate
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Obtaining formatter of type 'ODataMediaTypeFormatter' for type='HttpError', mediaType='application/json; charset=utf-8'', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Will use same 'ODataMediaTypeFormatter' formatter', Operation=ODataMediaTypeFormatter.GetPerRequestFormatterInstance
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Selected formatter='ODataMediaTypeFormatter', content-type='application/json; charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Selected formatter='ODataMediaTypeFormatter', content-type='application/json; charset=utf-8'', Operation=PerRequestContentNegotiator.Negotiate
2018-04-24T13:39:30  PID[7936] Information [2018-04-24T13:39:30.3614885Z] Level=Info, Kind=Begin, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Message='Value='System.Web.Http.HttpError', type='HttpError', content-type='application/json; odata.metadata=minimal'', Operation=ODataMediaTypeFormatter.WriteToStreamAsync
2018-04-24T13:39:30  PID[7936] Error       [2018-04-24T13:39:30.3614885Z] Level=Error, Kind=End, Category='System.Net.Http.Formatting', Id=800002ee-0000-ef00-b63f-84710c7967bb, Operation=ODataMediaTypeFormatter.WriteToStreamAsync, Exception=System.NotImplementedException: The method or operation is not implemented.
   at System.Web.HttpContextBase.get_Response()
   at System.Web.UI.Util.GetUrlWithApplicationPath(HttpContextBase context, String url)
   at System.Web.Routing.RouteCollection.NormalizeVirtualPath(RequestContext requestContext, String virtualPath)
   at System.Web.Routing.RouteCollection.GetVirtualPath(RequestContext requestContext, String name, RouteValueDictionary values)
   at System.Web.Http.WebHost.Routing.HostedHttpRouteCollection.GetVirtualPath(HttpRequestMessage request, String name, IDictionary`2 values)
   at System.Web.Http.Routing.UrlHelper.GetVirtualPath(HttpRequestMessage request, String routeName, IDictionary`2 routeValues)
   at System.Web.Http.Routing.UrlHelper.Link(String routeName, IDictionary`2 routeValues)
   at System.Web.OData.Formatter.ODataMediaTypeFormatter.GetDefaultBaseAddress(HttpRequestMessage request)
   at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders)
   at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__19.MoveNext()

似乎它想序列化HttpError对象,但是不能。 我不知道该错误对象中包含什么,因为我看不到它。 我的代码没有创建这样的对象。 我知道验证可以,但是我还没有验证任何东西。

即使调用$metadata也会导致此错误。 该服务针对每个请求返回500。 我可以保证数据库连接正常。 我什至可以在调试器中看到这一点。 我的控制器和相应的Action被调用(ODataParameters为null ,这是不应该的),这也可能与此相关。

我确实在云环境中将.Net版本设置为4.7。 我也整日尝试了不同的设置,但没有任何改变。

我的猜测是整个HttpConfiguration东西有问题。

有没有人遇到过这种情况,或者没有人知道可能是什么问题?

任何提示,不胜感激。

我设法解决了这个问题。 这是微不足道的。 我不知道为什么,但是app.UseWebApi(httpConfiguration)杀死了它。 我查看了堆栈跟踪,发现在WebApi中的某个点它试图从HttpRequestMessage获取HttpContext ,该消息为null 因此,最终的解决方案是完全删除此呼叫。

我找到这个是因为我拼命地重新创建了整个项目,并逐行复制了:(

谢谢@NinjaDeveloper的回答。 我尝试了一下,但没有任何改变。 我还尝试了其他各种组合。

降级到.net 4.6.1,可能将此节点添加到Web配置文件中

  <system.web>
    <compilation targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.5.1" /> 
  </system.web>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM