Deployed API returning bad requests status code - c#

I developed a simple API using .Net core and it locally works as intended. After deployment only the WeatherForecast endpoint is working and the rest results in status 500. I have used code first with EF and enabled migrations during the publish process through VS code.
Any suggestion?
Azure gives me these errors but I am not really familiar with Azure
image 1
my code
endpoint
EDIT: The endpoints are working now, but as I said in a comment below I changed my local db connection string (see my appsettings.json in git) by the one that azure gives me and everything is working correctly but, if you have alredy filled you db with many records probably that's not the entire solution

Looking into your code, I see that you have added connection string of the localdb. Did you update this in configuration on app service?
I am pretty sure that your application is unable to connect to the database and hence the 500 : internal server error
Also you should add appsettings to .gitignore so that it is not pushed to a remote public repository.

Related

API Returns EF Core records locally, but not when hosted in Azure

I have a web app (api) in .NET 5 (core). It accesses an Azure DB and has dozens of tables that all work well. However, 1 api request always returns 0 records, but only when the api is hosted in an Azure app service. When it runs locally, in debug or release, it works as expected.
The web request never returns errors, all 200's, but no records.
Other queries of the same table produce expected results.
When I put logging in after the db call [a simple ctx.MyTable.Where(i => id > 0)] and it logs that no records are ever returned.
When I run the api locally (still using the Azure DB), it always works.
I've tried Debug and Release.
I've changed the Azure app service to .NET 5, .NET 6 (Early), 32-bit, 64-bit.
I've updated all NuGet packages to the latest.
Let me add, I've logged the DB connection string in local and host, debug and release, it's always the same.
This ones really got me. It's a big project so I can't show code...but I can't even think of scenarios that could induce this sort of behavior.
It's hard to say without checking the code or error whether it's a database problem or a code first problem. Try changing the entity framework connection string. Also try requesting your data in list rather db object (if you are doing) using -
public IEnumerable<Conversation> GetDBname()
{
return db.DBname.ToList();
}
Make sure you use the Azure connection (SQL Database connection strings for ADO.NET) in the Web.config file.
<connectionStrings>
<add name="MyAzzureConnection" connectionString="paste the connection string from Azure here" />
</connectionStrings>
Check this Deploy an ASP.NET app to Azure with Azure SQL Database document and Using Web API 2 with Entity Framework 6 for more information.
You should also check this Web API implementation - Best Practices Microsoft document for clarification.
OK, I got it! Somewhere down the calling tree a date was being used where the time was not trimmed off in the query, but was in the logging. So it looked from the logs like the date being used was the same on Azure as it was local. In fact, it was n-timezones different between the two.

.Net Core WebApp API Endpoints don't work once deployed to Azure

I'm having an issue with deploying a WebApp / API to Azure.
When I run it locally on localhost:44385//api/user/getuser/1, it returns the JSON for that user,
once deployed to an App Service on Azure, it starts returning a 404.
Its running on the free version as its for a uni assignment.
I've spent hours poking around and reading, and cant seem to find where i've gone wrong in the setup.
Hoping someone might of had a similar issue.
Have you checked to make sure the server's routing rules are correct and that it's not looking in the directory structure?
https://learn.microsoft.com/en-us/azure/application-gateway/url-route-overview#:~:text=In%20this%20article&text=URL%20Path%20Based%20Routing%20allows,to%20different%20backend%20server%20pools.
Please verify below points:
That the Connection String of DB is correctly picked up by your Web API/it's correctly setup and configured.
If the DB and table/collection is present when you query and if not then you are creating it through your code.
If your data is present at the right place (User with ID = 1 in DB and collection/table you are querying).
If your code is not throwing 404 incorrectly (even in positive scenarios).

Connection string in application settings doesn't work when https is enabled: Azure Web App

I have a .net core web app that works well in local environment. However, I got this exception when I publish it to Azure Web App:
Then I found this article and did the same approach as the image shows:
However, it fails once the setting is applied.
The difference between the tutorial and my web app is that my one has a SSL certification to enable https connection. Do I need some extra management to make the web app get the connection string under this condition?
EDIT:
I saw #lolops's post and found there was a right coonection string in kudu environment. So I published it again... and it worked. It's odd and I don't know why the problem has been gone.
What I did specially was only restarting my PC. Could this be a solution? Anyway sorry for troubling.
Try visiting https://YOURPROJECT.scm.azurewebsites.net and find "Environment variables", search for your connection string and compare to the variable you use. It will be prefixed with the type of connection you chose (SQLAzure).

C# EF Code First Deploy to Godaddy

