Search code examples
asp.netwcf-data-servicessql-server-2012-express

Can't access a database from a WCF Data Service hosted in an ASP NET App


I built a WCF Data Service hosted in an ASP NET App, this Data Service offers access to a SQL Server DataBase.

I also built an app that accesses the service, i.e., accesses the SQL Database through the service.

I created a Login in SQL Server for NT Authority\Network Service with a default schema of dbo.

(Actually, I created it for NT Authority\Servicio de red, Network Service is "Servicio de red" in english)

I also executed the following in Sql Server:

ALTER LOGIN [NT AUTHORITY\Servicio de red] 
WITH DEFAULT_DATABASE=[MiniNorthwind]; 
GO
EXEC sp_addrolemember 'db_datareader', 'NT AUTHORITY\Servicio de red'
GO
EXEC sp_addrolemember 'db_datawriter', 'NT AUTHORITY\Servicio de red'
GO 

But when I run the app, I get the following error>

enter image description here

The error is produced when the app tries to iterate over a table (Line 48):

var customerOrders = new DataServiceCollection<Order>(ordersQuery);

What could it be?

Rafael


Solution

  • You should set UseVerboseErrors from the DataServiceConfiguration object passed to InitializeService in your service class to true.

        public static void InitializeService(DataServiceConfiguration config)
        {
            // Don't let this go into prod
            config.UseVerboseErrors = true;
        }
    

    You should then receive a message more helpful than "An error occurred while processing this request".

    Note: I always get them mixed up, so you might need to use the ServiceBehavior attribute to set IncludeExceptionDetailInFaults to true as well. You assign this attribute to your service class.

    [ServiceBehavior(IncludeExceptionDetailInFaults=true)]
    public class FooService : DataService<FooServiceContext>