Visual Studio

Migration from ASP.NET Core 2.1 to 2.2

On December 4th .NET Core 2.2 including ASP.NET Core 2.2 and Entity Framework 2.2. In this post, I will be taking one of the projects used in the ASP.NET Basics series and converting it from ASP.NET 2.1.x to the new 2.2 version of ASP.NET Core. This will all be based on the official 2.2 migration guide.

The code before any changes can be found here. In the sample solution, this guide will be working with the Contacts project only.

Installation

Head over to the .NET download page and download the new version of the .NET Core SDK for version 2.2 which is available for Window, Linux and Mac.

After installation is done run the following command if you want to verify the SDK is installed.

dotnet --list-sdks

You should see 2.2.100 listed. If you are like me you might also see a few preview versions that would be good to uninstall.

If you are using Visual Studio make sure you are on at least version 15.9. If not updates can be downloaded from here.

Project File Changes

Right-click on the project and select Edit projectName.csproj.

Change the TargetFramework to netcoreapp2.2.

Before:
<TargetFramework>netcoreapp2.1</TargetFramework>
After:
<TargetFramework>netcoreapp2.2</TargetFramework>

Update any Microsoft packages with a version to 2.2.x the following is an example.

Before:
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" PrivateAssets="All" />

After:
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" PrivateAssets="All" />

If you want to use the new IIS in-process hosting model you also need to add the following line to a property group.

<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>

The following is my full csproj for reference.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
    <UserSecretsId>aspnet-Contacts-cd2c7b27-e79c-43c7-b3ef-1ecb04374b70</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
  </ItemGroup>

</Project>

Startup Changes

In Startup.cs update the compatibility version to enable the new 2.2 features.

Before:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

After:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

Wrapping Up

As with the migration to 2.1 the move to 2.2 is really easy to do. Make sure you check out the official migration guide for more details that may have not been covered by this project.

The code in its final state can be found here.

Migration from ASP.NET Core 2.1 to 2.2 Read More »

Visual Studio Missing Scaffolding for ASP.NET Core

This morning I set out to try the Scaffold Identity option that was added as part of the ASP.NET 2.1 release. The docs for this feature is really good so I didn’t think I would have any issue, but I was wrong.

Sample Application

To start I used the .NET CLI to create a new application using the following command.

dotnet new razor

I then opened the project in Visual Studio. Then following the docs I right-clicked on the project and expanded the Add option, but the New Scaffolded item is missing.

Sample Application 2

Since the project created using the CLI didn’t have the option I thought I would try creating a new project in Visual Studio. From in Visual Studio using File > New > Project.

Under Visual C# > Web select ASP.NET Core Web Application and click OK.

Select Web Application and then click OK.

After the creation process finishes I right-clicked on the project and expanded the Add option and the New Scaffolded item is there.

What’s the difference?

After trying a lot of different things I finally got to the point of looking at the csproj files for both projects. Here is the csproj from the Visual Studio create project.

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
  </ItemGroup>
</Project>

And the csproj from the CLI created project.

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0-preview3-35497" PrivateAssets="All" />
  </ItemGroup>
</Project>

Notice that the CLI created project is targeting .NET Core 2.2 which is in preview at the time of this writing. Not surprisingly Visual Studio has some limits on what can be done using preview bits.

Wrapping Up

I can’t believe that I let having preview bits installed cause me issues again. I’m guessing that most people aren’t installing the previews so hopefully, this isn’t an issue most of you will have to deal with.

If you do want to use the CLI to create an application targeting a specific version of .NET Core it can be done using a global.json file. Check out Controlling .NET Core’s SDK Version for more information.

Visual Studio Missing Scaffolding for ASP.NET Core Read More »

Deploying an ASP.NET Core Application to DigitalOcean

This is the fourth post in a series on deploying a very simple ASP.NET Core application different cloud provides. This post is going to be dealing with setup and deployment to DigitalOcean.

Google Cloud Platform
Amazon Web Services
Microsoft Azure
DigitalOcean (this post)

Sample Application

The sample application ended up becoming its own blog post since it looks like the easiest deployment to DigitalOcean for .NET is going to be using Docker. Check out my Adding Docker to an ASP.NET Core Application post to get your application setup with Docker and published to a registry.

