Having trouble linking to my Azure DB - asp.net

I'm publishing an ASP.NET MVC 5 app to Azure Websites. In my Azure portal, I linked the website to my Azure SQL database. However, when I click the "View Connection String" link on the portal, I get a popup that says there are no connection strings.
Please keep in mind that my connection string in my app have a different name i.e. it's not named DefaultConnection, rather it's named myAppNameConnection.
Also, I'm publishing the app directly from VS 2013 and I'm able to select the correct DB in the publish wizard. However, when the app is published it is NOT able to communicate with the database. Any idea what may be causing this issue?

In your VS tool you have the right connection string configured in your Web.config file.
When you publish the website in Azure, it overwrites automatically the connection strings in the web.config file with the ones stored as global variables in the website service.
You must go on the Azure portal, click on your website, then click on configure on the tab menu.
Go down to connection strings, and insert the right connection string there.

Related

ASP.NET Core - Application not connecting to database after publishing

I created a simple ASP.Net Core application with user authentication (so all the Entity Framework has been preloaded into the web app template). It is connecting to my database with the connectionString that is located in my appsettings.json file, with "data source = {computerName}\\{serverName}" setup.
The database instance and Visual Studio are located on the same machine. This works fine in returning data to the web api while within development and debugging mode. When I publish it and try to go to the site from a domain name it does allow me to view the webpages that are not needing database connection but the rest that need data from the database returns pages like this:
Error.
An error occurred while processing your request.
Development Mode
Swapping to Development environment will display more detailed information about the error that occurred.
Development environment should not be enabled in deployed
applications, as it can result in sensitive information from
exceptions being displayed to end users. For local debugging,
development environment can be enabled by setting the
ASPNETCORE_ENVIRONMENT environment variable to Development, and
restarting the application.
I have tried to change the Environment Variables for ASPNETCORE_ENVIRONMENT from Development to Production to no success.
I have tried adding appsettings.Production.json to publishOptions in my project.json file, even though there is no file appsettings.Production.json, and this did not help. dotnet publish
Adding the evironment variable in the web.config file did not work Deployment
I need help getting the published web api to connect to my SQL Server database from outside the development Visual Studio setup.
The last thing I can think of is that maybe I am incorrect in how I understand the connection string. If the web api uses the connection string to connect to the database from the server-side then it should work just fine like it does when in development calling to http://localhost:port# since it's all on the same machine. But, if the database string needs to be based on a client-side call then it would have to be with domain names and IP addresses.
Can someone tell me which one it is?
The only thing else that comes to mind is that there is something I am not doing, and need to do, inside of IIS Manager. I see connect string there as well but unsure what that is for our does since the connection string is inside the application. Also maybe I am suppose to give the app some kind of authorization to communicate with the database server even though they are on the same machine???
After much research, finally googling "how to deploy a web api in iis" I was able to learn from Host ASP.NET Web API in IIS using Visual Studio Publish that I needed to add the security entity BUILTIN\IIS_IUSRS. Then placing a mapping to my database tables and giving db_datareader (and possibly db_datawriter) for the database to the IUSER to allow access from my self hosted IIS web api. This from the above mentioned link with the part stating
Accessing Database under IIS APPPOOL\ASP.NET v4.0
As we are using ASP.NET v4.0 App Pool make sure IIS APPPOOL\ASP.NET v4.0 is added to your Database Server -> Security -> Logins.

Can't Access SQLite DB on Azure Web App