I am having issues deploying my app to godaddy hosting using EF code first. I dont want my app to create the database since the database already exists.
So I read some posts on here and placed this in the constructure:
Database.SetInitializer<myContext>(null);
Now I get a permission error writing to the temp file.
Anyone had a successful experience deploying this to godaddy shared hosting?
When the project used Model first and edmx files it was deployed fine.
I was able to get this to work right after posting this..
All I had to do was move:
Database.SetInitializer<myContext>(null);
to the global.asax Application_Start
Can you post what's the server error you are receiving.
setting db initializer to null should work fine. I have tried that. If you post the exception, we can probably find out why exactly that happening
You should use a generated database from the development and remove the database initializer from your code. From my experience, I always got a problem if I didn't remove the database initializer.
I've faced kind of problem before. I am using a different hosting site though. I deployed my MVC 3 Code First app back then. When I deployed the app, I didn't remove the database initializer, so the app drop the database and try to create it again. But when it create the database, it got a permission error. The problem is, the database is actually dropped in the server but in the hosting site cpanel, it is still exist. I send a ticket to the support, and problem solved.

WCF Service Application and MVC4 Application hosted on local IIS (not express) throwing connection string erro

There are already a handful of questions out there but none seem to have gotten any attention or answers to what is causing this error, and how to fix it.
Background
I have created an ASP.NET MVC4 application that has a single point of data access in another application I created using the WCF Application project template in VS2012. If I run both applications in debug mode (so it is using IIS express at this point for both) they can talk to each other just fine. All my calls work, no issues.
I wanted to test it in a more realistic environment so I got my IIS set up (IIS 8 on a windows 8 box), created a website, and underneath that website I am hosting both these applications, the MVC one, which I am calling Dashboard, and the WCF one which I am calling Service. So it looks something like this in IIS
MYMACHINENAME
|
--App Pools
|Sites
--local
--aspnet_client
--dashboard (application, MVC4)
--service (application, WCF)
I can hit the localhost/service and I see the right information there, so I know it is hosting correctly.
I updated the service reference in the MVC4 project to point to localhost/service rather than the iis express one I was using.
The Problem
Adding the new service reference to the MVC4 app worked fine, I got the wsdl info and I can see all the endpoint calls I defined. After adding the new service reference, I re-published the 'dashboard' application. When I navigation to localhost/dashboard, I get an error deep inside the Reference.cs file that the service reference created.
Here is the code that is throwing the error:
MyServiceAPIClient svc = new MyServiceAPIClient();
svc.Open();
User userRecord = svc.GetUserRecord(adSAMName);
On that third line it gets into the Reference.cs file that is created when I add the service reference. This line of code it dies on in the Reference.cs file is here:
public Dashboard.Web.MyApiService.User GetUserRecord(string userName) {
return base.Channel.GetUserRecord(userName);
}
And the error message is this:
An error occurred while getting provider information from the database.
This can be caused by Entity Framework using an incorrect connection string.
Check the inner exceptions for details and ensure that the connection string is correct.
There is no inner exception for me to look at, so I am at a loss here.
The Question
Should be pretty obvious what the question is here. Multiple questions really:
What could be causing this issue? Keep in mind it works fine in IIS express, just debugging both in VS2012.
Is this an issue with the configuration of my MVC app, WCF app, or IIS? A combination of them all?
How do I fix it??
There is a lot of code files and config files I can post here, but I wanted to keep it shorter for reading sake, if there is code that you would like to see here, let me know if the comments and I can post it.
Update
I did as advised and attached to the process running the service in IIS. Digging into the inner exceptions it seems to be an access/login issue with trying to hit the database.
I can't seem to get the connection string to work with a built in SQL user though, so I am scratching my head again.
This is the connection string I am trying:
<add name="MyDB" connectionString="Server:.;Database=MyDatabase;User Id=SQLAuthUser;Password=yourmom;" providerName="System.Data.SqlClient"/>
I run the process again and it tells me login failed for that user I specified, and I am also getting this message that I have never seen before:
Could not determine storage version; a valid storage connection or a version hint is required.
Halp!?
Update: Solution
So my connection string is now this:
<add name="NeumontDB" connectionString="Server=ServerName; Database=NeumontDB; User Id=Username; Password=password;" providerName="System.Data.SqlClient"/>
And I also needed to set sql server to allow mixed mode authentication. Now I seem to be able to hit the service without issues.. Hooray!
The problem lies in your WCF app, and is most likely a config related. To fix this, I would recommend that you attach debugger to your WCF hosted in IIS and debug the GetUserRecord method.
Here are some tips for how to debug WCF service hosted in IIS
Debug WCF service hosted in local IIS not working
debugging asp.net WCF service hosted in IIS
What I suspect is that you are using windows auth in your connection string, and the user owning the app pool does not have access to the Database. If your debugging reveals that that's the case, I would recommend that you assign DB rights to apppool user or change the connection string to use SQL authentication
Now for the connection string error, I would recommend that you go through the following blog entry.
http://failuresincoding.blogspot.com/2011/12/entity-framework-provider-did-not.html
Also, Do you have multiple SQL Servers on the local box? I would recommend that you use the complete instance name instead of "."

Categories

Resources