Thursday, December 29, 2016

Sending emails with .NET Core v1.1 and mailgun with attachements

I simply wanted to send some emails programmatically using .NET Core v1.1 with attachments.
Perhaps I should have chosen a different route, however I had already signed up for a 
mailgun account as it seemed like that would be the quickest and easiest way to do it.  
Perhaps it was... but it certainly wasn't very quick, and wasn't exactly easy; 
atleast not until I figured it out.  Now it seems super quick, and super easy.  

I hope this helps someone save some time.  Peace.


public static void SendEmailWithAttachement
(string emailTo, string emailSubject, string emailText, byte[] attachmentByteArray, string attachmentFileName)
        {
            // the domain name you have verified in your Mailgun account
            const string DOMAIN = "mg.mynotsosupersecretdomain.com";
            // your API Key used to send mail through the Mailgun API
            const string API_KEY = "key-mysupersecretkey";

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = 
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes("api" + ":" + API_KEY)));

                using (var content = new MultipartFormDataContent())
                {
                    var values = new[]
                    {
                        new KeyValuePair<string, string>("from", "Jimmy Neutron <Jimmy.Neutron@gmail.com>"),
                        new KeyValuePair<string, string>("to", emailTo),
                        new KeyValuePair<string, string>("subject", emailSubject),
                        new KeyValuePair<string, string>("text", emailText)
                    };

                    foreach (var keyValuePair in values)
                    {
                        content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
                    }
                    
                    var fileContent = new ByteArrayContent(attachmentByteArray);//(System.IO.File.ReadAllBytes(fileName));
                    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        Name = "attachment",
                        DispositionType = "form-data",
                        CreationDate = DateTime.Now,
                        Size = attachmentByteArray.Length,
                        FileName = attachmentFileName,
};
content.Add(fileContent); var requestUri = "https://api.mailgun.net/v3/" + DOMAIN + "/messages"; var result = client.PostAsync(requestUri, content).Result; //TODO do something with the result... //like log it... or something dude. } } }

Monday, December 19, 2016

ASP.NET Core v1.1 inside a docker container connected to a containerized PostgreSQL database with Entity Framework v1.1

Running the latest dotnet core inside a docker container with entity framework code first hooked into a postgresql database (also in a container) was way harder for me than I think it should be.  So I wrote this blog to help others who may decide to go down this path.

I really just want to run the latest version dotnet core inside of a docker container and connect my code to a postgresql database using entity framework, and then update everything with migrations and 'update-database'.  You know, like back in the 'good ole days' when it was just plain ASP.NET MVC with EF hooked into a MS-SQL server database.  I suppose getting to a cross-platform codebase along with a sweet object relational mapper should be a little bit complex... but that doesn't mean it should be complicated.  However my goals were much more difficult to achieve than I first anticipated.

Once I figured out a mechanism that would work on my development machine (virtual machine running Windows) and something that would work on my production environment (linux [ubuntu 16.04] with docker) I wanted to make sure I created a 'kernel of truth' which could help others, and possibly myself in the future (I really never want to bang my head against the wall that hard again).

So, I created a project, which can be found on github: https://github.com/TotalTechware/kot


And now I'll tell you a little bit about it.

Let's just get this out of the way.... I really do not know what I am doing... but I think this is correct.... I mean at least it works! Of course, the more I read the more about the state of ASP.NET Core v1.1 and EntityFramework Core v1.1, it becomes painfully obvious that at this point not many people are sure what is 'correct' (particularly the tools).  So it would appear I am in good company.  But I will give it a shot at explaining what I did.  Some things I've forgotten, some things I wish I could forget... but I digress.... on to it.

I downloaded the community version of Visual Studio 2017 RC and installed the dotnet core 1.1 SDK.  Then I tried to go -> file -> new project... This kind of works... but not really.  I proceed to update all the packages to v1.1 (instead of v1.0.1) and things are going a little bit better.  But honestly I'm not sure exactly how I ended up with a working project.... which is why I made this 'kernel of truth' project.  

