Asp.Net EF Code First continuous integration with VSTS - Azure Sql Db not deploying - asp.net-mvc-5

I am having trouble getting migrations to run to create / update my Sql Db hosted on Azure. They run fine locally to my LocalDb but seem to be completely ignored when releasing to Azure.
Details:
- Asp.Net MVC 5.2.3
- Entity Framework 6.1.3
- Visual Studio Team Services (online)
- Azure Web App & Azure SQL Db (under Imagine / Dreamspark subscription)
I created a different application that didn't use CI, but used the Publish feature within Visual Studio and it published fine and worked. I have since removed that web app & db from Azure since the subscription only allows one db.
I'm thinking it may be something I'm missing in setting up the build definition in VSTS, I'm just not sure what.
I have tried:
https://blogs.msdn.microsoft.com/webdev/2014/04/08/ef-code-first-migrations-deployment-to-an-azure-cloud-service/ (this did nothing for me)
https://www.benday.com/2016/11/07/using-tfs-build-to-deploy-entity-framework-database-migrations-with-migrate-exe/ (I received a build error that I didn't have a release definition in my bin folder)
... as well as a bunch of others (I don't have the windows open any longer or I'd link them too).
My current build steps include:
NuGet restore - NuGet Installer
Build solution - Visual Studio Build
Test Assemblies - Visual Studio Test
Publish symbols path - Index Sources & Publish Symbols
Publish Artifact - Publish Build Artifacts
In my local env, EF works when I Enable-Migrations, Add-Migration, Update-Database. I'm guessing none of the above steps do this so I need one or more steps. Ben Day's blog seems like it should work, but for some reason, it's not finding my bin/release file.
The actual application deploys to Azure just fine upon release of the above successful build definition. It's just that the database is completely ignored. Using SSMS, I check the Azure db, and while it exists, it has none of my tables or data.
What am I missing?
Thanks.
**Edit - found the simple item I was missing. I need to add the connection string for the db to the web app in Azure. It now has my initial tables, and stores data using those tables, but doesn't pick up migrations. So something I've done in the last two days got my initial tables out there. Now.. to remember what it was.
**2nd Edit - So, I think the tables made it into the release db before I fixed my connection string in VSTS. One of the things I tried was creating a separate database project. I think when I pushed that at some point, the tables in existence at that time also pushed to release, I just couldn't see them because I hadn't connected the application and db together? At any rate, EF migrations are still not being recognized. I tried Ben Day's suggestion again, but I"m getting the build error:
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
I am using EF 6.1.3

I ran into this as well. I was using Visual Studio to publish my Azure app service. After doing some research (https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application#deploy-to-azure), I came to learn that VS Publish transforms your web config when you check the box for "Update Database" in Publish setting, adding a connection string that refers to the database containing your _migrations table, as well as a "contexts" element under "entityFramework". You can see what a previous publish has done with your web.config by looking at "..obj\Release\InsertEFCodeFirstDeploy\transformed\web.config".
So to recreate this same action when using a build and release definition in VSTS, I added 2 XDT "insert" transforms in Web.Release.config in my service project. Mine looks like this:
<?xml version="1.0"?>
<!-- For more information on using Web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=301874 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="MS_TableConnectionString_DatabasePublish" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=AzureStorageEmulatorDb45;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True"
providerName="System.Data.SqlClient" xdt:Transform="Insert" />
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
<entityFramework>
<contexts xdt:Transform="Insert">
<context type="helpmeshopService.Models.helpmeshopContext, helpmeshopService">
<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[helpmeshopService.Models.helpmeshopContext, helpmeshopService], [helpmeshopService.Migrations.Configuration, helpmeshopService]], EntityFramework, PublicKeyToken=b77a5c561934e089">
<parameters>
<parameter value="MS_TableConnectionString_DatabasePublish" />
</parameters>
</databaseInitializer>
</context>
</contexts>
</entityFramework>
</configuration>
Next, you will need to go to your App Service "Application Settings" in the Azure portal and add a connection string with name, "MS_TableConnectionString_DatabasePublish", and set its value to the same value as for "MS_TableConnectionString" that should already exist in your settings.

Related

Azure Deployment Issue: Unhandled Exception: Could not load file or assembly 'Microsoft.Azure.DocumentDB.Core

