Deploy your MongoDB application on Azure with MongoLab add-on

Standard

MongoLab is MongoDB-as-a-Service (DBaaS) platform. The Azure Marketplace MongoLab add-on will provide you with a MongoDB database hosted in the Azure cloud and managed by MongoLab’s cloud database platform. To deploy your application in Azure with Mongolab, first you need to provision your Database. You can do it from Azure Management portal. (The MongoLab workflow is not yet supported in the Preview Portal). Open the Azure Market Place, under App Services, you can find MongoLab.

MongoLab from Azure Marketplace

MongoLab from Azure Marketplace

In the Next screen you can select the Plan and provide the name of the Database. To evaluate MonogLab comes with a sandbox plan, which is free, for this post I choose the sandbox plan.

MongoLab - Plan and DB name

MongoLab – Plan and DB name

Next screen will confirm the purchase and creates the Mongo Database on Azure.

MongoLab - Purchase confirmation

MongoLab – Purchase confirmation

Once the Database created, you can manage it using MongoLab management portal, link to the portal available in the Azure Management Portal. You require a connection string to connect to MongoDB from ASP.NET which is also available in the portal.

MongoLab - Database options

MongoLab – Database options

You can get the connection details from Connection Info button, which is required to connect to MongoDB from ASP.NET code.

MongoLab - Database Connection Info

MongoLab – Database Connection Info

Now you can use the connection string in C# code. I am using last post code here.

_mongoClient = new MongoClient("mongodb://MongoSample:ABC00129020.ABC18991ABJ@SHJWHJS.mongolab.com:34348/MongoSample");
_mongoDatabase = _mongoClient.GetDatabase("MongoSample");
_employeeCollection = _mongoDatabase.GetCollection<Employee>("Employees");

_mongoClient.GetDatabase() method, you need to provide the database name you just created with MongoLab. Except this and connection string change, there is no difference in code. You can use kpm publish option to publish the package and can use FTP deploy option to deploy your code to azure.

You can manage your collection and database from MongoLab management portal. Here is the Mongolab Management portal for the MongoDB just created.

MongoLab - Management Portal

MongoLab – Management Portal

You can connect to MongoLab DB from Mongo shell as well. Always get the connection string or any other settings from configuration file instead of hard coding it in the code.

Happy Programming :)

CRUD application using ASP.NET 5 and MongoDB

Standard

MongoDB (from humongous) is one of many cross-platform document-oriented databases. Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster. Released under a combination of the GNU Affero General Public License and the Apache License, MongoDB is free and open-source software.

Getting MongoDB Up and Running

  1. You can download and install MongoDB from https://www.mongodb.org/downloads based on your system configuration.
  2. Once installation is completed, you need to create a folder structure like this. C:\data\db (if you installed it on a different drive, instead of C: drive, specify the drive).
  3. Now you can run the mongod.exe from C:\Program Files\MongoDB\Server\3.0\bin. You can see a console window displaying several messages pertaining to initialization and listening.
  4. Run the mongo.exe, which is MongoDB administration shell. By default, the shell will connect to the MongoDB instance that you just started.
    Mongod.exe is running and waiting for connections

    Mongod.exe is running and waiting for connections

Working with MongoDB from ASP.NET application.

  1. Configure your project.json file to download MongoDB C# driver.
    {
    	"dependencies": {
    		"Microsoft.AspNet.Diagnostics": "1.0.0-beta3",
    		"Microsoft.AspNet.Hosting": "1.0.0-beta3",
    		"Microsoft.AspNet.Mvc": "6.0.0-beta3",
    		"Microsoft.AspNet.Server.WebListener": "1.0.0-beta3",
    		"Microsoft.AspNet.StaticFiles": "1.0.0-beta3",
    		"Microsoft.AspNet.Server.IIS": "1.0.0-beta3",
    		"MongoDB.Driver":"2.0.0-*"
    	},
    	"commands": {
    		"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
    	},
    	"frameworks": {
    		"aspnet50": {}
    	}
    }
    
  2. You can connect to MongoDB using MongoClient class. GetDatabase() method of MongoClient class returns the MongoDB database instance. If the database doesn’t exists, MongoDB will create the database and returns an instance of IMongoDatabase.
  3. A Collection in MongoDB is loosely akin to a Table in SQL databases. Just as a Table may contain many related Rows, a Collection may contain many related Documents. IMongoDatabase.GetCollection<T>() returns a collection, with the name. If the collection doesn’t exists, similar to Database, MongoDB will create the collection and returns an instance of IMongoCollection<T>. So there is no problem even if you call the these methods many times.
    _mongoClient = new MongoClient();
    _mongoDatabase = _mongoClient.GetDatabase("EmpDb2");
    _employeeCollection = _mongoDatabase.GetCollection<Employee>("Employees");
    
  4. Here is the Model class. Instead of using integer Id property, Guid type is used. While creating the instance of the Employee class, I am assigning the default values.
    public class Employee
    {
    	public Employee()
    	{
    		Id = Guid.NewGuid();
    		JoiningDate = DateTime.UtcNow;
    	}
    	public Guid Id { get; set; }
    	[Required]
    	public string Name { get; set; }
    	[Required]
    	public string Designation { get; set; }
    	public DateTime JoiningDate { get; set; }
    	public string Remarks { get; set; }
    }
    
  5. Create method. You can use InsertOneAsync method to insert a document to MongoDB.
    _employeeCollection.InsertOneAsync(employee);
    
  6. Update entity
    _employeeCollection.ReplaceOneAsync(x => x.Id == employee.Id, employee);
    
  7. Delete an entity
    _employeeCollection.DeleteOneAsync(x => x.Id == employee.Id);
    
  8. Read all entities from DB
    _employeeCollection.Find("{}").ToListAsync();
    

    You can specify filter conditions or Lamda expressions inside Find method, which will return specific entities. Here is a lamda expression to get one entity, using Find() method.

    _employeeCollection.Find(x => x.Id == id).SingleAsync();
    