My suggestion at creating a new v1.1 project is to copy the relevant parts of my kot.csproj and paste it into your .csproj.  One of the major issues of confusion is the switch from using project.json to now using project.csproj with msbuild.  Even more confusing is that the 'latest' docker container is still pointing to the project.json style, whereas I read project.json is history; and microsoft is moving to msbuild stuff.... which caused me all sorts of headaches, and I'm sure others too.

I used the package manager inside visual studio as sparingly as possible, and probably will try to steer clear of it, until later tool set upgrades.  Right now, I'm using the cmd line inside windows and bash shell inside of docker containers to call things like 'dotnet ef migrations add nameOfMigration*' and things like 'dotnet ef database update'.  Which is just fine with me, but can be a major source of confusion when you are used to doing everything inside of PM> (the package manager console inside visual studio) because at the moment there is no such console when on a linux machine (or even just using command prompt on windows).  It does appear that the CLI tools are moving toward allowing package manager type stuff, but we will still have to wait and see.  

Bottom line.  I can now spin up a virtual machine with docker installed and with a few commands (found in the project under: bashCommandsToSpinUp.txt) and have two docker containers running and communicating... one running code and the other running the database.  Hurray!!!

I know this blog may sound a bit contentious... I'm not actually upset with anything other than myself for having to spend so much time searching and trying various things to get this to work.  I hope this article (and more specifically the kot project) will help others spend far less time than I did, and get them back to doing productive code writing stuffs.

Peace homies. 

Tuesday, May 24, 2016

Visual Studio development of ASP.NET Core RC2 Web App with PostgreSQL and Entity Framework code first deployed to Ubuntu

Getting started with Visual Studio development for ASP.NET Core RC2 connected to a PostgreSQL database using Entity Framework code first all deployed to a Ubuntu Linux server*

*While being developed in a Windows environment using Visual Studio 

Wow!  That's a mouthful of a heading!

Alright, so Microsoft just released ASP.NET Core RC2 a week ago.  ASP.NET Core RC2 is a cross platform web framework which allows a traditional c# web developer who is used to using tools like Visual Studio to develop applications for Windows servers can now target much cheaper Linux servers.  Hurray!  That's great.  But what about databases?  Well, Microsoft has announced they will be releasing MS-SQL Server 2016 on Linux, which is pretty incredible.  But it's still going to cost money.  PostgreSQL is a free open source SQL database which there is an open source package written (Npgsql) which provides Entity Framework support to PostgreSQL.

Basically what this means, is that you can develop with Visual Studio using the traditional ASP.NET MVC/webAPI with Entity Framework and Controller and View generation goodness you are used to, and now deploy to a free server with a free database.  Oh and did I mention, Visual Studio Community 2015 is free as well!  It's never been a better time to be a c# developer.  And in fact, any one can now use this toolset, so I expect it will grow in popularity.

Getting started with ASP.NET Core RC2 from a Windows and Visual Studio perspective

This is part 2 of a 2 part blog series for getting started with ASP.NET RC2 with Entity Framework connected to PostgreSQL deployed to an Ubuntu Linux server (In part 1 of this series the Ubuntu Linux server is created, setup and configured).  This blog entry will go over getting started on the Windows development experience, and that will include creating a basic app, and deploying to the linux server.

You will either need to run Windows 7+ in a virtual machine (which is what I do on my MacBook using Virtual Box running Windows 7) or you will need to be running it natively. 

Let's get started with download and install Visual Studio Community (It's free): https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx

Install Microsoft ASP.NET and Web Tools Preview 1 tooling for Microsoft .NET Core 1.0.0.0 RC2: https://visualstudiogallery.msdn.microsoft.com/c94a02e9-f2e9-4bad-a952-a63a967e3935

Creating our example ASP.NET Core RC2 Application in Visual Studio


Open up visual studio, and select to create a new project.  For this example I will be naming the project unape.  I'm choosing this name as an acronym for Ubnutu nginx ASP.NET Core PostgreSQL Entity Framework.  This is what I'm calling the unape stack. Select .NetCore and ASP.NET Core Web Application.  Select the web application template, and leave the authentication method as Individual User Accounts.  This setting will populate the base application with a database context, controller methods, and views for user's to login with a password and change their password, along with role values.

