I am trying to implement an odata query with async and support for $select and $expand. I can get everything working when I create a regular sync method returning HttpResponseMessage, but as soon as I make it async and return Task<HttpResponseMessage> I start getting 406 error responses. Is this a bug with the OData MediaType Formatters or am I doing something wrong?
I followed Sample #4 [here](https://aspnetwebstack.codeplex.com/wikipage?title=%24select%20and%20%24expand%20support&referringTitle=Specs) and I got it working just fine.
```C#
public class EntriesController : ODataController
{
public HttpResponseMessage Get(ODataQueryOptions<CatalogEntry> opts)
{
var db = new IntegrationCatalog();
var query = opts.ApplyTo(db.Entries.AsNoTracking());
var result = query;
//var result = (await query.ToListAsync()).AsQueryable();
return CreateQueryableResponse(Request, result);
}
private static HttpResponseMessage CreateQueryableResponse(HttpRequestMessage request, IQueryable queryable)
{
MethodInfo mi = typeof(EntriesController).GetMethod("CreateQueryableResponseImpl", BindingFlags.Static | BindingFlags.NonPublic);
mi = mi.MakeGenericMethod(queryable.ElementType);
return mi.Invoke(null, new object[] { request, queryable }) as HttpResponseMessage;
}
private static HttpResponseMessage CreateQueryableResponseImpl<T>(HttpRequestMessage request, IQueryable<T> response)
{
return request.CreateResponse(response as IQueryable<T>);
}
}
```
However, as soon as I change the method to async it starts returning an http 406 on me.
```C#
public async Task<HttpResponseMessage> Get(ODataQueryOptions<CatalogEntry> opts)
{
var db = new IntegrationCatalog();
var query = opts.ApplyTo(db.Entries.AsNoTracking());
//var result = query;
var result = (await query.ToListAsync()).AsQueryable();
return CreateQueryableResponse(Request, result);
}
```
I followed Sample #4 [here](https://aspnetwebstack.codeplex.com/wikipage?title=%24select%20and%20%24expand%20support&referringTitle=Specs) and I got it working just fine.
```C#
public class EntriesController : ODataController
{
public HttpResponseMessage Get(ODataQueryOptions<CatalogEntry> opts)
{
var db = new IntegrationCatalog();
var query = opts.ApplyTo(db.Entries.AsNoTracking());
var result = query;
//var result = (await query.ToListAsync()).AsQueryable();
return CreateQueryableResponse(Request, result);
}
private static HttpResponseMessage CreateQueryableResponse(HttpRequestMessage request, IQueryable queryable)
{
MethodInfo mi = typeof(EntriesController).GetMethod("CreateQueryableResponseImpl", BindingFlags.Static | BindingFlags.NonPublic);
mi = mi.MakeGenericMethod(queryable.ElementType);
return mi.Invoke(null, new object[] { request, queryable }) as HttpResponseMessage;
}
private static HttpResponseMessage CreateQueryableResponseImpl<T>(HttpRequestMessage request, IQueryable<T> response)
{
return request.CreateResponse(response as IQueryable<T>);
}
}
```
However, as soon as I change the method to async it starts returning an http 406 on me.
```C#
public async Task<HttpResponseMessage> Get(ODataQueryOptions<CatalogEntry> opts)
{
var db = new IntegrationCatalog();
var query = opts.ApplyTo(db.Entries.AsNoTracking());
//var result = query;
var result = (await query.ToListAsync()).AsQueryable();
return CreateQueryableResponse(Request, result);
}
```