locked
ASP.Net MVC Deployment - Browser Error 500 RRS feed

  • Question

  • User1779487221 posted

    I am developing an ASP.Net MVC web application and have been publishing it on my own web server as the project develops, allowing my client to review progress. Up until now there have been no problems.

    I am now working on my Web API including Entity Framework, data repository and data context and moving from local, in memory, data to accessing the data from a SQL Server database. On my development computer I have created a LocalDb database and my initial simple code to access this database and retrieve some data builds and runs with no errors.

    I published this latest version and I get a the following displayed in my browser along with a 500 error: 

    An error occurred while starting the application.

    .NET Core X64 v4.1.1.0    |   Microsoft.AspNetCore.Hosting version 1.1.0-rtm-22752    |    Microsoft Windows 10.0.14393    |   Need help?

    When I deployed this version I copied the IndigoDb.mdf and IndigoDb_log.ldf file into the server Administrator's AppData folder and manually edited the connection string to be as follows:"Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|\IndigoDb.mdf;Integrated Security=SSPI;MultipleActiveResultSets=True"If I comment out the line in my HomeController Index() method, which calls my repository to get some data, and publish the solution everything works fine so it does appear that when the data context tries to connect to the database it causes the error.I feel sure I have made a simple newbie error here, but I just cannot see it. Any advise would be very welcome.

    Monday, September 18, 2017 11:59 AM

Answers

  • User753101303 posted

    Hi,

    And SQL Server Express is installed on the server ? Also if using Integrated Security the account under which the app runs needs to have access to the db (while on a dev machine the code runs under the developer account and so the same account is used to connect to the db).

    Anyway it's best to take the habit to start from an actual error message rather than trying to guess. Try https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling to implement a way to track server side exceptions so that you can start from an actual error message rather than trying to guess what happens...

    BTW you have a dedicated https://forums.asp.net/1255.aspx/1?ASP+NET+Core forum in which you'll have likely better support.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 18, 2017 4:46 PM
  • User2053451246 posted

    The account under which the app runs - is this the user account which I login to my server with (i.e. Administrator)? If so can you explain how I check that this account has access to the database.
    Open IIS, expand your server name, and click Application Pools.  Select the pool your application is using and click Advanced Settings... at the right.  In the Process Model section you will see the user that IIS is running your app as.

    To see which app pool your app is using, select your site's folder.  Then click Advanced Settings... at the right.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 18, 2017 6:13 PM