As soon as you hit ok the app will be created and the packages will need to be restored.  This will add folders and stuff that aren't immediately available until all the packages have restored.   This takes about 15-45 seconds or so.  Although it may be dependent on internet connection speed.  The restoration process is indicated by an icon next to the references in the solution explorer. 


Add PostgreSQL EntityFramework data provider Npgsql


In order to connect Entity Framework to PostgreSQL a package named Npgsql must be added to the project.  It's an open source project, check it out.  Inside Visual Studio open up the Package manager console and run the following command:  

Install-Package Npgsql.EntityFrameworkCore.PostgreSQL -Pre

This command will cause Visual Studio to add the appropriate references to the project.json file and subsequently restore (download) the references for Npgsql.  The -Pre is for RC2.  The process of this restoration will again be indicated by the references icon in the solution explorer.  


Change database connection string to point to PostgreSQL


Next, the default connection information inside the appsettings.json file needs to be changed.  Inside the ConnectionStrings json object change the value for the DefaultConnection property.  Change the default connection from:

"Server=(localdb)\\mssqllocaldb;Database=aspnet-POengine-bb847c65-bea9-4d5e-9bd6-627efd36bfc7;Trusted_Connection=True;MultipleActiveResultSets=true"

Which will target a local ms-sql server database.  Replace this value with one that will target our PostgreSQL database:

"User ID=*pgsql-user*;Password=*pgsql-user-pass*;Server=*Ubuntu-IP-Address*;Port=5432;Database=unapeDbContext;"

Replace the values for *pgsql-user* and *pgsql-user-pass* with the user name and password combination that was created for the PostgreSQL database in Step 3 of the previous blog post when we were configuring the server. The value *Ubuntu-IP-Address* should also be changed to the appropriate IP address for the Ubuntu server.  This was also acquired in the previous blog post.


Create a simple example Model class named 'Item'


Now create a plan old object in the models folder, call it 'Item.cs'.  This will be our simple model for this example app.  To anyone who has done Entity Framework code first, this should look very familiar, as it is the exact same for type of model file.  Type or copy the code below into Item.cs:

namespace unape.Models
{
    public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public float Price { get; set; }
    }
}


Create a database context and add the 'Item' Model to the context


Now we need to create a database context so we can connect our Item object model to the actual database tables.   This is the same way as traditional entity framework code first, however we have to add an additional construction to the database context file that accepts options and passes them down to a base constructor.  Create a new file inside the Data folder and name it 'unapeDbContext.cs', write this code in it:

using Microsoft.EntityFrameworkCore;
using unape.Models;

namespace unape.Data
{
    public class unapeDbContext : DbContext
    {
        public unapeDbContext(DbContextOptions<unapeDbContext> options) : base(options) { }

        public DbSet<Item> Items { get; set; }
    }
}


Update method in Startup.cs to use Npgsql and add database context


Now we must modify the configuration of the application to use PostgreSQL instead of the default MS-SQL.  This is done inside the 'Startup.cs' file.  In the ConfigureServices method, change the following line:

services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

to:

services.AddDbContext<ApplicationDbContext>(options => 
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));

This will modify the application's individual account authentication database context to use the PostgreSQL with the connection string we modified earlier.  We need to add an additional line right underneath this one to add the database context for the 'unapeDbContext', this is done by the following line:

services.AddDbContext<unapeDbContext>(options => options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));


Use dotnet cli for migrations and updating the datatbase


At this point the application is ready for database migrations.  In previous versions of Visual Studio the Visual Studio Package manager was used to do entity framework database migrations.  In ASP.NET Core the dotnet command line interface(cli) is used.  Open 'cmd.exe' and navigate to the directory of the unape project's src folder.  (this is done by using the command 'cd' and the directory's name you want to enter... But I expect you know how to do this, if not google will help.)  Once in the unape project's src folder, type the command:

dotnet ef migrations add firstMig -c unapeDbContext

This will create a folder named migrations inside the project, along with an initial migration file and a 'firstMig' file.  These migrations will be used to update the database with the following command:

dotnet ef database update -c unapeDbContext

The migrations will be executed on the PostgreSQL database which will cause the Item table to be created with the columns corresponding to the Item.cs classes properties.  Now we need to update the database for the individual account authentication database context, type the following command:

dotnet ef database update -c ApplicationDbContext

Now the migrations for the tables associated with user names, passwords and roles will be added to the PostgreSQL database.  As you can probably tell, the '-c' flag is for declaring the database context name.


Scaffold the controller and views for the 'Item' Model


At this point we have the database ready to roll, and now we need to add a way to create, read, update, and delete Item's.  Visual Studio has had a wonderful tool for doing this rapidly for quite a while now, it's called scaffolding.  Right click inside of the 'Controllers' folder and select Add-> New Controller.  Select MVC Controller with views, using Entity Framwork and click 'Add'.   The Model class: should be entered as 'Item', it will auto populate as you begin to type, and the Data context class should be 'unapeDbContext'.  Leave all the defaults selected, and click 'Add'.  This will a generate five views and a controller with corresponding corresponding actions.  


Add link and test run the app


In order to easier access the scaffolded Item views, let's add a link inside our 'Views/Shared/_Layout.cshtml' file.  Inside the navbar unordered list, add an additional list item:

<li><a asp-controller="Items" asp-action="Index">Items</a></li>


At this point we are ready to test run the app inside visual studio.  Press F5 to build and launch the application, this will automatically bring up a browser and navigate to the locally running site.  Once the app is launched, clicked the 'Items' link in the navbar toward the top right of the page.  To ensure we have everything completely setup correctly, create a new item.  Everything should work correctly, and the new item will show up in the index page.  At this point we are ready to bundle everything up and publish our example app onto our Ubuntu Linux server. 


Publish the app to Ubuntu using ftp


Stop the app running in visual studio.  Right click the project name in the solution explorer, and select publish.  We are planning on using the ftp server we setup on the Ubuntu server, but there isn't a publish option with ftp.  What we will do is deploy to the file system and then copy the files over to the ftp server.  Select the file system as the publish target, and give it some profile name.  The default target location is inside the bin\release\publishoutput\ folde.  This is fine. Click Publish.

Now we will ftp into our Linux box via Windows explorer, inside the windows explorer toolbar, type in ftp://'IP-address-of-Ubuntu' for me this looks like:

ftp://192.168.1.131

With another windows explorer folder navigate to the unape's project bin\release\publishoutput\ folder and copy the contents to the ftp server.


Run the published ASP.NET RC2 web application on Ubuntu


Once the files have finished coping to the Ubuntu server, hop back onto the terminal and navigate to the publish output folder which was copied over via ftp.  The files will be in the Ubunut user's home directory.  Run the following command to start the web application:

dotnet unape.dll

Once the application launches it will begin listening on localhost port 5000.  We previously configured nginx to take external requests on port 80 and route them to localhost port 5000.  Now we can open up a web browser and type in, 'http://*ip-address-of-Ubuntu*', and bam baby!  ASP.NET Core with Entity Framework connected to a PostgreSQL database all hosted on Ubuntu Linux!  How awesome is that!?  

Setting up Ubuntu 14.04 for ASP.NET Core RC2 with PostgreSQL

Getting started building an ASP.NET Core RC2 with a PostgreSQL database hosted on Ubuntu Linux

Downloading, Installing, and Preparing Ubuntu 14.04 Linux server

Over the next two blog posts I will go over the steps in order to create a web app using ASP.NET Core RC2 with Entity Framework connected to a PostgreSQL database all hosted on an Ubuntu Linux server.

This first post will cover setting a virtual machine with Ubuntu 14.04 to be ready to host the web app.  The intended audience is for asp.net c# developers who are familiar with visual studio, c# & EF on Microsoft Windows operating systems and Microsoft SQL server, and would like to get started with the latest free and open source cross platform version of ASP.NET known as ASP.NET Core, and connect it to a free and open source database sql server known as PostgreSQL.   Instead of writing queries by hand I will use the Entity Framework as the ORM.