I was having performance issues with table storage and upgraded to the latest library/sdk. Everything works fine locally and I can run on the emulator. However, when I try to deploy to my Azure Cloud Service I get the following error:
Details: Recovering role... Unhandled Exception: Could not load file or assembly 'Microsoft.Azure.DocumentDB.Core, Version=2.11.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
The web role just recycles constantly with this error. The actual dll version is 2.11.6.0. Things I have tried:
I have logged on to the web role and checked the dll is the expected one (2.11.6.0).
bindingRedirects: all relevant projects have a binding redirect of this form:
<dependentAssembly>
<assemblyIdentity name="Microsoft.Azure.DocumentDB.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.11.6.0" newVersion="2.11.6.0" />
</dependentAssembly>
I have done a text search through all files in my project for Microsoft.Azure.DocumentDB.Core - every reference (that mentions version) references 2.11.6.0. This includes files of this form dll.config file - which I did not manually edit but which do have the correct redirect.
I tried changing the Azure role osFamily to 6 (it had been 5)
I tried deleting the packages folder and regenerating
I tried deleting all redirect statements and allowing Visual Studio to automatically generate them for me.
The publish is done via 'publish' in Visual Studio 2019 on the Cloud Service (csdef).
Could anyone suggest what else I can try to deploy this cloud service?
Eventually found a way round this. So, when run locally a file WebRoleName.dll.config is produced alongside the web.config. This file is not deployed. The content is an exact copy of the web.config file. All its settings are ignored apart from the redirects. The redirects are used, seemingly, just while the web role is being started up. So, I added the file based on my local configuration (so completely the wrong database settings, etc.) to the root of my Web Role project and set it to Content - Copy Always.
Now everything runs OK - obviously it would be better if this generated file was deployed by the Visual Studio Azure publish automatically.

EscapeTextForRegularExpressions error while webdeploying to azure

In my asp.ent MVC4 project, I am using webdeploy to deploy to azure and I deployed this project to the app service on azure several times before. but out of a sudden I have this error.
"EscapeTextForRegularExpressions" task was not given a value for the required parameter "Text"
I Have found an answer:
Add this file into the web project in its root folder.
File Name : *.wpp.targets
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ExcludeFromPackageFolders Include="Internal">
<FromTarget>*.wpp.targets</FromTarget>
</ExcludeFromPackageFolders>
</ItemGroup>
</Project>
Never the less I cannot understand the real cause of the problem
According to your description, I found a similar issue noted the IncludeIisSettings within *.csproj. Per my test, if I specified the <IncludeIisSettings>true</IncludeIisSettings> with my csproj, then when I use the Web Deploy method from the Publish wizard of VS, I could encounter the similar issue as follows:
After remove the IncludeIisSettings or set the value to false, then I could deploy my web application to azure web app successfully.
I have another answer maybe can help someone:
You must open the Solution explorer then right-click the project, select properties, on Package/Publish Web check your Configuration is on Release and not on Active (Debug) in the drop-down, this was the solution in my case.

Error WAT200: No default service configuration "ServiceConfiguration.cscfg" could be found in the project

After installing Visual Studio Enterprise 2017 (Version 15.1 (26403.7)) my Azure Cloud Service does no longer build and gives me the 'Error WAT200: No default service configuration "ServiceConfiguration.cscfg" could be found in the project'. Although it still works fine with Visual Studio Enterprise 2015. (The right TargetProfile is also specified...)
I ran into this last night.
It actually happens when you rename Cloud Service configurations away from Cloud and Local.
In my case, I had deleted the Cloud configuration and created a new, differently named one.
By ensuring that the configurations are called Cloud and Local, the Cloud Service projects will build again using "Resharper build", and (most likely) under "Lightweight solution load".
The configurations can be renamed by right-clicking one of the service roles under the Cloud Service node in Solution Explorer, choosing Properties, then opening up the "Service Configuration" dropdown and clicking <Manage...>.
Now, in the resulting dialog, the configurations can be renamed:
The following bug report got me on the right track:
https://youtrack.jetbrains.com/issue/RSRP-450390
The best way is to change the service configuration prefix in ccproj of your project, add yourprojectname.configurationname
Dot is the life saver.
<ServiceConfigurationPrefix>yourproject.ServiceConfiguration</ServiceConfigurationPrefix>
Add these in itemgroup:
<ServiceDefinition Include="yourproject.ServiceDefinition.csdef" />
<ServiceConfiguration Include="yourproject.ServiceConfiguration.cscfg" />
Make sure you have renamed your cscfg files from the folder it resides.
Reload
Bingo. It works.
Ok I found it, after disabling the 'Lightweight Solution Load' (which in VS 2017 u can disable by right click on the Solution and then Disable Lightweight Solution Load), the error disappears and the Cloud Service builds again.
Similar to Rishikesh mentioned, but I added a service config entry
<ServiceConfiguration Include="ServiceConfiguration.cscfg" />
to the ccproj. Then copied a configuration, e.g. Production and named it ServiceConfiguration.cscfg. Then reloaded the project. That got the ReSharper to build, but MORE importantly for me at the moment was to figure out how to the get Azure DevOps pipeline to build the solution with multiple Classic WebRole projects in it. That was failing for the SAME reason.