I have created an ASP.NET MVC web app which uses an SQLite database under EF6.
The app works fine in VS. The database is included in the project in the App_Data folder. When I publish to Azure (or an IIS test server on the LAN) I get an error. The Azure instance is showing an HTTP 500 error, and it only happens on views which require database access to load. Other than that it works fine. The IIS test server shows an error saying unable to open database file. I tried loosening file permissions on the .db file on the IIS instance, but nothing changed. I'm not sure how to go about doing anything on the Azure instance.
Can someone please assist me with debugging a SQLite connection problem on Azure/IIS?
Details
VS 2015 Enterprise
Azure app services (free tier, production server)
SQLite3 database
IIS on Windows Server 2012R2 (test server)
I have tried all sorts of combinations of connection strings:
setting a connection string to the D:/xxx/xxx path in Azure Portal
modifying the connection string in the deployment profile in VS (replaces value in web.config upon deployment)
just leaving it all the same
Nothing has worked or even changed the error. Perhaps one of those configurations is able to access the file but simply can't open it (which is sort of what the IIS error suggests, I think). Even then, how do I go about finding out?
EDIT
On the IIS test server I discovered that the entire problem was that the connection string in Web.config was wrong. I manually edited it on the server and it fired right up. I'm still not sure how to correct the path on the Azure server, nor how to correct my deployment settings in VS to inject the correct path. Thoughts along those lines are still appreciated.
EDIT 2
I've also found that each time I hit a page requiring DB access, I see this error show up in the application logs on Azure:
2017-01-03T20:25:32
System.ApplicationException: The trace listener AzureBlobTraceListener is disabled. ---> System.InvalidOperationException: The SAS URL for the cloud storage account is not specified. Use the environment variable 'DIAGNOSTICS_AZUREBLOBCONTAINERSASURL' to define it.
at Microsoft.WindowsAzure.WebSites.Diagnostics.AzureBlobTraceListener.RefreshConfig()
--- End of inner exception stack trace ---
After some abysmally slow debugging sessions, I finally remembered that in toying around I had set a connection string value in the Azure portal.
Apparently whatever value is set in the Azure Portal overrides (at runtime?) the values in the Web.config file
I simply deleted the connection string from the Azure Portal (your app/Application Settings/Connection Strings) and it worked. The final connection string looks like this:
<add name="yourThingEntities" connectionString="metadata=res://*/cadb_SQLite.csdl|res://*/cadb_SQLite.ssdl|res://*/cadb_SQLite.msl;provider=System.Data.SQLite.EF6;provider connection string="data source=D:\home\site\wwwroot\App_Data\yourSQLiteDBFile.db"" providerName="System.Data.EntityClient" />
To get the Web Deploy working in VS, you'll need to edit the publish profile to have a connection string matching the format
data source=D:\home\site\wwwroot\App_Data\yourSQLiteDBFile.db
Apparently not many people use SQLite for web apps, and most people should not. If you ever expect more than one user for your website at a time, you shouldn't fix SQLite, you should convert to SQL Server CE or just full-blown SQL Server which Azure will take care of for a paltry $5/mo. Choose wisely!

Where is the Azure Web App database?

I been following a Microsoft tutorial on Azure Web Apps:
https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-deploy-aspnet-webforms-app-membership-oauth-sql-database/
I created a Web App on Azure then created the website in VS 2013 on my PC with authentication all working. I then tried to publish the website onto Azure. When I go through the publishing steps, I do not get a connection string. I have tried waiting (24 hours) and looked around the app to see if there are any settings, but I cannot find a way to get a connections string. Everything other than the login-in works.
I know I selected a free 20Mb database, but I cannot see any sign of one on the Azure portal. I created a second Azure Web app and it is exactly the same.
Where would I find the Azure app database (in the Azure Portal) used for storing the username & password? Any idea how I can get the login to work in the Azure cloud?
So long you shouldn't have to wait. I just tried it and had the free DB after two minutes.
You find all SQL Server databases incl. the free one in the SQL Server section. That's the icon on the left with the container and the "DB" in front. There you should find your new database. Click on it, switch to the dashboard and on the right you find the section "quick glance". Click on the link "Show connection string" and copy from there what you need.
Btw, you should not be able to create a second free SQL DB. If you're allowed to create "second" free SQL DB, than there is no first one. That would definitely mean there went something wrong in the first place.
While creating the website you would have specified the database server, db name etc. Under the sql db section this db will be listed. If you have done that way. then migrate the db that you have locally to azure using the migration wizard and then provide that azure sql db connection string in the web.config ( from where the connectionstring is used.) then the application should work fine.

Deploy the default MVC 4 template to windows Azure

