set ASPNETCORE_ENVIRONMENT does not seem to be working - deployment

I have followed along a book example and built an application called Sportstore (Pro ASP.Net Core MVC by Adam Freeman).
Now I am on the deployment chapter and he deploys to Azure.
I have created the databases on Azure, opened the firewall and opened a database with Visual Studio so I can see the Azure DB through the Visual Studio 2017 SQL Object Explorer. Pretty cool.
I have an appsettings.production.json file with the Azure Connection String.
So the author has us open a command prompt. He also shows the powershell commands but I just went to the command prompt.
I type:
set ASPNETCORE_ENVIRONMENT=PRODUCTION
I don't get an error here but I also don't get any kind of confirmation. I believe I would get some type of message here if it worked.
Then I type:
dotnet ef database update --context ApplicationDbContext
Now I get the message:
No migrations were applied. The database is already up to date.
This is telling me it still thinks it is in development.
Is there a command to check what environment it is in?
I know you can set the system environment and reboot. But this seemed like you don't have to reboot.
Is this command the same as going to the Windows Environment variables dialog and changing it there. I know you have to reboot with that.

Yes that will set the environment variable ASPNETCORE_ENVIRONMENT to PRODUCTION. You can see what environment variables are set by just running
set
in command prompt. You will get an output like:
C:\Users\username>set
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\username\AppData\Roaming
ASPNETCORE_ENVIRONMENT=DEVELOPMENT
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=mycomputername
ComSpec=C:\WINDOWS\system32\cmd.exe
...

Related

Restore x64 NuGet packages via PowerShell

I've got .NET 7.2 WEB API project, which was build with x64 Visual Studio 2022 v17.2 - v17.4. Now I try to set up CI/CD pipeline, for what I use .yml with following code:
- 'C:\nuget.exe restore server.csproj'
- '& "C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild" server.csproj'
Constantly getting error:
error NETSDK1064: Package Microsoft.EntityFrameworkCore.Analyzers, version 7.0.2 was not found. It might have been deleted since NuGet restore. Otherwise, NuGet restore might have only partially completed, which might have been due to maximum path length restrictions.
What I've tried is:
I enabled Win32 long paths as it suggested here
I've created new WeatherForecast project and new repo with name tst, so path certainly cannot be shorter
Nothing gives any success
As far as I understand, the problem is that I use x32 NuGet CLI with x64 VS 2022 builder. Bit depth does not match. As far as my project uses .NET 7, I cannot use VS 2019. Also I cannot find x64 NuGet CLI - official site provides only x32 versions
I need some way to restore packages and then build the project from YML file. What surprised me is that I can sequentially run all commands from .yml in cmd.exe and as a result I will get build with no errors. I tried to $ whoami prior to any of .yml command and it gives me that it's SYSTEM user, which by the way got full access to IIS's wwwroot folder, but the error triggers prior deployment step, so I have no clue how it could be related
I'm stuck, got no idea what to do, but guess that solution is quite simple, I'm to blurry now. SOers please help me
Solution
Great thanks to #mu88!
In accordance to his suggestions yml took that form:
- 'dotnet restore service.csproj -v n'
- 'dotnet service.csproj'
And everything works as it supposed to
After some investigation (see the comment section) we found out that using dotnet build instead of msbuild solves the problem.

Problems setting the ServiceName of a Windows Service

I have created a Windows Service via Visual Studio 2019.
The Visual Studio project's solution is (for the purposes of this question):
ProductSyncService
So it was no surprise to find that the name displayed in Services.msc was this.
I would like to change it to:
Product Sync Service
I first set the Service Name property of serviceInstaller1, but that didn't work:
so I then tried setting it via the code as well (belt & braces), but that is not working either:
Despite doing a 'Clean' and then a rebuild and a re-install, Services.msc still shows it with the concatenated name.
I should add that I'm using Advanced Installer to create the MSI and that it has no references to the concatenated name.
I eventually found that there is indeed ServiceName property within Advanced Installer that was overriding the values in my Visual Studio project.
Easy to change and it's working fine now.

How to Deploy a .Net web app to Azure using Powershell