ASP.NET Core RC2 is pretty exciting because it allows the use of the awesome Microsoft tools such as Visual Studio and allows developers to deploy their apps to a completely free and open source tech stack!

Basically there are 3 basic steps to take to get our Linux server ready to run our app.
  1. Install Ubuntu 14.04 on virtual machine.
  2. Install ssh, PostgreSQL, ftp, nginx, & dotnet RC2
  3. Configure PostgreSQL, ftp, & nginx

Step 1. Install Ubuntu 14.04 on virtual machine.

Download and Install Virtual Box: https://www.virtualbox.org/wiki/Downloads
Install Ubuntu 14.04 on a new virtual machine with virtual box.  Inside of virtual box's settings for the virtual machine, be sure to set the network settings to use the 'Bridged Adapter' option.  This will provide the virtual machine with a network accessible IP address.  It took me about 8 minutes to create a virtual machine with virtual box and install Ubuntu.

Step 2. Install ssh, postgresql, ftp, nginx, & dotnet rc2

The following commands can be run to install all the required items:  Since I would prefer to copy and paste my commands instead of typing them, I will simply run the first two commands and then run 'ifconfig' to get the ip address of the linux VM.  I will then open up terminal, and ssh into the vm using the command: 'ssh username@ip-address'.  

sudo apt-get update

sudo apt-get install openssh-server

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -

sudo apt-get update

sudo apt-get install postgresql postgresql-contrib

sudo apt-get install vsftpd

sudo apt-get install nginx 

sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'

sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
sudo apt-get update

sudo apt-get install dotnet-dev-1.0.0-preview1-002702

Step 3.  Configure PostgreSQL, ftp, & nginx

Now the servers need to be configured.  PostgreSQL is the most time consuming to configure, let's start with it first.  PostgreSQL will need a database user created, and set to allow for external connections.  Create a database user name by executing the following command.  Immediately after the command runs a prompt will request the password the user.  Enter a password, and remember it.  It will be needed for the connection string info.

sudo -u postgres createuser *webAppDbUserName* --createdb --pwprompt

sudo nano /etc/postgresql/9.5/main/pg_hba.conf

At the top of the pg_hba.conf file, add the following two lines, and save the file:
host     all     all     0.0.0.0/0     md5
host     all     all     ::0/0         md5

sudo nano /etc/postgresql/9.5/main/postgresql.conf

The postgresql.conf file has the following line:
#listen_addresses = 'localhost'  

For examples sake we will all the database server to be connected to by any IP address.  This is done by removing the '#' which comments out the line, and also replacing 'localhost' with '*'. Now the line should look like this:
listen_addresses='*'

Save the configuration file with the changes.  Now we can restart the PostgreSQL server and it will be accessible by external connections.  The command to restart PostgreSQL is:

sudo service postgresql restart

Now PostgreSQL is configured to allow remote connections, and it has a user with a password which will allow for creating databases.  Great, now we just need to configure the ftp server and nginx.  The ftp server will simply need to be switched to enable writing by removing a commenting '#' in the configuration file.  Type the following command to open the file:

sudo nano /etc/vsftpd.conf

Look for the line that says 

#write_enable=YES

Remove the '#' and save the file.  Now restart the ftp server using the following command:

sudo service vsftpd restart