VS 2013 Azure Publish hangs with last Pubxml file

If I publish to Azure Cloud Service using the Cloud Project and follow the publish wizard from it works fine but only if I delete the last Pubxml file (stored in the profile folder) first.
If I try and publish when a Pubxml file already exists it will hang trying to navigate between wizard steps.
Using SDK V2.9.6, although had the same issue on previous versions.
I have multiple subscriptions, and the issue seems to be whilst the Pubxml does store the subscription, it is ignored and the default (first one in the list alphabetically) is used instead.
So if I run a publish where there was a previous Pubxml, it jumps to the Diagnostics page. I click next for the summary and the issue areas are highlighted with a red error indicator (it has the wrong subscription selected by default, and hence it cannot find the correct cloud service). However I cannot click back as it just hangs.
I'm using SDK V2.9.6, and didn't see this problem. Actually, my pubxml files have subscription information included.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AzureCredentials>{"ServiceManagementEndpoint":"https:\/\/management.core.windows.net\/","ResourceManagementEndpoint":"https:\/\/management.azure.com\/","SubscriptionId":"my-subscription-id"}</AzureCredentials>
...
</PropertyGroup>
</Project>
Which version of Azure SDK is your pubxml files generated from?

VS 2012 MVC Web App Publish fails when Precompilation set

I have an MVC 5 web application that I'd prefer to save time with startup by pre-compiling on publication. However, when I choose "Precompile during publishing", I get the following Error:
Error 5082 Could not load file or assembly 'DotNetOpenAuth.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246'
or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).
error ASPRUNTIME 0 0 USIS
The Web.Config Does have a binding redirect for this Reference
<dependentAssembly>
<assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780CCD10D57B246" culture="neutral" />
<bindingRedirect oldVersion="1.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
This builds and deploys fine if I do not have this precompile option checked, and the Web is working fine without issues. I just cannot publish with this precompile option checked.
I'd really like to have the website compile all views and etc only on publish, and never recompile in the live production site. Some Documentation I was looking at was suggesting this was the propose of these features... but I'm having no luck.
EDIT: I have had some success. I realized that I was purposely not deploying the Web.Config file such that a developer doesn't accidently harm the production environment configuration, but it appears that the precompile build was copying files to a temporary location, and without the Web.Config file being deployed, no Web.Config file was there, meaning no Dependency translations.
So, Now it looks like I'm going to have to deploy the Web.Config if I want to pre-compile (I had set its Build Action from "Content" to "None", and now I've set it back again.) This means I'm going to need to look into Web.Config Transformations, or etc.
Thanks,
Greg
The only thing precompilation does is compile the views. The fact that this happens only when precompilation is set means that there's something in one of your views that is generating this error. That's kind of irrelevant, though.
The best way I've found to correct this particular error is to uninstall and reinstall the offending Nuget package.
Just go into the Package Manager Console, make sure the project that generates the error is selected for "Default Project", and then run:
> Uninstall-Package DotNetOpenAuth.Core -Force
> Install-Package DotNetOpenAuth.Core
That should resolve the issue and allow your site to publish and run fine, with or without precompilation.
Solution:
It turns out that since I wasn't publishing the Web.config file, that the binding redirects were not being made. The solution was to change this file back to being published (Build Action from "None" to "Content"), and then the pre-compilation was succeeding.
For others with an MVC or ASP.NET web site that runs slow each time hitting a new page/view, I'd recommend giving this pre-compilation feature with publishing a try. It's just hidden under file options in the publication settings (VS 2012.) I chose to Precompile, and to make the site not-updatable to avoid the dynamic view/page compilations. See for more information: What effect does the new precompile during publishing option have on MVC4 applications? .
Also, to push the correct settings and connection strings to Live and Test I started using the Translation Templates "Web.Release.config" and "Web.Debug.config". These will swap out Web.config lines when deploying only. See http://go.microsoft.com/fwlink/?LinkId=125889 for more information.

Resources