DigitalOcean

Head over to DigitalOcean and sign up for an account. Using that link will get you $10 in free credit (I will get some credit too if you spend $25 or more, but the main point is to get you some credit to get started with).

As part of the sign-up process, you will have to enter a credit card. It won’t be charged as long as you don’t go over your free credit.

After you finish the sign-up process you will be dropped on the dashboard for your account. Click the Create Droplet button to get started.

On the next page select One-click apps, and then the Docker option.

Scroll down and select your Droplet size. Since this is a basic example and will never have any real traffic I went with the cheapest option which is $5 a month.

There are a lot of options, but with the two selections above we can take the defaults are the rest of them for this sample. Scroll all the way down and click the Create button.

This will drop you back to your Dashboard and you will have your new Droplet listed. Click on the name of the Droplet to get to its details.

There is a lot of information on this page, but the thing we are interested in is the Console link in the top right of the summary.

A new browser window should open with a console that is running on your Droplet.

If you are like me and took the defaults when creating your Droplet then you will have gotten an email from DigitalOcean with the username and password you can use to log in. You will be forced to change your password on your first log in.

Installing the sample application

Now that our Droplet is up and we are logged in it is time to get our application up and running. All of this section is going to be happening in the console of the Droplet. First, use the following command to log in with Docker.

docker login

Next, we need to pull the image for our application (your image name will be different).

docker pull elanderson/testrepository

The following command can then be used to run the application. -p 80:80 in the command is binding port 80 from the container to port 80 on the host. Hopefully, if you took the defaults on everything this will just work for you, but if not you will tweak this part of the command.

docker run -p 80:80 elanderson/testrepository

Wrapping Up

There were some details to work out, but after getting over those humps Docker is amazing. This post was about DigitalOcean, but I could take the image used in this post and deploy it to any Linux image that supports ASP.NET Core and Docker. Hopefully, I will find an excuse to play around more with Docker soon.

Deploying an ASP.NET Core Application to DigitalOcean Read More »

Deploying an ASP.NET Core Application to Microsoft Azure

This is the third post in a series on deploying a very simple ASP.NET Core application to the major cloud provides. This post is going to be dealing with setup and deployment to Microsoft Azure.

The following is are the other posts in this series.

Google Cloud Platform
Amazon Web Services
Microsoft Azure (this post)

Sample Application

This is the same as the first post in the series, but am including it in case you missed that post. The sample application that we will be deploying is the basic Razor Pages applications created using the .NET CLI. The following commands are what I used to create the application, create a solution, and add the project to the solution. I ran all these commands in a CloudSample directory.

dotnet new razor
dotnet new sln
dotnet sln add CloudSample.csproj

Microsoft Azure

The rest of this post is going to be based on the official Create an ASP.NET Core web app in Azure documentation. I already have all the Azure related stuff installed and have used it before, so it will be much easier for me to miss a step. If that happens please leave a comment and I will get the post fixed.

Azure Development for Visual Studio

The additional tools need to use Azure from Visual Studio are part of the Visual Studio installer. I’m assuming that Visual Studio is already installed. To add Azure select the Tools > Get Tools and Features menu.

After the installer launches close all instances of Visual Studio. From the list of features select Azure development and click the Modify button to start the installation.

Sign up for Azure

While the tools are installing is a good time to sign up for Azure if you don’t already have an account. You can sign up here. This is one of the parts that doesn’t work well when you already have an account, but the link above should guide you through the process.

Application Publication

Hope back in Visual Studio and open the solution you want to publish. In the Solution Explorer window right-click on the project and select the Publish menu option.

This will show the publish target dialog where you can select what you want to publish to. In this example, we are going to be publishing to an App Service. The details area select Create New option. Then click the Publish button to move to the next screen

The next step collects the information needed to Create an App Service. The first thing to set is the account in the upper right corner of the screen. This could already be set based on how you are logged in to Visual Studio. Next, click the New link next to the Resource Group and enter the name you want to use. Then click the New link next to the Hosting Plan.