Now the ftp server is configured to allow the writing of files.  Finally the nginx server will need to be configured.  For simplicity sake and since this is a fresh install, I will delete all the configuration information for the nginx server and replace it (Don't delete this file if you already have a configured nginx server).  The following two commands will delete the existing file, and then open an editor to replace it:

sudo rm /etc/nginx/sites-enabled/default
sudo nano /etc/nginx/sites-enabled/default

Now type or copy and paste the following into the empty file, and save it:

server {
     listen 80;
     location / {
         proxy_pass http://localhost:5000;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection keep-alive;
         proxy_set_header Host $host;
         proxy_cache_bypass $http_upgrade;
     }
 }

Now we just need to restart the nginx server and it will be configured to point our external traffic on port 80 towards our locally hosted site on port 5000.  This is the default location for ASP.NET Core apps to be hosted.  Restart nginx server with the following command:

sudo service nginx restart

Our Linux server is now setup and configured to allow us to deploy our ASP.NET Core web app.  All we need to do now is get an ASP.NET Core web app and run it.  We will begin building and deploying our ASP.NET Core RC2 web app in the next blog post.  

Monday, May 23, 2016

ASP.NET Core RC2 with Entity Framework code first connected to PostgreSQL database deployed on a Ubuntu 14.04 Linux server

[EDIT: this was my first blog post, and it is pretty long, it is also not formatted the best.  I worked to fix these issues by splitting this post into two, Post 1: Setting up Ubuntu 14.04 for ASP.NET Core RC2 with PostgreSQL and Post 2: Visual Studio development of ASP.NET Core RC2 Web App with PostgreSQL and Entity Framework code first deployed to Ubuntu  I would recommend reading those two posts, instead of looking at this one.]

This blog entry will go through setting up a completely newly installed Ubuntu 14.04 server, and creating an example ASP.NET core web application in visual studio on windows, and deploying to the Linux server.

Okay here goes something new for me... Blogging about software development.

Microsoft has released ASP.NET Core RC2 a week ago, which is an open-source cross platform version of the c# web framework.  And what's even cooler, is that there is an open source database connector for entity framework and postgresql!

My setup is a macbook pro running two virtual machines.  I run windows with visual studio in one, and ubuntu on the other one.

Download and install Virtual Box: https://www.virtualbox.org/wiki/Downloads

Download Ubuntu 14 Server 64bit:  http://releases.ubuntu.com/14.04/ubuntu-14.04.4-server-amd64.iso

Install Ubuntu 14.04 using Virtual Box.

Change network settings inside virtual box for ubuntu virtual machine use the 'Attached To' drop down to select the 'Bridged Adapter' option.  This will allow us to easily get an ip address for our virtual machine and access it via ssh, ftp, and http.

Once ubuntu is setup, we begin by installing ssh.  Ssh will allow connecting to the ubuntu server via a more user friendly remote terminal.  The following commands will install the ssh server, and show the IP address information.

sudo apt-get update
sudo apt-get install openssh-server 
ifconfig

Take note of IP address, it will be needed. Minimize the ubuntu virtual machine, open terminal inside of mac, and login to virtual machine via ssh using the following command:

ssh *ubuntu-setup-username*@*ubuntu-ipaddress*

There will then be a prompt for the ubuntu setup username's password, enter it and login.

Now to install postgresql9.5(the latest version as of this blog... skipping this step we would install 9.3) we need to add the repository location, get the cpg key and update our repository, and finally install postgresql 9.5, using the following commands:

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -

sudo apt-get update

sudo apt-get install postgresql postgresql-contrib

Once this completes postgresql will be installed.  Now we need a database user for our application to use to access the postgresql database.  To create a new user, execute the following commands:

sudo su postgres
(become the 'postgres' user)

psql
(start the psql shell)

Inside the psql shell postgresql statments can be executed. But for now, let's log out and create a user. 

\q
(quit the psql shell)

createuser --interactive

This will create a prompt for a role (user) name and if they should be a super user. I always select 'y' for yes they should be a super user.  Then a password will need to be created for the user.  This is done by using the psql shell.  Type the following commands:

psql
ALTER USER *username* WITH PASSWORD '*password*';
\q

su *ubuntu-setup-user*
(switch back to the ubuntu setup user)

There are two configuration files we need to modify in order to open postgresql to outside connections.  The first one can be opened and modifed using the following command:

sudo nano /etc/postgresql/9.5/main/pg_hba.conf


This file controls the ability to connect to the postgres server from various ip addresses.  For our purposes, I just open the database to any ip address.  This is again not something you would want in a real production server, this is just for getting started.  We will add two lines to open to all ipv4 and ipv6 ip addresses.  
host     all     all     0.0.0.0/0     md5
host     all     all     ::0/0         md5


Once that file is saved there is one more configuration file for enabling external connections.  Type the following command to access it:

sudo nano /etc/postgresql/9.5/main/postgresql.conf

This file has a line:
#localhost='localhost' 
this needs to be changed to:
localhost = '*'
Make sure to remove the comment mark '#' at the beginning of the line.  (that darn commenting pound sign got me spinning my tires for atleast an hour).  

Now just restart the postgresql server to use the new configuration.  Type the following command to do restart it:

sudo service postgresql restart

Now you can connect to the database from a remote host. Hurray! (You can test this with pg-admin on the windows development machine, which is a handly visual tool for windows to access the postgresql database.  It's basically the equivalent of ms-sql server managment tools for postgresql).  If we can connect using this application on the windows development machine, then we know visual studio along with entity framework will have connectivity.  Download pgAdmin here: https://www.pgadmin.org/download/windows.php . Let's finish setting up ubuntu)

To install and configure ftp we need to do a few steps.  First we need to install the service vsftpd.  Use the following command to do so:

sudo apt-get install vsftpd

Now we need to modify the configuration file

sudo nano /etc/vsftpd.conf 

we must remove the '#' sign in the configuration file so we can upload the files.  Uncomment the line: 

write_enable=YES

Save the configuration, and restart the vsftpd server:

sudo service vsftpd restart

Now the ftp server is setup to allow files to be copied to the home directory of the ubuntu user.  

We need to install and configure nginx 

sudo apt-get install nginx
sudo rm /etc/nginx/sites-enabled/default
sudo nano /etc/nginx/sites-enabled/default

type the following into empty nginx configuration file:

 server {
     listen 80;
     location / {
         proxy_pass http://localhost:5000;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection keep-alive;
         proxy_set_header Host $host;
         proxy_cache_bypass $http_upgrade;
     }
 }

Save the configuration file, and then restart the nginx server:

sudo service nginx restart


Finally for setting up ubuntu we install dotnet core RC2 we need to execute the following commands: 

sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'

sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
sudo apt-get update

sudo apt-get install dotnet-dev-1.0.0-preview1-002702


Once the linux virtual machine is all setup, it's time to get the windows virtual machine's development environment setup.  Once windows is installed, you'll need to install visual studio, and the web tools for RC2.


Install Microsoft ASP.NET and Web Tools Preview 1 tooling for Microsoft .NET Core 1.0.0.0 RC2: https://visualstudiogallery.msdn.microsoft.com/c94a02e9-f2e9-4bad-a952-a63a967e3935 

For the next part of this entry I go over the steps inside of visual studio to setup a basic CRUD app.  The source code that should result from following steps proceeding can be found at https://github.com/TotalTechware/unape

Open up a new project in visual studio name it 'unape', which stands for ubuntu, nginx, asp.net core, postgresql, entity framework.

Select .NetCore -> ASP.NET Core Web Application 

Select the web application template, and leave the authentication method as Indvidual User Accounts

At this point, the packages have to restore which will also add folders and stuff that aren't immediately available until all the packages have restored... so basically go check your email or get a cup of coffee cause this will take about 30-60 seconds... i'm guessing this is also dependent on your internet speed, so results may vary.  The restoration process is indicated by an icon next to the references in the solution explorer.

In order to connect entity framework to postgresql we use a package called npgsql.  It's an open source project.  Inside Visual Studio open up the Package manager console and run the following command:
Install-Package Npgsql.EntityFrameworkCore.PostgreSQL -Pre

This will cause visual studio to download and restore the references for npgsql for RC2, which again the progress of this downloading process will be indicated by the icon next to the references in the solution explorer.  


Change the default connection information inside the appsettings.json file.  Change the line from:
  
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-unape-1a240cd2-788a-41a2-a6b9-ef2e02774e98;Trusted_Connection=True;MultipleActiveResultSets=true"

to:

 "DefaultConnection": "User ID=*pgsql-user*;Password=*pgsql-user-pass*;Server=192.168.1.131;Port=5432;Database=unapeDB;"


Use the name and password of the postgresql user we created previously. 

Now we will create a plain old object in the models folder.  For this example purpose, this simple Item object will work:

namespace unape.Models
{
    public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public float Price { get; set; }
    }
}

