{"action":"created","issue":{"url":"https://api.github.com/repos/OData/WebApi/issues/170","repository_url":"https://api.github.com/repos/OData/WebApi","labels_url":"https://api.github.com/repos/OData/WebApi/issues/170/labels{/name}","comments_url":"https://api.github.com/repos/OData/WebApi/issues/170/comments","events_url":"https://api.github.com/repos/OData/WebApi/issues/170/events","html_url":"https://github.com/OData/WebApi/issues/170","id":54015091,"number":170,"title":"ODataMediaTypeFormatter is throwing a SerializationException when SingleResult.Create takes in a query that returns an empty result.","user":{"login":"LianwMS","id":9375712,"avatar_url":"https://avatars.githubusercontent.com/u/9375712?v=3","gravatar_id":"","url":"https://api.github.com/users/LianwMS","html_url":"https://github.com/LianwMS","followers_url":"https://api.github.com/users/LianwMS/followers","following_url":"https://api.github.com/users/LianwMS/following{/other_user}","gists_url":"https://api.github.com/users/LianwMS/gists{/gist_id}","starred_url":"https://api.github.com/users/LianwMS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LianwMS/subscriptions","organizations_url":"https://api.github.com/users/LianwMS/orgs","repos_url":"https://api.github.com/users/LianwMS/repos","events_url":"https://api.github.com/users/LianwMS/events{/privacy}","received_events_url":"https://api.github.com/users/LianwMS/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/OData/WebApi/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/OData/WebApi/labels/FollowUp","name":"FollowUp","color":"006b75"},{"url":"https://api.github.com/repos/OData/WebApi/labels/Import%20From%20CodePlex","name":"Import From CodePlex","color":"000000"},{"url":"https://api.github.com/repos/OData/WebApi/labels/medium","name":"medium","color":"E36B23"}],"state":"closed","locked":false,"assignee":{"login":"VikingsFan","id":5082288,"avatar_url":"https://avatars.githubusercontent.com/u/5082288?v=3","gravatar_id":"","url":"https://api.github.com/users/VikingsFan","html_url":"https://github.com/VikingsFan","followers_url":"https://api.github.com/users/VikingsFan/followers","following_url":"https://api.github.com/users/VikingsFan/following{/other_user}","gists_url":"https://api.github.com/users/VikingsFan/gists{/gist_id}","starred_url":"https://api.github.com/users/VikingsFan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/VikingsFan/subscriptions","organizations_url":"https://api.github.com/users/VikingsFan/orgs","repos_url":"https://api.github.com/users/VikingsFan/repos","events_url":"https://api.github.com/users/VikingsFan/events{/privacy}","received_events_url":"https://api.github.com/users/VikingsFan/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/OData/WebApi/milestones/14","html_url":"https://github.com/OData/WebApi/milestones/v5.8","labels_url":"https://api.github.com/repos/OData/WebApi/milestones/14/labels","id":1156192,"number":14,"title":"v5.8","description":"","creator":{"login":"congysu","id":7425148,"avatar_url":"https://avatars.githubusercontent.com/u/7425148?v=3","gravatar_id":"","url":"https://api.github.com/users/congysu","html_url":"https://github.com/congysu","followers_url":"https://api.github.com/users/congysu/followers","following_url":"https://api.github.com/users/congysu/following{/other_user}","gists_url":"https://api.github.com/users/congysu/gists{/gist_id}","starred_url":"https://api.github.com/users/congysu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/congysu/subscriptions","organizations_url":"https://api.github.com/users/congysu/orgs","repos_url":"https://api.github.com/users/congysu/repos","events_url":"https://api.github.com/users/congysu/events{/privacy}","received_events_url":"https://api.github.com/users/congysu/received_events","type":"User","site_admin":false},"open_issues":4,"closed_issues":37,"state":"open","created_at":"2015-06-09T04:55:49Z","updated_at":"2016-01-12T04:09:08Z","due_on":"2016-01-07T16:00:00Z","closed_at":null},"comments":23,"created_at":"2015-01-12T02:18:25Z","updated_at":"2016-02-07T21:14:32Z","closed_at":"2015-10-29T02:12:49Z","body":"A SerializationException is being thrown from\nSystem.Web.OData.Formatter.ODataMediaTypeFormatter.GetSerializer(...) when\nSingleResult.Create(...) takes in a query that returns an empty result.  \n  \nThis happens when an invalid key is specified by the user. A 404 Not Found\nshould be returned instead of a 500 Internal Server Error.  \n  \nThis issue is related to:  \n[ https://aspnetwebstack.codeplex.com/workitem/1021\n](https://aspnetwebstack.codeplex.com/workitem/1021)  \n[ https://aspnetwebstack.codeplex.com/workitem/1040\n](https://aspnetwebstack.codeplex.com/workitem/1040)  \n  \n** To Reproduce **   \n\n    \n    \n    [EnableQuery]\n    public SingleResult<Product> GetProduct([FromODataUri] int key)\n    {\n        return SingleResult.Create(Enumerable.Empty<Product>().AsQueryable());\n    }\n\n** Exception Details **   \n\n    \n    \n    {\n        \"message\": \"An error has occurred.\",\n        \"innererror\": {\n            \"message\": \"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'.\",\n            \"type\": \"System.InvalidOperationException\",\n            \"internalexception\": {\n                \"message\": \"'SingleResult`1' cannot be serialized using the ODataMediaTypeFormatter.\",\n                \"type\": \"System.Runtime.Serialization.SerializationException\",\n                \"stacktrace\": \"   at System.Web.OData.Formatter.ODataMediaTypeFormatter.GetSerializer(Type type, Object value, IEdmModel model, ODataSerializerProvider serializerProvider)\\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.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()\"\n            }\n        }\n    }\n\n** Workaround **   \n\n    \n    \n    public class NullSerializerProvider : DefaultODataSerializerProvider\n    {\n        private readonly NullEntityTypeSerializer _nullEntityTypeSerializer;\n    \n        public NullSerializerProvider()\n        {\n            _nullEntityTypeSerializer = new NullEntityTypeSerializer(this);\n        }\n    \n        public override ODataSerializer GetODataPayloadSerializer(IEdmModel model, Type type, HttpRequestMessage request)\n        {\n            var serializer = base.GetODataPayloadSerializer(model, type, request);\n            if (serializer == null)\n            {\n                var response = request.GetOwinContext().Response;\n                response.OnSendingHeaders(state =>\n                {\n                    ((IOwinResponse)state).StatusCode = (int)HttpStatusCode.NotFound;\n                }, response);\n                return _nullEntityTypeSerializer;\n            }\n            return serializer;\n        }\n    }\n    \n    \n    public class NullEntityTypeSerializer : ODataEntityTypeSerializer\n    {\n        public NullEntityTypeSerializer(ODataSerializerProvider serializerProvider)\n            : base(serializerProvider)\n        { }\n    \n        public override void WriteObjectInline(object graph, IEdmTypeReference expectedType, ODataWriter writer, ODataSerializerContext writeContext)\n        {\n            if (graph != null)\n            {\n                base.WriteObjectInline(graph, expectedType, writer, writeContext);\n            }\n        }\n    }\n    \n    \n    var odataFormatters = ODataMediaTypeFormatters.Create(new NullSerializerProvider(), new DefaultODataDeserializerProvider());\n    config.Formatters.InsertRange(0, odataFormatters);\n\nWork Item Details\n--------------------\n**Original CodePlex Issue:**\t[Issue 2190](http://aspnetwebstack.codeplex.com/workitem/2190)\n**Status:**\tProposed\n**Reason Closed:**\tUnassigned\n**Assigned to:**\tlianw\n**Reported on:**\tNov 20, 2014 at 3:00 PM\n**Reported by:**\tryansimmen\n**Updated on:**\tTue at 2:28 PM\n**Updated by:**\ttimmhagen\n"},"comment":{"url":"https://api.github.com/repos/OData/WebApi/issues/comments/181118514","html_url":"https://github.com/OData/WebApi/issues/170#issuecomment-181118514","issue_url":"https://api.github.com/repos/OData/WebApi/issues/170","id":181118514,"user":{"login":"parthsha","id":8407673,"avatar_url":"https://avatars.githubusercontent.com/u/8407673?v=3","gravatar_id":"","url":"https://api.github.com/users/parthsha","html_url":"https://github.com/parthsha","followers_url":"https://api.github.com/users/parthsha/followers","following_url":"https://api.github.com/users/parthsha/following{/other_user}","gists_url":"https://api.github.com/users/parthsha/gists{/gist_id}","starred_url":"https://api.github.com/users/parthsha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/parthsha/subscriptions","organizations_url":"https://api.github.com/users/parthsha/orgs","repos_url":"https://api.github.com/users/parthsha/repos","events_url":"https://api.github.com/users/parthsha/events{/privacy}","received_events_url":"https://api.github.com/users/parthsha/received_events","type":"User","site_admin":false},"created_at":"2016-02-07T21:14:32Z","updated_at":"2016-02-07T21:14:32Z","body":"@VikingsFan Can you please also add the following where you have request.GetOwinContext().Response?\r\n\r\n//// in case you are NOT using Owin, uncomment the following and comment everything above\r\n// HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.NotFound;\r\n\r\nMy project wasn't using Owin and I was building OData prototype."}}