简体   繁体   中英

ODataController erroring after return

I have an OData service that is implemented with several MVC controllers using ODataController. I am having an issue with all but one of the controllers, where an Internal 500 error is being returned with nothing of help after my return statement:

/// <summary><see cref="ODataController" /> reacting to queries relating to <see cref="Contract" /></summary>
[CustomExceptionFilter]
public class ContractsController : ODataController
{
    // GET: odata/Contracts
    [EnableQuery]
    public IQueryable<Contract> GetContracts()
    {
        return DataAccess.GetContracts();
    }

    ... other methods
}

/// <summary>Single point of reference to access data</summary>
public static class DataAccess
{
    /// <summary>Gets the queryable collection of <see cref="ContractCoverageDetail" /></summary>
    /// <returns>The queryable collection of <see cref="ContractCoverageDetail" /></returns>
    public static IQueryable<Contract> GetContracts()
    {
        IQueryable<Contract> results = null;

        using (EntityFrameworkContext context = new EntityFrameworkContext())
            results = context.Contracts.ToArray().AsQueryable();

        return results;
    }
}

Another controller using the same DataAccess class returns data just fine. All that is being returned for each other controller is:

<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <m:code/>
    <m:message xml:lang="en-US">An error has occurred.</m:message>
</m:error>

The error appears to be raised after my return statement, and if I step through after the return (F10), I hit each individual { get; } property on the returned entity of the collection, after which a result with the above error is returned to the browser. I can't get actual error information (innererror) to appear for the life of me, and it's odd that one controller is working, while the remainder fail without any detail.

Does anyone have an Idea for what might be causing this, or how to turn on the error detail after the return statement?

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

in the global.asax.cs does not help, nor does either of the following in web.config:

<system.web>
  <customErrors allowNestedErrors="true" mode="On" />
</system.web>

<system.webServer>
  <httpErrors existingResponse="PassThrough" />
</system.webServer>

Any idea how I can get to the actual exception being raised?

It is hard to say exactly what the cause of this issue is but here are a couple things you can try. Hopefully, one of these will get you more information on your exception.

Debugging all code
It may seem obvious but make sure that you are not debugging only your code. You can check this by accessing the debugging options under the Debug -> Options and Settings... menu. Make sure that Enable Just My Code is not checked. This might be fine usually. When there are errors with exceptions and you cant seem to get to them, sometimes this setting can mess you up

Debugging Exceptions
Another thing that can be both useful and painful at the same time are the Debug -> Exceptions... settings. These are sets of exceptions that can be configured to cause Visual Studio to automatically break allowing you to see their moving parts. For debugging MVC and .NET in general you would want to enable the Common Language Runtime Exception.

With debugging exceptions turned on, you will probably get a ton of exceptions that you wont want. Just ignore them and keep running till you get to exceptions that relate to oData.

Here's the simple guide about how to debug the Web API OData and ODL lib.

http://odata.github.io/WebApi/10-01-debug-webapi-source/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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