Inside the Data folder we will create a new database context.  The only difference between the traditional method of EF code first in the Database context file is the addition of the additional constructor which takes in options and passes them down to the base constructor.  Here is the way my file looked:

using Microsoft.EntityFrameworkCore;
using unape.Models;

namespace unape.Data
{
    public class unapeDbContext : DbContext
    {
        public unapeDbContext(DbContextOptions<unapeDbContext> options) : base(options) { }

        public DbSet<Item> Items { get; set; }
    }
}

Alright, now we must modify the configuration to use postgres instead of the default ms-sql.  This is done inside the Status.cs file.  

In the ConfigureServices method, change the following line:

services.AddDbContext<ApplicationDbContext>(options =>                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

to:

services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));

This will modify the application's authentication database context to use the postgres database we have setup, we also need to add a line to this method to allow our unapeDbContext to be configured.  That is done pretty intuitively like this:

services.AddDbContext<unapeDbContext>(options => options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));

Build the solution to make sure we don't have any typos or anything unexpected.  Now let's create a migration for our unapeDbContext, and after we have created a migration, we will update the database with both the unapeDbContext as well as the ApplicationDbContext.  Open Cmd.Exe on the windows machine, and navigate to the directory of the unape project's src folder.  Then Type the following commands into the cmd.exe command prompt:

