Original: Http://stephenwalther.com/archive/2015/02/03/asp-net-5-and-angularjs-part-7-running-on-a-mac
In this article, I will run the previous ASP. NET 5 project on my Mac.
Installing Mono
First you have to install mono. The Mono project is independent of Microsoft. Mono is an open source implementation of. NET running on Mac OSX and Linux.
http://www.mono-project.com/download/
The installation of mono is very convenient and friendly.
Update Homebrew
The next step is to install KVM using homebrew. In this step I have several problems.
The first problem is that Ruby's position on the yosmite has changed, resulting in homebrew not working:
Http://apple.stackexchange.com/questions/153790/how-to-fix-brew-after-its-upgrade-to-yosemite
I update homebrew with the following command so that homebrew can work on OSX Yosemite:
Cd/system/library/frameworks/ruby.framework/versions sudo ln-s current 1.8 sudo brew update sudo RM 1.8
Installing KVM
After the homebrew is working properly, install the ASP. NET 5 KVM using homebrew. First, you need to run the following command to associate Aspnet/k GitHub repository with Homebrew:
Next, run the following command to install the KVM:
Brew Install KVM
After running, I encountered an error with KVM installed, but "just not linked". Perform the following command to fix the problem:
sudo brew link KVM
Perform the following command to verify that KVM and mono are installed properly:
KVM List
Before running the KVM or K command, you need to run the following command:
SOURCE kvm.sh
You need to run this command every time you open terminal, so terminal know where to find KVM and K.
Install the Movies App
Next, I get the source code for the movies app from GitHub repository and execute the following command:
git clone https://github.com/StephenWalther/MovieAngularJSApp.git
On Mac I use sublime text as the editor for. Net apps.
Using the In-memory database
The Movies app uses EF7 and Microsoft SQL Server. Microsofe SQL Server cannot run on Mac.
You have the following choices about databases:
Sqlite–sqlite is an open source database that can run on Windows, Windows Phone, OSX, Linux. .
Azure SQL database– deploys your database to the cloud.
In-memory– uses the in-memory database (note that a restart of the Web service data is lost).
I can't let sqlite work on the beta version of ASP. NET 5. Unable to find the Usesqlite () method, I guess this sqlite provider has not yet supported the beta version of ASP. NET 5.
Using Azure SQL database is the best choice for real-world applications.
I chose to use the In-memory database in this example.
For the Movies app to use in-memory provider I'm going to make 2 changes. First, I'll add a entityframework.inmemory reference to the Dependencies section of the Project.json file.
Then, modify the Startup class. Modify the Configureservices () method in Startup.cs to check whether it is running in mono (OSX or Linux) environments. When the Movies app is running in mono, use the in-memory provider. Otherwise, use Microsoft SQL Server.
public void Configureservices (iservicecollection services) {//SQL Client not a vailable on mono var Usingmono = Type.GetType ("Mono.runtime")! = NULL; ADD EF Services to the Services container if (Usingmono) {services. Addentityframework (Configuration). Addinmemorystore (). Adddbcontext<moviesappcontext> (); } else {services. Addentityframework (Configuration). Addsqlserver (). adddbcontext<moviesappcontext> (options = options. Usesqlserver (Configuration.get ("Data:DefaultConnection:ConnectionString")); }); }//Add ASP/Identity services. Addidentity<applicationuser, Identityrole> (Configuration). Addentityframeworkstores<moviesappcontext> (); Add ASP. NET MVC services. Addmvc ();}
The
Createsampledata () method of the startup class is updated. Only run on Windows to call Ensurecreatedasync () to create the database. You do not need to create a database file when using In-memory provider .
private static Async Task Createsampledata (IServiceProvider applicationservices) {using (var DbContext = Applicationser Vices. Getservice<moviesappcontext> ()) {//Ensure SQL Server database created var sqlserverdatabase = DbC Ontext. Database as Sqlserverdatabase; if (sqlserverdatabase! = null) {Sqlserverdatabase.ensurecreatedasync (). Wait (); }//Add some movies var movies = new List<movie> {new Movie {title= "Star Wars", D Irector= "Lucas"}, new movie {title= "King Kong", director= "Jackson"}, new movie {title= "Memento", Dir Ector= "Nolan"}}; Movies. ForEach (M = DbContext.Movies.AddAsync (m)); Add some users var usermanager = applicationservices.getservice<usermanager<applicationuser>> (); Add Editor user var Stephen = new ApplicationUser {UserName = "Stephen"}; var result = AwaiT Usermanager.createasync (Stephen, "[email protected]"); Await Usermanager.addclaimasync (Stephen, New Claim ("CanEdit", "true")); Add Normal user var Bob = new ApplicationUser {UserName = "Bob"}; Await Usermanager.createasync (Bob, "[email protected]"); }}
Using Kestrel
Unable to use IIS under OSX. Under OSX you need to use the Kestrel Web server, which is developed by the ASP.
Under the covers, Kestrel is built on top of LIBUV, which are also used by NodeJS. You can read about LIBUV here:
http://nikhilm.github.io/uvbook/
In order to use Kestrel, you need to add Kestrel dependencies in the Project.json file. Here is my complete dependency section:
"Dependencies": {"Kestrel": "1.0.0-*", "entityframework.inmemory": "7.0.0-*", "Entityframework.sqlserver": "7.0.0-beta2", "Entityframework.commands": "7.0.0-beta2", "MICROSOFT.ASPNET.MVC": "6.0.0-beta2",/* "Microsoft . AspNet.Mvc.WebApiCompatShim ":" 6.0.0-beta2 ", */" Microsoft.AspNet.Diagnostics ":" 1.0.0-beta2 "," microsoft.aspnet.d Iagnostics. Entity ":" 7.0.0-beta2 "," Microsoft.AspNet.Identity.EntityFramework ":" 3.0.0-beta2 "," Microsoft.AspNet.Security.Coo Kies ":" 1.0.0-beta2 "," Microsoft.AspNet.Server.IIS ":" 1.0.0-beta2 "," Microsoft.AspNet.Server.WebListener ":" 1.0.0- Beta2 "," Microsoft.AspNet.StaticFiles ":" 1.0.0-beta2 "," Microsoft.Framework.ConfigurationModel.Json ":" 1.0.0-beta2 "," MICROSOFT.FRAMEWORK.CODEGENERATORS.MVC ":" 1.0.0-beta2 "," Microsoft.Framework.Logging ":" 1.0.0-beta2 "," Mic Rosoft. Framework.Logging.Console ":" 1.0.0-beta2 "," Microsoft.VisualStudio.Web.BrowserLink.Loader ":" 14.0.0-beta1 "},
Get NuGet Dependencies
Before running the Movies app, we need to get the dependencies defined in the Project.json file from NuGet. Execute the following command:
KPM Restore
Run Kestrel
Execute the following command:
K Kestrel
Executing the above command will start to enable the Kestrel Web server.
Kestrel The default port is 5104. You can see which Port Kestrel is using in the Commands section in the Project.json file.
Open it:
http://localhost:5104
You can log in with Stephen or Bob (the password is [email protected]).
Deactivate Kestrel
To deactivate Kestrel surprisingly unexpected trouble. In OSX, the terminal ctrl-c will not do anything and will not stop the service. Here are my steps:
Ctrl-z Stop Kestrel.
Enter PS to get all processes.
Enter the kill process ID to end the mono process.
Here is StackOverflow's discussion of this issue:
Http://stackoverflow.com/questions/25712814/how-to-quit-asp-net-kestrel-web-server-on-a-mac
Running ASP. NET 5 on a Mac