You can download the full source code from here

Here is the Mongo DB database and collection using RoboMongo.

Mongo DB - Database and Collection using RoboMongo

Mongo DB – Database and Collection using RoboMongo

Happy Programming :)

Continuous Integration with ASP.NET 5, GitHub and Travis CI

Standard

This post is about setting up a simple continuous integration environment for ASP.NET 5 with GitHub and Travis-CI. So when ever someone commits to the source, the build will be triggered. Once the build is succeeds, all the unit tests will be executes. If all the tests pass, the build will be successful. Otherwise the build will be treated as failed build.

  1. Creating source code and unit tests – As first step you need to create a simple ASP.NET 5 application and unit tests. In this post I am using a basic CRUD MVC application, I am using Moq and repository pattern make the solution testable.
    [HttpPost]
    public IActionResult Create(Employee employee)
    {
    	if (ModelState.IsValid)
    	{
    		_employeeRepository.Save(employee);
    		return RedirectToAction("Index");
    	}
    
    	return View("Create", employee);
    }
    

    And here is the test methods for the same

    [Fact]
    public void VerifyCreateEmployeeRedirectsToError()
    {
    	var employeeRepository = new Mock<IEmployeeRepository>();
    	employeeRepository.Setup(x => x.Save(It.IsAny<Employee>()));
    	var employee = new Employee() { Id = 1 };
    	var employeeController = new EmployeeController(employeeRepository.Object);
    	//Mocking the employeeController.ModelState.IsValid = false
    	employeeController.ModelState.AddModelError("Error", "Name is Required");
    
    	var createResult = employeeController.Create(employee) as ViewResult;
    
    	Assert.NotNull(createResult);
    	Assert.Equal("Create", createResult.ViewName);
    }
    
    [Fact]
    public void VerifyCreateEmployeeInsertData()
    {
    	var employeeRepository = new Mock<IEmployeeRepository>();
    	employeeRepository.Setup(x => x.Save(It.IsAny<Employee>())).Verifiable();
    	var employee = new Employee() { 
    		Id = 1, 
    		Name = "Employee", 
    		Designation = "Designation", 
    		JoiningDate = DateTime.Now };
    	var employeeController = new EmployeeController(employeeRepository.Object);
    	var createResult = employeeController.Create(employee) as RedirectToActionResult;
    	Assert.NotNull(createResult);
    	Assert.Equal("Index", createResult.ActionName);
    	employeeRepository.Verify();
    }
    

    I have added only two unit tests for create method. I am using almost same code as I mentioned in this post – Creating Unit Tests for ASP.NET MVC 6 Applications.

    Now you need to create the project.json file, to compile and test.

    {
    	"authors": [
    		"Anuraj"
    	],
    	"description": "ASP.NET 5 Unit Testing Sample Application",
    	"version": "1.0.0",
    	"webroot": "wwwroot",
    	"dependencies": {
    		"Microsoft.AspNet.Diagnostics": "1.0.0-beta3",
    		"Microsoft.AspNet.Hosting": "1.0.0-beta3",
    		"Microsoft.AspNet.Mvc": "6.0.0-beta3",
    		"Microsoft.AspNet.Server.WebListener": "1.0.0-beta3",
    		"Microsoft.AspNet.StaticFiles": "1.0.0-beta3",
    		"Microsoft.AspNet.Server.IIS": "1.0.0-beta3",
    		"xunit": "2.1.0-beta1-*",
            "xunit.runner.aspnet": "2.1.0-beta1-*",
    		"Moq":"1.0.0-*"
    	},
    	"commands": {
    		"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000",
    		"test": "xunit.runner.aspnet"
    	},
    	"frameworks": {
    		"aspnet50": {}
    	}
    }
    

    I am using K Runtime with beta 3 packages. Now you can restore the packages using kpm restore command. Once everything installed, you can run the application by k web command. You can run the tests by k test command.

  2. The Travis YML file – This file is equivalent TFS build script or build workflow file. This file helps to configure the build environment. In Travis CI, C# beta support available, so when we specify the language, Travis CI will download the mono framework and compile the source code on top of that. You can find more details about C# support in Travis CI here. You need to keep the .travis.yml file in the root directory. Here is the .travis.yml which can be used for ASP.NET 5 build and executing tests.
    language: CSharp
    install:
      - curl -sSL https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.sh | sh && source ~/.k/kvm/kvm.sh
      - kvm upgrade
      - kpm restore --no-cache
    script:
      - k test -parallel none
    

    The language line instructs the build agent to compile the project in C#. Because of this, build agent will download mono and execute all the steps. In the install phase, I am installing the KVM and downloading the runtime and packages using kvm upgrade, kpm restore commands. Finally, the script block will get execute, it will run the XUnit tests, once completed, Travis CI verifies the exit code of the process and based on exit code it will pass or fail the build. The -parallel none option required, it is a known issue in k test command, otherwise this command will not return exit code. You need to add this file also to the github repository.

  3. Linking GitHub and Travis CI – Once the above steps completed, now you need to tell travis ci about your github repository. You can do this by signin to Travis-CI. And from the Account page select the repository.
    Linking Travis CI and GitHub

    Linking Travis CI and GitHub

    Now we are ready with the CI environment, you can verify it by committing to the github repository. Here is the log output from Travis CI

    Travis CI build output

    Travis CI build output