dotnet ef migrations add firstMig -c unapeDbContext
(this will create a folder named migrations inside the project along with two migration files)

dotnet ef database update -c unapeDbContext
(this will cause the migrations to be exectued on the postgres database, which will cause the database unapeDb to be created and have the Item table with the columns we gave to the Item object)

dotnet ef database update -c ApplicationDbContext
(this will cause the migrations which are for user name, password, and roles authentication to execute and corresponding tables to be created in the postgres database)

Alrighty, at this point we should have the database ready to roll, now we just need to add the controller and views for the CRUD operations on the Item object.  This can be done easily using scaffolding.  Because we picked the individual account as our authentication the appropriate package has been added to allow us to do "right-click Add new controller" inside visual studio.  So at this point it is the same scaffolding stuff as traditional entity framework.

Right click inside of the controllers folder and select to add a new controller. Select MVC Controller with views using entity framework.  Using the drop downs, or typing select the Model class:  Item (unape.Models), and Data context class: unapeDbContext (unape.Data).  Leave all the defaults selected, and the controller name should have auto populated with ItemsController.  Click Add.  This will generate 5 views and a controller with the corresponding action methods to access and populate those views.  

In order to make it easier to access the 'Items' screens, let's add a link to our Items index page inside the _Layout.cshtml file.  Inside the navbar unordered list, add an additional list item:
<li><a asp-controller="Items" asp-action="Index">Items</a></li>

At this point, run the app inside of visual studio.  The browser will open with our app, with the ability to navigate to the Items controller from the navbar link, and also create, view, update, and delete all the items.  Everything should work just fine.  At this point, we are ready to deploy our basic example app into Linux.  To do this we will copy over our the folders that are created during the publish process.  

Stop the app running in visual studio.  Right click the project name in the solution explorer, and select publish.  Select the file system as the publish target, and give it some profile name.  The default target location is inside the bin\release\publishoutput\ folder.  this is fine.... click publish.

Now we will ftp into our linux box via windows explorer, inside the windows explorer toolbar type in ftp://'ipaddress of server'  for me that looks like:
ftp://192.168.1.131

With another windows explorer folder navigate to the unape's project bin\release\publishoutput\ folder, and copy the contents to the ftp server with a drag and drop.

Now once the files have copied over to the ubuntu server.  We will hop back onto the terminal to navigate to the publish output folder that was copied over via ftp, and run the following command:

dotnet unape.dll

This will launch the app, and it will be listening on localhost:5000, we configured the nginx reverse proxy to take requests coming in from the outside on port 80 to be routed to localhost:5000, so now we can open up a web browser and type in the http://'ip-address of ubuntu server',   Bam baby!  We are now running the asp.net core with entity framework connected to postgresql database all on ubuntu linux!