I am very new to Windows Azure and am tinkering with it to learn how it works. To keep things as simple as possible, I am trying to deploy the default MVC 4 template that is generated by VS 2012 Express for web. I created a new project - an Azure cloud service. I selected an MVC 4 web role. And that creates a nice little asp.net application with home, about and contact pages, and links to register and login. I just want to deploy this site to Azure and see it work on the cloud.
However the membership does not appear to work on azure. The pages of the application appear correct. However when I click the 'Register' link it does not take me to a page to nter a new username and password. I do not believe the membership tables are being generated anywhere either.
Considering how simple this is, I think I must be making a silly mistake somewhere. I would really appreciate if someone could help me with this.
Also, for the record, I have noticed that if I create a stand alone MVC 4 project in VS2012 Express (as opposed to a Windows Azure Cloud service project with an mvc 4 web role) then when I right click that MVC4 project and choose to publish it, it allows me to specify a sql database using a connection string. What is more is once it is published to Azure, the membership works perfectly. I can create useraccounts and log in. If I examine my SQL Azure database using the azure portal, I can see the new membership tables created there.
But when I try to publish a Windows Azure Cloud service with MVC4 webrole, I do not see any option to provide a connection string of any sort to the azure project. Maybe that is why the membership is not working... since there is no database linked with the web role... but I cannot figure out how to specify this. I was under the impression that publishing a stand alone MVC4 project to Azure, and publishing an Azure Cloud Service with an MVC4 Web Role is the exact same thing, please correct me if I am wrong. And either way, I should be able to see the membership functionality of my mvc 4 web role.
Here are the steps to recreate what I have. Basically it is all default stuff:
Create a new project in VS2012 Express
File -> New Project -> 'Windows Azure Cloud Service'.
On the next screen add an ASP.NET MVC 4 Web Role to it.
On the subsequent screen choose 'Internet Application'.
These 3 steps create a new project. It runs locally just fine.
Now I try to put it on the cloud.
Right click on the windows azure project in solution explorer and choose 'Publish'.
Choose the subscription (I already have certificate/credentials in place to publish to azure from VS so I select that)
Create a cloud service on the next screen (I name it 'garyazurecloudservice').
Accept all the default settings in the Publish wizard and click Publish.
The publishing process takes a few minutes.
The web application can be viewed at http://garyazurecloudservice.cloudapp.net/.
The Home, About and Contact pages all display correctly. However it I click the 'Register' link at the top right I see a webpage with the correct layout template, but an error in red that says:
"Error. An error occurred while processing your request."
I have tried to create an SQL database on Azure but I don't see any way to connect this role to it. Possibly that is the problem? Or maybe it is something else altogether. I would appreciate if anyone can tell me what is wrong.
I have spent the entire day googling and going in circles with no success. Thank you for any help you can provide.
Gary
You're right in that the Membership section of your website would not be able to create the SQL tables in the background when running on Azure, due only the problem of where those tables would reside.
Remember, that Azure hosting space is different from traditional hosting, in that your site would exist on a virtual machine, with files being replicated to another virtual machine if the first were to experience problems. Also, what would happen if you were to invoke a second 'server' to host your website, to cater for a load, perhaps? Where would the tables live then, and how would the second virtual machine know how to access the first?
The answer would be to create a SQL Azure database, upload your tables there, and then you'd have a permanent place for 1, 2 or 1000 'servers' that are running your website to access a central database repository of your data.
I know in your example, it should just 'work' and I agree to some extent, but your application running on your local machine or with your existing (traditional) web host is quite different to the Azure model of working.
In terms of your question regarding how the SQL Azure set-up works, presumably you've created this through the Portal, via the www.windowsazure.com website? If so, then you should be able to see and manage your SQL Azure database from there, including gaining access to the Connection Strings section, whereby you would copy and paste these into your web.config file for your MVC4 application. From there, you can utilise this database pretty much as though you would any other.
I hope this all helps - Azure is a beautiful beast once tamed.

IIS7 application using wrong SQL Server 2008 Express database

I'm stumped. I have a client site on a virtual private server - Windows Web Server 2008 SP2, IIS7, SQL Server 2008 Express.
I'm trying to setup a second web application, to allow him to review updates prior to their going live. I've created the web application in IIS7, and I have added a second database to SQL Server. The second db is essentially a copy of the production db, with 'DEV' prefixed to the database name and a few new fields in a few tables.
My production site works fine. However, the test site comes back with an SqlException: "Invalid column name 'version'." This is one of the new fields - which leads me to believe that my dev site is referring to the production database, and not the dev database. Connection strings, however, do point to different databases (although the login is the same for both):
Production site: "Server=.\SQLExpress;Database=myDbName;User ID=myUserName;Password=myUserPassword;"
Dev site: "Server=.\SQLExpress;Database=DEVmyDbName;User ID=myUserName;Password=myUserPassword;"
Seems like this should be something obvious that I'm missing. A colleague suggested that I create another application pool for the second IIS application, but it does not seem to have helped.
UPDATE, following further testing:
I changed the dev site's connection string to
"Server=.\SQLExpress;Database=DEVmyDbName;User ID=devuser;Password=myNewDifferentPassword;"
and created a new SQL server login on the database, and only gave it access to the dev database. I also set the dev database as the account's default database. I removed access to the dev database from the production login. When I try to login to the site, I get the following error:
The server principal "devuser" is not able to access the database "myDbName" under the current security context.
As a result, there are two things I know for certain:
Since it's using the new username to login, I know the app is using the connection string I'm feeding it;
For some reason, once logged in, it appears to be ignoring the specified database and trying to use the production database instead.
I don't think it should make a difference, but I will mention I'm using NHibernate for database access.
Still stumped.
It turns out that NHibernate was significant. One of the NHibernate configuration options that was set was "default_schema" - when I removed it, the app started connecting to the correct database.
It appears that the default_schema property was overriding whatever database was specified in the connection string.

Resources