In the Hosting Plan dialog, you get options to give the plan a name, pick your hosting location, and the size. For the Size, I recommend starting with the Free option. After all the options are set click OK.

This will return you to the Create App Service dialog where you can click the Create button to deploy the application. After deployment finishes the site will open in your default browser.

Wrapping Up

For me using Azure from Visual Studio is the simplest experience of all the cloud providers I have tried so far. I don’t guess that is surprising since Microsoft controls both products. Forgetting the integration with Visual Studio it seems that Azure App Services, Google App Engine, and Amazon Elastic Beanstalk are all on par with each other.

As always don’t forget to shutdown/delete the associated resources for your test project to avoid changes.

Deploying an ASP.NET Core Application to Microsoft Azure Read More »

Deploying an ASP.NET Core Application to Amazon Web Services

This is the second post in a series on deploying a very simple ASP.NET Core application to the major cloud provides. This post is going to be dealing with setup and deployment to Amazon Web Services (AWS).

The following is are the other posts in this series.

Google Cloud Platform
Amazon Web Services (this post)
Microsoft Azure

Sample Application

This is the same as the first post in the series, but am including it in case you missed that post. The sample application that we will be deploying is the basic Razor Pages applications created using the .NET CLI. The following commands are what I used to create the application, create a solution, and add the project to the solution. I ran all these commands in a CloudSample directory.

dotnet new razor
dotnet new sln
dotnet sln add CloudSample.csproj

Amazon Web Services

The rest of this post is going to be based on a couple of pieces of documentation from Amazon for the Visual Studio Toolkit and Beanstalk with .NET Core. I will be walking through the whole process but wanted to make sure you had access to the same information this post is based on.

AWS Toolkit for Visual Studio

Amazon provides a Visual Studio extension to aid in interactions with their platform from Visual Studio. To install open the Tools > Extensions and Updates menu. Select Online from the left side of the dialog and then search for AWS and select AWS Toolkit for Visual Studio 2017.

Click download, close Visual Studio to trigger installation, and restart Visual Studio after the extension’s installation has completed. When you open Visual Studio back up you should see the AWS Getting Started page with has this link to the IAM Users page in the AWS Console. This will land you on the following page where you have to either sign into your account or sign up for a new account.

After you get through the sign-up/in process you will be at the IAM console where we are going to add a new user by clicking the Add user button.

On the next page fill out the User name you want to use and check the Programmatic access check box and then click the Next: Permissions button.

The next page is all about what permissions the user will have which is handled via groups. Since my account doesn’t have any groups I need to create one using the Create group button.

On the Create group form enter the Group name you want to use. Next, use the search box for Policy type to find the policies related to Elastic Beanstalk. I have selected the full access policy for this sample, but if it is more than just a test this would require more research to see if that is the proper policy to use or not. When done click the Create group button.

This will return you to the group page from above. Click the Next: Review button to continue. The next page is just a review of the options selected on previous pages. Click the Create user button to continue.

The last page has a button to Download .csv, click it and download the file. Back over in Visual Studio on the AWS Getting Started page click the Import from csv file.

Browse and select the csv download above. Then click the Save and Close button on the AWS Getting Started page. After that is complete you should see the AWS Explorer window.

Sign up for AWS Elastic Beanstalk

If you notice in the AWS Explorer screenshot above the Please sign up for this service under AWS Elastic Beanstalk. Double click the Please sign up for this service it will open a new browser window. On this window click the Get started with AWS Elastic Beanstalk button.

The next page will prompt for payment information. After entering your information click the Secure Submit button. The next step is an automated phone call to verify that you who you say you are. The final step in signing up is to select your plan.

Select the Free plan to finish your sign up.

Publishing from Visual Studio

Now that sign up is complete hope back in Visual Studio and open the solution you want to publish. In the Solution Explorer window right-click on the project and click Publish to AWS Elastic Beanstalk.

This will open the Publish to Amazon Web Services dialog. I just took the defaults since this is a new application and I don’t have any existing enviroments. Click the Next button to continue.

On the next screen, an Application Name and Environment Name are needed. Fill them in and click Next.

The next screen is where you can configure the type and size of the EC2 instance that will back your application. I used the defaults. As always click Next when ready.

