In this section, we examine the existing projects that will be migrated to ASP.NET Core and .NET Core.
- [Voiceover] Take a little bit of time to review the application and the projects that we're going to migrate from web API 2.2 to asp.net core. If you haven't already downloaded the files, go ahead, I'll wait just put me on pause. Hey, welcome back, so go ahead and get the solution from the downloaded files, and we want to start with the WebAPI22Sample Solution, this was in the original code directory, restore the NuGet packages, correct the connection strings if needed, execute the Entity Framework Migrations, and then confirm the IP of the service.
Sounds like a lot of things to do, it's really not so bad. So let's open up the project here, and we want to restore the NuGet packages, that's as simple as right-clicking on the solution name and click on 'restore NuGet packages', and I already have mine updated so there was nothing to display but down here in the output window you might see a bunch of packages being restored. Now let's confirm the connection strings, there's two places in this solution that we have connection strings, first in the app config for the test project, one of the test projects, and we want to see down here on line 18 where we have the store context connection string and just confirm that your server name is correct.
If you're using local db, you don't have to change anything, it'll create a database on that local db sequel server express instance named SpyStore short web API, and you don't have to worry about any passwords, we are using integrated security. The other place to check the connection string is down in the web config for the SpyStore web API project. So open up the web config, scroll down to line 68 and do the exact same thing, confirm that the connection string is what you'd like it to be.
To get the database created, we're going to use entity framework migrations. This is also very simple, it's already built for you, all you have to do is go into the solution items, in line five, which is the update database line, go down to package manager console, and if you don't have it showing, you can go up here into the quick launch and type 'package' and there're several different ways to get to it, but we're going to hop right in here.
Power shell starts up, copy in the line and hit 'enter'. Now my database is already current so there was nothing to do, and it says so. If you're building this for the first time you'll see a lot of output, and it'll tell you that it's complete. The final thing we want to do is confirm the IP address of the service, since you copied this off of the download code, it shouldn't be an issue but just to be safe, let's right click on SpyStore web API project, go to properties, go to web, and make note of the port that it is running on.
In this example it's 62335. That's all the changes we have to make in this project, let's now open the SpyStoreServiceTests Solution. Again restore the NuGet packages, and then we have to confirm the IP address of the service in the BaseTestClass, so let's open up that project, open up the BaseTestClass here in the SpyStoreService.tests project, lots of Ss in that name, and confirm that the port in the service address matches the port that we saw in the other application.
Now that we've confirmed all the settings for the existing projects, let's look a little bit at what they do. So the store web API, if it's not set as the start up, go ahead and right click and say 'set as startup project'. This is a very simple web API, it's got one real controller, the values controller is what's created with file new project. And the shopping cart controller has examples of all four HTTP verbs that we want to look at today.
So we have a couple of different get methods, three to be exact. Then we have a post, which adds a new record to the shopping cart, we have a put which updates a record in the shopping cart, and then we have a delete. Not going to go deep into all the code now 'cause we're going to migrate all this, which is also a models project that holds our three main models, category product and shopping cart record as well as a view model.
We then have a very simple data access layer, and any framework has some initializers, now I will tell you the standard way of doing data initialization in EF6 was using a data intializer, well I won't say standard, one of the ways of doing initialization in EF6 and there is one of those in this project, however, I'm not really using it. What I have done is adopted the paradigm we use in EF core to repopulate the database, and I do this mostly for testing, and we'll see this in action but know that the initialized data is where we populate all of the data fro the database for the examples.
We have a project named 'DAL tests' and these are just a few tests using X unit on the full .net framework to verify that our data access library is working as expected. Not a fully tested application, but tested well enough to show some examples of migrating unit test as well as the web API and I highly recommend, if you're doing web API work whether it's in 2.2, the full .net framework, or in asp.net core, you should have lots of unit tests around it because unlike an mvc app which you should also be testing, you can't just hit F5 and navigate around your site to make sure that it works.
The second solution, the SpyStoreServiceTest just links to the data access library and the models, so those are the same projects we just saw, and then here is a whole suite of tests that just test the web API. The reason I do it in a separate solution is really for me just a matter of convenience. This way I can hit F5 in the web API solution, it's running, I get the full de-bug experience and then I've got this separate solution where I'm executing unit tests so I can de-bug both sides.
- Creating the .NET Core project
- Adding Entity Framework
- Migrating the data access layer (DAL)
- Configuring services and the HTTP pipeline
- Adding remaining services to the dependency injection container
- Migrating controllers and actions
- Testing the services