I have a "Web app" in Azure to which I deploy/publish a .Net web application using Visual Studio. (Build --> Publish), and it works.
I want to be able to deploy/publish my application using a Powershell script. I got the following script to work for the build portion:
CMD> "c:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe" WebApplication1.sln
To make it also deploy, I need to add a few parameters:
CMD> "c:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe" WebApplication1.sln /p:DeployOnBuild=true /p:PublishProfile="C:\Users\jgodse\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1\Properties\PublishProfiles\jg-7-web-app-1 - Web Deploy.pubxml" /p:Configuration=Release
I got an error:
Build FAILED.
"c:\Users\jgodse\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1.sln" (default target) (1) ->
"c:\Users\jgodse\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1\WebApplication1.csproj" (default target) (2) ->
(MSDeployPublish target) ->
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.targets(4295,5): msdeploy error ERROR_USER_UNAUTHORIZED: Web deployment task failed. (Connected to the remote computer ("jg-7-web-app-1.scm.azurewebsites.net") using the Web Management Service, but could not authorize. Make sure that you are using the correct user name and password, that the site you are connecting to exists, and that the credentials represent a user who has permissions to access the site. Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_USER_UNAUTHORIZED.) [c:\Users\jgodse\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1\WebApplication1.csproj]
0 Warning(s)
1 Error(s)
I am obviously missing my Azure credentials (seeing as Visual Studio was able to capture them), and I am also not running in Powershell.
So I took the entire command and put it into a file called deploy.bat, opened up a Powershell window and did the following:
PS> Login-AzureRmAccount
(I typed in my user/password in the GUI popup).
PS> cmd /c .\deploy.bat
The build was fine, but I got the same error when trying to publish. I guess that the Azure credentials did not carry through when shelling out to the CMD program.
How do I use Powershell to call MSBuild on my .Net project to publish to an Azure web app?
You can use your existing .pubxml file but you need the password in order to be able to deploy. There are several ways to get it. The most obvious one is to get it from the portal by navigating to the blade of you Web app and then clicking on "More" and finally on "Get publish profile"
This file contains all sorts of data but the one that you need is called userPWD - this is the password that you need to use. Copy the password and add it to your MsBuild command:
CMD> "c:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe" WebApplication1.sln /p:DeployOnBuild=true /p:PublishProfile="C:\Users\jgodse\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1\Properties\PublishProfiles\jg-7-web-app-1 - Web Deploy.pubxml" /p:Configuration=Release /p:Password="Value of userPWD"
Obviously storing this value in your build scripts is not recommended. What you could do is download the publish settings using Powershell (Get-AzurePublishSettingsFile), extract the userPWD value, pass it to MsBuild to publish your app, and finally clean everything up.
There are various ways to implement you build infrastructure and what I have proposed might not be the best solution for you but it is up to you to experiment and decide what works best for you.
Some resources:
Automate Everything (Building Real-World Cloud Apps with Azure) - This one uses ASM instead of Resource Manager but you can easily tweak it to use ARM
Using Windows PowerShell scripts to publish to dev and test environments
Hope this helps.

How do I change an entity framework project to use Microsoft SQL Server

I am trying to debug a program using Entity Framework code first on my personal (work) computer.
We have recently had a domain migration, meaning that the user I log in as now is not the same that I used before. This caused me to loose access to the databases I had on the computer. To get around this, I have uninstalled everything to do with Microsoft SQL Server on the computer, and installed the latest version of Microsoft SQL Server, 2014 - 12.0.4213.0 . I then restored the database I need.
When I first tried to run the program, Visual Studio complained that the project is set up to use SQL Server Express, which was not installed. The recommended solution is to change the project to use SQL Server instead. To do this, I must click on "the database file" and follow the instructions. I have looked through the entire solution. There is a great many files, but I found no good candidate for "the database file."
It seems that my Google fu is not strong enough to find anything about this. So my question is: how do I change the project to use SQL Server?
I also have a second, related question. I tried to solve the problem by installing SQL Server Express. However, when I try to restore the database to this, no base appears in the drop down list. When I try to run the program now, I get another error:
Unable to create the file 'c:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Timelønsblanket.mdf' because it already exists.
I guess that this is also why I cannot restore the database. What I have found in websearches warns that I should not manually delete .mdf files.
Any advice on what to do?
I have solved the problem. All that was needed was a correct connection string. No need to find a "database file".

TFS to VisualStudio Online Migration - Access to directory is denied

I'm trying the new OpsHub TFS to Visual Studio Online migration tool and have run into a snag. After successfully setting up the migration configuration, I begin the migration.
On the first changeset of the migration, I receive the error:
"OH_SCM_009: Error occurred while sync. Access to the path 'O:\w15_2\ProjectName'"
Where ProjectName is the name of my project.
Through deletion and recreation of the project in VSOnline, I found that the folder 'w15_2' would change, an indication that this path (and subsequent access failure) is in VSOnline. I have confirmed the user I am using for migration is a project collection admin, project admin, and project collection service account (per the OpsHub help page).
I'm not really sure where to go from here... any ideas how to get around this access denied error?
The error indicates that the utility does not have ability to write at location at :\Program Files\OpsHub Visual Studio Online Migration Utility\TFS_Temp which is used by the utility for temp work space. (It is aliased to O drive). Please check to make sure that this location is writeable.