The next screen allows you to configure permissions. You will never guess, but I used the defaults and clicked Next.

The next screen allows you to pick your project build configuration and framework to use. The defaults are set based your project so they should be right. Click the Finish button.

The final screen is to review all the options. If you are happy with everything click the Deploy button.

During deployment, I saw the following error in my output window.

Caught AmazonIdentityManagementServiceException whilst setting up role: User:myUser/CloudSampleBlog is not authorized to perform: iam:GetInstanceProfile on resource: instance profile aws-elasticbeanstalk-ec2-role
Caught Exception whilst setting up service role: User: myUser/CloudSampleBlog is not authorized to perform: iam:PutRolePolicy on resource: role aws-elasticbeanstalk-service-role

This turned out to not really be an issue. I went to my Elastic Beanstalk Dashboard and the application was there and in the process of deploying. The whole process took around 5 minutes. Unlike with Google, I did have to find the URL in the dashboard instead of a browser being opened to the URL automatically.

Wrapping Up

Initial setup with AWS is much more complex than with Google. Redeployment was in the two-minute range which is much faster than what I saw with Google. Overall it seems that Elastic Beanstalk is on par with Google’s App Engine.

Don’t forget to shut down and/or delete your test project when you are finished to make sure you don’t get charged.

Deploying an ASP.NET Core Application to Amazon Web Services Read More »

Deploying an ASP.NET Core Application to Google Cloud Platform

This is the first post in what is going to be a series on deploying a very simple ASP.NET Core application to the major cloud providers starting with Google’s cloud. This series was inspired by a question from one of my coworkers about the ease-of-use differences for .NET between the cloud providers.

The other posts in the series:
Google Cloud Platform (this post)
Amazon Web Services
Microsoft Azure

Sample Application

The sample application that we will be deploying is the basic Razor Pages applications created using the .NET CLI. The following commands are what I used to create the application, create a solution, and add the project to the solution. I ran all these commands in a CloudSample directory.

dotnet new razor
dotnet new sln
dotnet sln add CloudSample.csproj

Google Cloud Platform

The rest of this post is going to be based on information from Google which can be found here.

Google Cloud SDK Installation

The first step is to download and install the Google Cloud SDK which can be found here. I’m walking through these steps for Windows, but the previous link has instructions for Linux and Mac as well.

After downloading and run the installer, and be patient it takes a while to run. When it is done you should see the following screen.

Make sure and leave the bottom two boxes checked as gcloud init must be complete to get everything working as it should. Click the Finish button and a console will open and walk you through the initialization process.

The first step in the process will open a web browser and ask you to login to your Google account. After logging in you will be asked for a cloud project to use. It should look something like the following.

You are logged in as: [your account].

Pick cloud project to use:
 [1] api-project-276206909805
 [2] blogbackup-141901
 [3] oval-botany-94217
 [4] Create a new project
Please enter numeric choice or text value (must exactly match list
item):

We want to select Create a new project which is option 4 in my case. When prompted for a project ID I just hit enter and it let me out of the process so I don’t think a project is actually required at this point so that is a little confusing.

Google Cloud Tools for Visual Studio Installation

Google provides a Visual Studio extension that makes interacting with their platform simpler from within Visual Studio. To install open the Tools > Extensions and Updates menu. On the left select Online and then search for Google. Next, select Google Cloud Tools for Visual Studio.

Click download, close Visual Studio to trigger installation, and restart Visual Studio. Now that the extension is installed we need to connect it to an account. Using the Tools > Google Cloud Tools > Manage Accounts menu to launch the Manage Accounts Dialog. Next click Add Account.

This will launch a web browser where you will need to complete the account add. After the account auth completes Visual Studio should show the Google Cloud Explorer window logged in the account you selected.

Create an App Engine Project

Open App Engine and click Create.

On the next page, you will need to give the project a name and then click Create.

On the next page select .NET as your language.

And select the location you want your application hosted in and click next.

If you aren’t already signed up then the next step signs you up for a 12 month trial with $300 worth of credit. Click the Signu up for free trial button to continue.

On the next screen, for the trial, you have to select your county as well as agree to the terms.