Travis CI supports build status images. You can include it in the readme.md files. You can the code for status image by clicking on the build status image, right side of the repository name.

build status image from Travis CI

build status image from Travis CI

You can find the full application, with .travis.yml and unit tests here.

Happy Programming :)

How to host ASP.NET 5 Application in Azure Web Apps

Standard

This post is about hosting ASP.NET 5 Application in Azure WebApps / Websites. If you are using Visual Studio, you can use the publish wizard. But if you are using k or dnx runtimes this option is not available. ASP.NET wiki in GitHub contains a page about FTP deploy but it is not working :( Later I found another page, which discuss about this. But I had to spent some time to make the deployment work successfully.

Here is the steps you need to follow to deploy ASP.NET app to Azure Web App.

  1. Include IIS in the project.json file – Even if you are using WebListener, you have to include the reference of IIS in your project.json file. This is important without this deployment will not work. So your project.json will look like this.
    {
        "webroot": "wwwroot",
        "dependencies": {
            "Microsoft.AspNet.Diagnostics": "1.0.0-beta3",
            "Microsoft.AspNet.Hosting": "1.0.0-beta3",
            "Microsoft.AspNet.Mvc": "6.0.0-beta3",
            "Microsoft.AspNet.Server.WebListener": "1.0.0-beta3",
            "Microsoft.AspNet.StaticFiles": "1.0.0-beta3",
            "Microsoft.AspNet.Server.IIS": "1.0.0-beta3"
        },
        "commands": {
            "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5001"
        },
        "frameworks": {
            "aspnet50": { }
        }
    }
    
  2. Bundle the application using KPM build command – I am using k runtime, I tried with dnx runtime, but I faced few issues, so switched back to k runtime. Here is the command, which will bundle the source and will make it ready for deployment. Make sure, you are bundling with the proper runtime. I am using shared mode in azure, and hence my target platform is x86. So I am bundling with x86 version of the runtime.
    kpm bundle --out C:\WebApp --runtime kre-clr-win-x86.1.0.0-beta3
    

    You can find the installed runtimes and the name from the “C:\Users\[username]\.k\runtimes” folder. I am using beta3 runtime. Once you execute this command, you will find a folder structure like this in the C:\WebApp folder.

    Bundle output - Folder structure

    Bundle output – Folder structure

    Make sure you have a bin folder inside wwwroot folder, and which contains file – AspNet.Loader.dll. And here is the web.config file, inside wwwroot folder.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="kpm-package-path" value="..\approot\packages" />
        <add key="bootstrapper-version" value="1.0.0-beta3" />
        <add key="runtime-path" value="..\approot\packages" />
        <add key="kre-version" value="1.0.0-beta3" />
        <add key="kre-clr" value="clr" />
        <add key="kre-app-base" value="..\approot\src\MVCApp" />
      </appSettings>
    </configuration>
    

    All the runtime, dependencies and packages are created inside approot folder. If you are deploying it in IIS, you need to point the website to the wwwroot folder.

  3. Publish files using FTP – Connect to the website FTP folder using any FTP client. Copy the contents in the C:\WebApp(output folder of kpm build) to site folder of the website. With this copy operation, You are overwriting the original site/wwwroot folder with the one in output folder and adding a new folder site/approot. Once upload finished, Remote site folder structure will look like this.

    Remote site folder structure

    Remote site folder structure

Hosting ASP.NET 5 apps in IIS and IIS Express.

As I mentioned earlier, you can host this app in IIS, by pointing the physical path to the wwwroot folder.

ASP.NET 5 - Hosting in IIS

ASP.NET 5 – Hosting in IIS

And you can do the same with IIS express as well.

iisexpress /path:C:\WebApp\wwwroot /port:5001

Happy Programming :)