All replies

  • User753101303 posted

    Hi,

    And SQL Server Express is installed on the server ? Also if using Integrated Security the account under which the app runs needs to have access to the db (while on a dev machine the code runs under the developer account and so the same account is used to connect to the db).

    Anyway it's best to take the habit to start from an actual error message rather than trying to guess. Try https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling to implement a way to track server side exceptions so that you can start from an actual error message rather than trying to guess what happens...

    BTW you have a dedicated https://forums.asp.net/1255.aspx/1?ASP+NET+Core forum in which you'll have likely better support.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 18, 2017 4:46 PM
  • User1779487221 posted

    Yes, SQL Express 2016 is installed on the server. I also installed SQL Server Management Studio and have since moved the database and attached it to the default SQLExpress instance and then changed the connection string to reflect this change. Using SSMS I can open and explore the database with no problems.

    The account under which the app runs - is this the user account which I login to my server with (i.e. Administrator)? If so can you explain how I check that this account has access to the database.

    I will implement the error handling suggestion you kindly made and see what information this displays.

    Thanks

    Monday, September 18, 2017 5:16 PM
  • User2053451246 posted

    The account under which the app runs - is this the user account which I login to my server with (i.e. Administrator)? If so can you explain how I check that this account has access to the database.
    Open IIS, expand your server name, and click Application Pools.  Select the pool your application is using and click Advanced Settings... at the right.  In the Process Model section you will see the user that IIS is running your app as.

    To see which app pool your app is using, select your site's folder.  Then click Advanced Settings... at the right.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 18, 2017 6:13 PM
  • User1779487221 posted

    That's clear thanks. My application is running under ApplicationPoolIdentity.

    Monday, September 18, 2017 6:47 PM
  • User1771544211 posted

    Hi TDC,

    Please try enable the detailed error page in your IIS settings, so that it will show detailed error message about the 500 error which may be helpful to troubleshoot your issue.

    The following link shows how to enable the detailed error page on IIS, please take it as reference.

    https://blogs.msdn.microsoft.com/rakkimk/2007/05/25/iis7-how-to-enable-the-detailed-error-messages-for-the-website-while-browsed-from-for-the-client-browsers/

    Best Regards,

    Jean

    Tuesday, September 19, 2017 5:51 AM
  • User1779487221 posted

    Good information thanks.

    My application's data context obtains the SQL connection string from a 'config.json' file which I have created. The json content of this file is as follows:

    {
      "ConnectionStrings": {
        "ScheduleContextConnection": "Data Source=TDCDEVELOPMENT\\SQLEXPRESS,1433;Initial Catalog=IndigoDb;Integrated Security=SSPI;MultipleActiveResultSets=True"
      }
    }

    In my Startup.cs file I create a new ConfigurationBuilder

    var builder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath);

    ...and add the json file

    builder.AddJsonFile("config.json");

    In my data context I configure the DbContextOptionsBuilder

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer(
                    _config["ConnectionStrings:ScheduleContextConnection"]
                    );
    
                base.OnConfiguring(optionsBuilder);
            }

    This works fine on my development computer when accessing data from a LocalDb database file (using a different connection string in the config.json file).

    Last night I tried running the application on the server in Kestral by running 'dotnet applicationName.dll'. I received an error pointing to the connection string. I originally used '.\SQLExpress' for the server, but this caused a json parsing error. I replaced this with 'SERVERNAME\\SQLEXPRESS' and I now receive a different error.

    Now when I run my application from the command line using 'dotnet applicationName.dll' I get the following error:

    Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler[1]
    An exception occurred in the database while iterating the results of a query.
    System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)

    So it looks as if I cannot connect to my SQL database. If I run SQL Server Management Studio (SSMS) I can open my database. I have checked server properties that 'Allow remote connections' is ticked.

    My database on my development computer, created using an Entity Framework initial migration, is a LocalDb file. I copied this and its log file to my server and using SSMS attached it to my development SQL Server.

    Any thoughts very welcome as to why I receive the error.

    Tuesday, September 19, 2017 2:01 PM
  • User1779487221 posted

    As I continue to resolve this problem I have made some advances.

    On my remote server I opened SQL Server Configuration Manager. In SQL Native Client 11.0 Configuration > Client Protocols > TCP/IP Properties I ensured the default port was set to 1433. In SQL Server Network Configuration > Protocols for SQLEXPRESS > TCP/IP in the Protocol tab I enabled it and in the IP Adresses tab > IPAll I set TCP Port to 1433.

    I restarted my server. At the command prompt I changed directory to the website folder (as configured in IIS) and ran 'dotnet Indigo.dll' again and this time the website was published and I could see the database being accessed successfully. A step in the right direction.

    I launched Google Chrome on the server and went to http://localhost:5000 and the website ran fine. The website doesn't run in IE, I will have to look into this later.

    All is not finished though. I still cannot access the website from a remote browser. Ensuring that in IIS the website is running, the Application Pool is set to 'No Managed Code' and the Identity is set to 'ApplicationPoolIdentity' when I try to access the website remotely I still get an error 500. If I point my IIS website to an earlier version of the application (before use of SQL) I can access it fine from a remote browser.

    Tuesday, September 19, 2017 5:58 PM
  • User1771544211 posted

    Hi TDC,

    TDC

    I launched Google Chrome on the server and went to http://localhost:5000 and the website ran fine.

    It seems that the database connection problem is fixed and now the problem is that you can't access this website remotely.

    Have you tried to set the port 5000 for remote connection and access the website use http://ip address of the server:5000 from remote?

    It turns out that you are using ASP.NET Core now, so please try the following methods to see if you can get more detailed error message about the 500 error.

    1. Change stdoutLogEnabled="false" to true and then check the logs at stdoutLogFile=".\logs\stdout". The error(s) there might tell you something.
    2. Check that you set up right Environment Name using ASPNETCORE_ENVIRONMENT environment variable as so use correct settings like connection string. On your machine by default you have "Development" environment.

    3. You may use Error Handling middlewares for showing exceptions like app.UseDeveloperExceptionPage();

    Best Regards,

    Jean

    Wednesday, September 20, 2017 7:31 AM
  • User1779487221 posted

    Hello Jean

    Thank you for your clear advise. I changed my stdout configuration, as you suggested, created a .\logs folder and restarted my server. Now when accessing the website I get the log file which is a great help.

    The log file reads: Login failed for user 'WORKGROUP\TDCDEVELOPMENT$'. There is an error number: 'Error Number:4060,State:1,Class:11'.

    The log file points to a method on my startup.cs file which seeds my database.

    If I change my Application Pool Identity to 'ApplicationPoolIdentity' I get a different error: Login failed for user 'IIS APPPOOL\Indigo'.

    Should I change my SQL Express to SQL Server Authentication and set a username and password?

    Wednesday, September 20, 2017 9:52 AM
  • User1779487221 posted

    Possibly cracked it.

    1. In IIS > Application Pools I selected the Application Pool for my application and set the Identity to Network Service.
    2. In SQL Server Management Studio (17.2) I connected in to the server, selected the server in the Object Explorer and opened the Security tab.
    3. In Security > Logins I selected 'NT AUTHORITY\NETWORK SERVICE' and right-clicked to select its properties.
    4. In the left pane I selected User Mappings. In the right pane I then ticked the database I wanted to grant access to and at the bottom I ticked db_owner (public was alrerady ticked). Clicked OK.

    I restarted the web server and then accessed my website from an external browser and everything worked fine. I hope this helps someone else. Thank you to those who helped point me towards this.

    Wednesday, September 20, 2017 11:37 AM
  • User1771544211 posted

    Hi TDC,

    I restarted the web server and then accessed my website from an external browser and everything worked fine. I hope this helps someone else. Thank you to those who helped point me towards this.

    I'm glad to hear that you have find the solution to your issue and thanks for your sharing.

    Best Regards,

    Jean

    Thursday, September 21, 2017 1:13 AM