The next screen is a summary of your account. Review the information and click the Start my free trial button to continue.

Deploy from Visual Studio

In the Google Cloud Explorer window in Visual Studio select the project you just created in the drop-down.

If you don’t see your new project try clicking the refresh button in the upper left-hand side of the window. Now that you have the correct project selected switch over to the Solution Explorer window and right click on your project and select Publish to Google Cloud.

On the next screen select App Engine Flex.

Then on the next screen click publish.

My first deployment took a really long time so don’t worry if you end up waiting a while (5 to 10 minutes). I tried a second deployment and it took just as long so I guess that is just how long it takes. When deployment finished it should open your site in a web browser.

Wrapping Up

After the initial set of installs and setup, Google Cloud with ASP.NET Core is pretty straightforward. The deployment time seems way too long, but that is really the only complaint I have.

Don’t forget to shut down and/or delete your test project when you are finished to make sure it doesn’t use up your credits.

Deploying an ASP.NET Core Application to Google Cloud Platform Read More »

Migration from ASP.NET Core 2.0 to 2.1

On May 30th .NET Core 2.1 was released including the corresponding version of ASP.NET Core 2.1 and Entity Framework Core 2.1. In this post, I will be taking one of the projects used in my ASP.NET Basics series and converting it from its current 2.0.x version into the newly released 2.1 version. The starting point for the code can be found here. This is all based on the official migration guide.

If you are looking at the sample solution it is the Contacts project that this post is going to be dealing with.

Installation

Thankfully the download page is much improved over the last time there was a release. Head here and to download and install the .NET Core 2.1 SDK. It is available for Windows, Linux, and Mac. The link should drop you on the appropriate page for your OS.

After installation, open a command prompt and run the following command. If all worked well you should see version 2.1.300 listed.

dotnet --list-sdks

If you are on Windows make sure and install at least Visual Studio 2017 15.7.

Project File Changes

Right-click on the project and select Edit {projectName}.csproj from the menu.

First, change the TargetFramework to netcoreappp2.1.

Before:
<TargetFramework>netcoreapp2.0</TargetFramework>

After:
<TargetFramework>netcoreapp2.1</TargetFramework>

The other required changes it to move away from the Microsoft.AspNetCore.All package to the version-less Microsoft.AspNetCore.App package.

Before:
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />

After:
<PackageReference Include="Microsoft.AspNetCore.App" />

I did a bit more digging and it turns out that the project file can be greatly simplified from the version I had for this application. The following is the full project file will all the bits that were not required removed and the two changes above already made.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <UserSecretsId>Your-Secrests-ID</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.0" PrivateAssets="All" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
  </ItemGroup>

</Project>

Main Changes

There have been changes to how what the Main function looks like to better allow for integration tests. This is the original code in the Program.cs file.

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

And the following is the new version.

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

Startup Changes

Startup.cs has one change that is required which is the removal of the following line from the Configure function.

app.UseBrowserLink();

In the ConfigureServices function if you want to use the new features in 2.1 change the services.AddMvc() to set the compatibility version. This allows you to upgrade the version of the SDK without having to change your whole application since you have to opt into the version you want to target.

Before:
services.AddMvc();

After:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

If you check out the official migration guide they also point out how to enable a couple more features such as HTTPS and some things to help with GDPR. Neither of these is needed in this application so I’m skipping them in this guide.

Identity Changes

I had to make one change in ManageLogins.cshtml to get my project to build because of a rename/removal AuthenticationScheme to DisplayName.

Before:
<button type="submit" class="btn btn-default" name="provider" value="@provider.AuthenticationScheme" title="Log in using your @provider.DisplayName account">@provider.AuthenticationScheme</button>

After:
<button type="submit" class="btn btn-default" name="provider" value="@provider.DisplayName" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>

If you haven’t made many changes to the identity code in your project you might consider using the new identity razor class library. You can find the details here.

Wrapping Up

Migrations between versions of ASP.NET Core have gotten easier over time as you can tell by the smaller length of these posts. One thing to note is while this will get you targeting 2.1 with all the performance benefits and access to a lot of the new features there will still be work needed if you want to do everything the new 2.1 style. I highly recommend creating a new 2.1 application to get a feel for the other changes you might want to make to your existing applications.

