Search code examples
c#asp.net-web-apiodata

WebApi, OData and ConventionErrors


I am struggling with the OData WebApi way to configure routing - mostly by not being able to get any sensible debugging information.

The API in question is part of a server service (as in: windows service) and as such OWIN based.

I have for example the following function:

function = builder.Function("MktSessions").ReturnsCollection<MktSession>();
function.Parameter<string>("Symbol");
function.Parameter<DateTimeOffset>("Begin");
function.Parameter<DateTimeOffset>("End");

and the controller has the following signature function:

[HttpGet]
[ODataRoute("MktSessions(Symbol={symbol},Begin={begin},End={end}")]
public IEnumerable<Reflexo.Api.MktSession> MktSessions (string symbol, DateTime begin, DateTime end) {

SOMETHING is wrong here. As long as the ODataRouter attbribute is presend, any call to $metadata resunlts in:

An error has occurred. The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code. System.InvalidOperationException at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.get_AttributeMappings() at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.SelectController(ODataPath odataPath, HttpRequestMessage request) at System.Web.OData.Routing.ODataPathRouteConstraint.SelectControllerName(ODataPath path, HttpRequestMessage request) at System.Web.OData.Routing.ODataPathRouteConstraint.Match(HttpRequestMessage request, IHttpRoute route, String parameterName, IDictionary`2 values, HttpRouteDirection routeDirection) at System.Web.Http.Routing.HttpRoute.ProcessConstraint(HttpRequestMessage request, Object constraint, String parameterName, HttpRouteValueDictionary values, HttpRouteDirection routeDirection) at System.Web.Http.Routing.HttpRoute.ProcessConstraints(HttpRequestMessage request, HttpRouteValueDictionary values, HttpRouteDirection routeDirection) at System.Web.Http.Routing.HttpRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request) at System.Web.Http.HttpRouteCollection.GetRouteData(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.HttpServer.d__0.MoveNext()

which is as useless as it gets as an error message. I never have a chance to see original exception and this one jsut tells me the config is not there - which has no direct resemblence of the original error.

If I remove the ODataRoute attribute it works - but I can obviously not call the function.

Is there any way I am overlooking to actually get a meaningfull error message from this? Obviously the ODataRoute is somewhere in error (anyone knows where?) and a sensible "Parameter name blablbla does not match" text somewhere would be really helpfull.


Solution

  • Answering myself.

    Now, on the side question there is a missing ")" at the end of the template.

    More important, though.

    When I add a call to HttpConfiguration.EnsureInitialized() to the end of the Owin configuration then I get the exception thrown there. This exception - while not having an inner exception - contains a meaningfull message that is lost in the web page output. This allows much better debugging.