• last year
Transcript
00:00In this lesson we're going to be publishing our API to our computer. So
00:04what happens is that there are a number of configuration considerations that we
00:11have to take when we want to do development against an API using an
00:16Android emulator. So what happens is that the Android emulator actually runs in
00:20its own kind of framework or network on its own network and our API is going to
00:26be running on another network and then trying to get them to talk can be a bit
00:31of a challenge. So what we're going to do is publish this API locally so that it
00:37is actually broadcasting from a server and then it would almost be like the
00:44Android device would be connecting to it through the internet right. So I just
00:49have a few refactors that I would like to carry out before we do this publish
00:54and number one has to do with our database connection string. So initially
00:58we would have set a path that was in the local folder so that's why we have this
01:05carlessdb file and that's fine but we can't have it in the folder when we
01:10publish because sometimes there are permissions issues and I don't want to
01:14bring it on that rabbit hole of dealing with all those permissions issues. So we
01:18can put it at a more neutral spot and I've put it on my C drive in a folder
01:24called carlessdb and the file will be called carlessdb right. So once our
01:30app is published that's where it will be looking for our database and literally
01:35it's as easy as just creating that file path and we can copy this database and
01:40just place it over there since that's what we've been testing with. You can
01:44also go to SQLiteStudio.pl and that actually gives you a management tool so
01:54if you're familiar with SQL Management Studio then you can get the SQLite
01:59Management Studio for your SQLite database right. So you can actually
02:03download that and use that to set up the tables and so on but if you just copy
02:08over the file then you wouldn't have to do all of that manual work because it's
02:11already created for us. So to publish what we'll do is right click go to
02:17publish and usually you get a wizard. I've already done it but I'll walk you
02:23through from scratch. So we click new and like I said we're doing it locally but I
02:28prefer to just push it to a folder and by default it will go to the publish
02:33folder. So at this point you also have another option so you could push it to
02:38publish folder each time and then you copy from that publish folder over to
02:43INetPub and I'll show you how to do that because I have IIS set up on my machine
02:47or you could just change this location to INetPub to the exact folder from IIS
02:55that the API will have to broadcast from but make sure that you're in admin mode
03:00with Visual Studio. So you'd right click the Visual Studio icon and say open as
03:05admin or run as administrator and then it would give you the permission to
03:09actually deploy. So like I said it can get complex with permissions because
03:13those are sensitive folders that are almost automatically protected. Now I am
03:17mentioning IIS and I do show you how to set up IIS in other courses but this is
03:22what IIS looks like and if you don't have IIS if you typed if you press start
03:26and type in IIS and you don't see internet information services manager
03:32as an option then you will need to enable some features so you can go to
03:38your control panel and look for Windows features or you can type it in the start
03:43just type in Windows features and it will bring it up and then you'll want to
03:46go to the internet information services and here you want to tick but I'm also
03:54just expanding so you can see exactly what I have ticked. Alright so you have
04:00the management console and then underneath application you want to make
04:04sure that you have these application development features enabled and you
04:11don't necessarily have to have all of these things because I do a number of
04:14things on my computer that you may not necessarily need but if you want to tick
04:19them off but usually when you tick the parent folder whatever is selected by
04:24default is usually good enough so you can do that and then click OK. Another
04:29thing that you will need is the hosting bundle for .NET 6 so we are
04:35building a .NET 6 API and we are going to be hosting it and essentially we're
04:42turning our local machine into a server so what you want to do is have the
04:47hosting bundle I'm on a Windows machine so obviously hosting bundle for Windows
04:53right and you want to make sure that that runtime is installed on your
04:58machine so that you can successfully deploy and run an API or .NET 6 web app
05:04in general. Now another thing another refactor that we need before we do the
05:11publish is I'm disabling the if statement around whether we use swagger
05:16in dev or not right so initially it would have had an if the environment is
05:22dev then use the swagger stuff but I'm I took off that if statement because when
05:26I publish it I want to see the swagger dot that's my immediate feedback that
05:30this thing is working so let's go through these steps again before I go to
05:35publish. One, ensure that you have set up IIS. Two, create a folder I didn't
05:43actually mention that part but create a folder in IIS so in IIS we want to have
05:51this website and I'll just go to one of them so you can see the folder so the
06:01folder for IIS really is we go to Windows C then we look for a folder
06:08called INetPub and then wwwroot or wwwroot and then inside of there you're
06:14going to create a folder so I called my own car list that's that's what I called
06:20my folder right so when I publish I'm going to be pushing directly to car list
06:24but like I said you can actually push to the the publish folder and then you copy
06:30over all right so I'm actually going to because I did it already it's hard to
06:37backtrack but I am going to walk you through each one so after you finish the
06:40code refactors those are the first thing change the connection string here as
06:45well as enable swagger for all environments then we're going to go and
06:51set up a publish profile so we click new we say folder next and then we publish
06:57locally so I'm just going to click finish and then it's going to create
07:02that publish profile it hasn't actually published as yet we can click show all
07:07settings just to verify that everything is what we want you can change the
07:11options to say do you want to clean out the folder every single time and then of
07:16course because we did the connection string in the program that says there is
07:19no database to detect and it's not going to see the data context like it would if
07:26we were doing a SQL server deployment right so you don't worry about those
07:33really because with SQLite you can't do that migration anyway so you can go
07:39ahead and click Save if you made any changes or not whichever one and then
07:45you can just hit publish so once you publish it builds the project once again
07:54and once that build is done we have a successful message letting us know that
08:01everything has been successfully deployed to our publish folder so you
08:06can just ctrl and click that URL that appears in the output window and then it
08:11will direct you to this Explorer window right so then we can actually take all
08:18of this you can just ctrl a ctrl C and then you jump over to INetPub and
08:28where's INetPub, there we go, www.root and then you create your folder here so I
08:37created car list you can call it car API it doesn't really matter you put it in
08:41there right so you create the folder and then you paste all of the files that you
08:45just copied inside of that folder now with that folder created when you go
08:52over to IIS what you do is right click say add new website you give it a name
08:59so I called my I have mine as car API I call this one car list and then we can
09:05select the pool so well by default it will create a new pool so we'll have to
09:11modify that then the physical path we go ahead and browse and find that new
09:16folder that we just created with the newly published files right so you can
09:21just go ahead and drill down into INetPub www.root find it right car list
09:28click OK we can leave this as HTTP leave the IP address and I would suggest that
09:35you just change the port for ease of use and debugging so I put line to like 8010
09:40if literally though this one that I already have published is 8099 so I'm
09:46just saying that you can append to the 80 it doesn't have to be exactly what I
09:50used but you just want to have a different number that it doesn't clash
09:54on port 80 and the host name is optional at this point because we'd have to
09:58modify our hosts file and so on I do show all of this end-to-end in other
10:03courses though so I'm not necessarily prioritizing all of that we just want
10:08the API up and running so that our mobile app can see it right so we add
10:13the website and then you click OK and once you click OK then you get the
10:17website space here now another thing that you want to do is go to your app
10:22pool and look for the app pool that was created and if you didn't change the
10:26name it would have the same name as the web app that you just created so car API
10:30corresponds with car API you want to double click that and make sure this is
10:35no managed code and that's I think by default would already say integrated so
10:40you change those two and then you click OK and then that's it for setting up
10:45your API so if you browse so here I'm about to browse to my web API that is on
10:51localhost 80 80 99 that's the port I put well then if I say slash swagger then it
11:00will load me the swagger doc and then if I try it out it will bring up my data
11:09why because I already created like I said you would want to have that folder
11:16in the C Drive that I call carless DB with the actual carless DB file right so
11:24make sure make sure you do that if you skip this step you might get a 500 error
11:28and it would actually be complained that it can't it doesn't see a database to
11:32connect to you're telling me pull from a database that I can't see so make sure
11:36that the file path that you specified in your connection string which in my case
11:42was something very simple make sure that it does exist so that see carless DB and
11:48the file name is carless which like I said I just copied and pasted from there
11:52if you need to you can create it from scratch using the SQLite studio and if
11:59you want to do that it can be a bit tricky but it can be done you can
12:03actually just go to the package manager console and run a command that says
12:08script hyphen migration so I'm showing you all the tricks right now script
12:14hyphen migration so script migration will actually look at the migrations
12:19that you have performed alright so all of the database changes that you may
12:24have done more or fewer than I have so if this and this is once again is if you
12:29want to do it from scratch right you don't want to copy that file so once you
12:35do script migration it shows you all of the things that you need to do right I
12:39have found that trying to run this just put it in paste it and run it has given
12:46me issues so the first thing would be go to the database right or add the
12:53database if the database doesn't exist you have to add it but or you have to
12:58create it but you can always go browse to the database on the file system once
13:04you're there then you can run the script and I would suggest you run it
13:07in pieces so run that one first run this and then run the create table then run
13:12all the inserts and then run commit I would suggest you chop it up like that
13:16but that's if you have to reach that far once again just copy and paste that file
13:21so now that our API has been published and published locally how do I know it's
13:29local once again I can browse to it via that URL right so I now know how to get
13:38to the API not in the development environment but should traffic need to
13:43come to my PC from a more global perspective than me running the API in
13:49Visual Studio then this is what that request URL needs to look like so this
13:53is now my base address alright so when we get back we'll turn our attention
14:00over to our mobile app we have to make some network configurations and we'll
14:07have to set up our service class