The code with all the changes can be found here. Remember that the only project that was upgraded was the Contacts project.

Migration from ASP.NET Core 2.0 to 2.1 Read More »

Migration from ASP.NET Core 1.1.x to 2.0

On August 14th .NET Core 2.0 was released including corresponding versions of ASP.NET Core 2.0 and Entity Framework Core 2.0 which got with the finalization of .NET Standard 2.0. The links take you to the release notes for each item.

In this post, I will be covering taking the project used for the ASP.NET Basics series from 1.1.x to the 2.0 release. The starting point of the code can be found here. This post is only going to cover conversion of the Contacts project.

Installation

If you are a Visual Studio user make sure you have the latest version of Visual Studio 2017, which can be found here and at a minimum should be version 15.3.

Next, install the SDK for your operating system. The list of installs can be found here. For development, it is key that you install the SDK, not just the runtime. The following is a preview of what to expect on the download page.

Csproj

The csproj file of the project being upgraded is the best place to start the conversion. The TargetFramework needs to be changed to 2.0.

Before:
<TargetFramework>netcoreapp1.1</TargetFramework>

After:
<TargetFramework>netcoreapp2.0</TargetFramework>

Next, PackageTargetFallback changed to AssetTargetFallback.

Before:
<PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;portable-net45+win8</PackageTargetFallback>

After:
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81</AssetTargetFallback>

There is a new Microsoft.AspNetCore.All package that bundles up what used to be a huge list of individual packages. Those individual packages still exist, but this new one wraps them and makes it much easier to get started. The following is the package list before and after.

Before:
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.1" />		
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.1" />		
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />		
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />		
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />		
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />		
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />		
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />		
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.1" />		
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />		
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />		
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />		
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />		
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />		
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />		
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />		
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />		
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />		
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0" />		

After:
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />

Last change in this file is to change the DotNetCliToolReference versions to 2.0.0.

Before:
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />

After:
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />

Program.cs

Program.cs is another area that has been simplified by creating a default builder that does all the same things that were happening before but hide the details. Keep in mind the old version still works and is valid to use if you use case needs it.

Before:
public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

After:
public static void Main(string[] args)
{
    BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
           .UseStartup<Startup>()
           .Build();

Identity

The remaining changes I had to make were all related to Identity. In the Startup class’s Configure function the following change was needed.

Before:
app.UseIdentity();

After:
app.UseAuthentication();

Next, in the ManageLoginsViewModel class, the type of the OtherLogins property changed.

Before:
public IList<AuthenticationDescription> OtherLogins { get; set; }

After:
public IList<AuthenticationScheme> OtherLogins { get; set; }

The SignInManager dropped the GetExternalAuthenticationSchemes function in favor of GetExternalAuthenticationSchemesAsync. This caused changes in a couple of files. First, in the ManageController the following change was made.

Before:
var otherLogins = _signInManager
                  .GetExternalAuthenticationSchemes()
                  .Where(auth => userLogins
                                 .All(ul => auth.AuthenticationScheme != ul.LoginProvider))
                  .ToList();

After:
var otherLogins = (await _signInManager
                   .GetExternalAuthenticationSchemesAsync())
                  .Where(auth => userLogins
                                 .All(ul => auth.Name != ul.LoginProvider))
                  .ToList();

The second set of changes were in the Login.cshtml file. First the function change.

Before:
var loginProviders = SignInManager.GetExternalAuthenticationSchemes().ToList();

After:
var loginProviders = (await SignInManager.GetExternalAuthenticationSchemesAsync()).ToList();

Then the change to deal with the changed property names.

Before:
<button type="submit" class="btn btn-default" 
        name="provider" value="@provider.AuthenticationScheme" 
        title="Log in using your @provider.DisplayName account">
    @provider.AuthenticationScheme
</button>

After:
<button type="submit" class="btn btn-default" 
        name="provider" value="@provider.Name" 
        title="Log in using your @provider.DisplayName account">
    @provider.Name
</button>

Wrapping up

With the changes in the Contacts project now works on ASP.NET Core 2.0!  Make sure to check out Microsoft’s regular migration guide. as well as their identity migration guide. A full list of breaking changes for this release can be found here.

There is a lot more to explore with this new release and I have a lot of projects to update. Don’t worry I won’t be doing a blog post on all of them, but if I do hit any issues I will create a new post of update this one with the fixes. The finished code can be found here.

Migration from ASP.NET Core 1.1.x to 2.0 Read More »

Visual Studio 2017 Error: The project doesn’t know how to run the profile IIS Express

I have a couple of computers I work between for the samples I use on this blog and when switching to between of them I got the following error last week.

The project doesn’t know how to run the profile IIS Express.

I verified the project would still run on the other computer with no issues. I also verified that on the computer with the issues the project would still work using dotnet run from the command line still worked.

Next, I went to verify the project properties. Here I noticed a strange thing on the computer with the error the Debug tab of the project properties was missing a lot of setting. The following is a screen shot of the computer with the issues.

And here is the same tab on the same project, but from a different computer.

The cause

After more time that I would like to admit I was able to track down the issue. On the computer with the issue, I often work on projects that are very large which tend to slow down Visual Studio pretty bad. In an effort to speed things up a bit I when through and disabled all the extensions that I could including the Microsoft Azure App Service Tools. Turns out that disabling the previous extension caused the Microsft ASP.NET and Web Tools extension to be disabled as well (with no warning).

The solution

The only way I was able to get the project to work properly was to enable both the Microsft ASP.NET and Web Tools extension AND the Microsoft Azure App Service Tools extension. I am not sure why the Microsft ASP.NET and Web Tools extension need the Microsoft Azure App Service Tools extension but based on my experience they are related in some way.

Visual Studio 2017 Error: The project doesn’t know how to run the profile IIS Express Read More »

Entity Framework Core Errors Using Add-Migration

I started off trying to expand my sample from last week’s post and hit some issues when trying to add a migration for a new DbContext.

The Setup

I added the following DbContext that only has one DbSet and auto applies migrations in the constructor.

using EfSqlite.Models;
using Microsoft.EntityFrameworkCore;

public sealed class ContactsDbContext : DbContext
{
    private static bool _created;

    public DbSet<Contact> Contacts { get; set; }

    public ContactsDbContext(DbContextOptions<ContactsDbContext> options)
        : base(options)
    {
        if (_created) return;
        Database.Migrate();
        _created = true;
    }
}

The command

Using Visual Studio’s Package Manager Console I ran the following command.

Add-Migration AddContacts -Context ContactsDbContext
Error 1 – No parameterless constructor

The above command resulted in the following error.

No parameterless constructor was found on ‘ContactsDbContext’. Either add a parameterless constructor to ‘ContactsDbContext’ or add an implementation of ‘IDbContextFactory<ContactsDbContext>’ in the same assembly as ‘ContactsDbContext’.

I read the first sentence and added a parameterless constructor to ContactsDbContext. I did think it was strange that a parameterless constructor wasn’t required the other contexts I had written in the past, but the error said to add a parameterless constructor so that is what I did.

Error 2 – System.InvalidOperationException: No database provider has been configured for this DbContext

Now having a parameterless constructor I ran the Add-Migration command again and was greeted with the following error.

System.InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

The second error forced me to step back and think more about what the problem was as it didn’t have an action I could take as the first sentence, which is, of course, my fault for not fully digesting what the error was saying.

The fix

The bit I was missing was the fact that I hadn’t added the following to the ConfigureServices function of the project’s Startup class.

services.AddDbContext<ContactsDbContext>(options =>
    options.UseSqlite(Configuration.GetConnectionString("Sqlite")));

With the above added I removed the parameterless constructor from ContactsDbContext and was able to successfully run the add migration command again.

Wrapping up

The moral of the story is to actually read the full error message before running off and trying to fix the problem. The second error message saying “using AddDbContext on the application service provider” is what triggered me to head in the right direction.

This was also a good reminder that tools like the ones used by Add-Migration can/do compile the project they are being used on in order to have enough context to perform their tasks.

Entity Framework Core Errors Using Add-Migration Read More »