HBD.Mef.Mvc Enhancement For WebApi

Like the previous post, I have introduced a Workspace for Web Mvc and along with the post, AzureNotes Module was provided as an example.

Now in this post, I would like to update you the new version 1.0.5 of HBD.Mef.Mvc. From this version, it will support the WebApi technology that allows developing a similar Workspace for Web API.

The HBD.Api.Shell

Similarly to HBD.Mvc.Shell, the HBD.Api.Shell is the Workspace allowing to develop a WebApi module in the different project and deploy to the Workspace lately.

Develop a WebApi module is simpler than develop a module for Wb Mvc because WebApi doesn’t have any resources (views, CSS files, and JavaScript files) other than controllers. Hence, deployment a module into the Shell is just copying the binaries and config files only.

To simplify the life, I have provided a general routing for all Modules including the Workspace as below.

This route is applying for all modules and all controllers. Hence, the pathway to access to a controller is api/{area}/{controller}{id} which {area} is Module’s name, {controller} is the controller name and {id} is optional.

The module registration also simplified instead of providing the Module Name and custom routing. Promptly with API module requires the name only. Below is sample code to register an API module.

The HBD.Mef.Mvc will base on the registered named and select the appropriate controller for the request. If there is no controller found for an appropriate request the HttpResponseException with 404 status code will be thrown.

Mef Controller Resolver Service

Please note that this registration is for Api modules only. There is nothing changed on the Mvc module registration that had been introduced on the previous post.

The AzureNoteApi

I would like to show you a similar module for HBD.Api.Shell called AzureNoteApi; this module is just exposing all actions of AzureNote in the previous post as WebApi. Below is the details information the Apis. You can use Postman to try out with my live demo.

Live Demo

The HBD.Api.Shell is also published on to Azure you might have a try by using Postman with the following API.

  • HBD.Api.Shell API

  • AzureNote API

Source Code

The latest source codes had been uploaded to Github you can download here.

Nuget Packager Unsupported Framework Issue on VSTS

I was facing an Unsupported Framework issue with Nuget packager on Visual Studio Team Service Online (VSTS) when porting my HBD Framework to the .Net Standard.

I had been mad with this issue in a couple of days, and eventually, I found a solution to fix it. Let me explain about the problem and the solution. So it might help for those who are facing the same problem.

1. Build and Deployment Process.

First, I would like to share my Nuget Publishing Automation Process. I have using VSTS as primary source codes repository sin last few years. In there, I created a few auto build and auto release processes that help to publish my packages into Nuget.org continuously.

Internally, I’m using the Package Management for VSTS as my test environment. So all beta versions of my packages have been published into this tool for testing purposes. Once the package is stable, I just simply trigger the other release to put it to Nuget.org.

Below is the illustration of the publishing process that had been implemented on my VSTS.

I will show you the details of the continuous deployment in difference post as the purpose of this post is fixing the Nuget Packager issues.

Continuous Deployment

2. The Issue.

When ported my HBD framework to the .Net Standard, the Nuget Packager was showing the below warning message when packaging my binaries on VSTS.

After then The .Net Standard had displayed as the Unsupported framework on my package. Hence, my package was not able to publish to Nuget.org as it is not allowed an Unsupported framework to be deployed.

Furthermore, if open the nuspec file with Nuget explorer the frameworks is displaying properly.


Here is the configuration of my nuspec file.

3. The solution

After spending a lot of times on this issue, I realized that the problem is the version of default Nuget.exe file in Nuget Packager is old (3.x), and it is not supporting the .Net Standard.

So, to fix this issue, I downloaded the latest version of Nuget.exe here and replaced the default version of Nuget packager by the following configuration of the build definition on VSTS.

  1. Put the Nuget.exe file in the Shared location on VSTS.
  2. Map the Shared location to the Build Definition.
  3. Replace the Nuget.exe of Nuget Packager with the version in Shared location.

 

Then execute a new build and re-deploy the package to Nuget.org again, here is the result.

  • Hopefully the issue of the Nuget Packager will be fixed soon. However, ai this time if you are facing the same problem then just apply this as the work around solution for your build.
  • VSTS online is free tool. Register here if you want to learn about the auto build, auto deployment.

You can download the latest source code of HBD.Framework here.

Start and Stop Azure VMs Using Microsoft Flow

You have a few VMs on Azure and not ordered to be accessible at 24/7. However, you regularly forget to turn it off after use. As you know, keep the VMs running idly on Azure is wasted. So if you are looking for a tool that helps you to turn off your VM without login to the Azure portal? Then this post is for you. You might know about the Microsoft Flow application that allows automating the manual actions. So get used of the Flow, I would like to show you how to create a button on Flow to start and stop your Azure VMs.

1. Setup Automation Account and Runbook

Runbook is a helpful feature that allows automating the manual tasks in the Automation Account. So get used of this, the following steps will show you how to create and integrate the Runbooks with Flow. You shall found more information about the Runbooks here.

2. Implement Runbooks

To running a Runbook the Automation account need to be established. Here I will create a new Automation account named HbdAutoAccount.

Navigate to Runbook Gallery, look up and import  2 Runbooks Start Azure V2 VMs and Stop Azure V2 VMs with following names Start-Steven-PC and Stop-Steven-PC.

Now, I will have 2 Runbooks in the automation account as below screenshot.

3. Create Webhooks

Turn into each runbook, click Edit and Publish buttons before using it, because Azure is not allowing to run the unpublished Runbooks. If any change on the Runbook, remember to publish it again to get effective.

Next, go to the Webhooks position of Runbook and create a hook with the following information.

  • Name: Start Steven PC
  • Enabled: Yes
  • Expires: 1 year from today.
  • URL:
  • Parameters:
    • Resource Group Name: VMs, the resource group of  VM.
    • Name: Steven-PC, the VM name.

Create the same hook for Stop-Steven-PC. After 2 Webhooks had been set up, I have 2 URLs as below. These URLs will be used to create two buttons on Microsoft Flow.

  • Start Steven PC: https://s3events.azure-automation.net/webhooks?token=q5F3KX1nRol1vjgkIz61NRLj6T%2fxNCQOeC4jDunBQaA%3d
  • Stop Steven PC: https://s3events.azure-automation.net/webhooks?token=q5F3KX1nRol1vjgkIz61NRLj6T%2fxNCQOeC4jDunBQaA%3d

4. Create the start and stop buttons on Flow.

1. Create Start and Stop buttons on Microsoft Flow.

After logged into Microsoft Flow (if you don’t have Flow account just create one, it is free). Click on My Flow and Create From Blank then select Flow button for Mobile with action is HTTP:

  • Flow name: Start Steven-PC
  • Http method: POST
  • URL: the start Steven PC URL had been established previously.

Then click Create Flow button. After this step, I will have a Mobile Button on Flow to start the VM. Following the same for the stop button.

Download Microsoft Flow for iPhone, and now I can Start and Stop my VM on Azure by using my phone directly.

2. Enable the auto shutdown of Azure VM.

Moreover, the Azure VM has the other feature so-called Auto Shutdown. It will help to shut down the VM during certain time periods. In here, the shutdown time of my VM is 1:00 am. So in case I missed, It will shut down my VM at 1:00 am every day automatically.

  • With Flow button you can able to start and stop a set of VMs at the same time by adding the HTTP actions contexcturely.
  • As I have openned 2 webhooks for Flow buttons this is a Security gap so shiuld not disclose the hook URLs to anyone even your wife.
  • The expire date of the hooks should keep it as short as manageable and when it is expired, you should create a new one and update the URL back to the Flow buttons.
  • Recently, the Azure for mobile has been released that allow you to manage Azure Resources on the go. However, using that tool is required the Azure Subscription. In case you want to release the VM for someone that they don’t have subscription, Flow is still useful for you.

The Vietnamese version here.

Why Workspace? Why Mef? What HBD?

As recently, I’d shared a few libraries that allow developing a Workspace, and many people asked me, What is Workspace and Why should We care about it?

So, I come up with this post to share a few advantages of the Workspace. I’m not talking about disadvantage here because every Framework has the pros and cons itself. However, We choice it because it was suitable for our company, our departments and our teams or in other words is our customers choice it.

The Advantages Of the Workspace

  1. When developing a new application, we need to build the framework, define the project’s structure, the theme and layout in the IDE and beside of that we need to implement the services that will be used in the application as authentication service, workflow service, input, and output services. What happen if there are so many applications are developing on the different teams in your department, and eventually, each team come up with an application that has difference theme, layout, and components. Just consider Workspace is a Framework and it will help to manage the layout, theme, services consistently between the development teams. It also advises removing the duplication efforts when two or three teams are implementing the similar components.
  2. The other case. In your department, there are a hundred websites are operating and now your company just released a new logo and requested to replace the logo on all application. So, how many teams need to involve and how long it take? As you know re-branding is not only a logo but also the theme, color table, and fonts of the application are getting changed. However, if you have a Workspace you just need to enhance and re-deploy it again without impact to the modules.
  3. Let’s see, If your department already had the Workspaces. So when any team wants to build a new application they can use that Workspace as a framework and start to develop their modules instead of taking time for solution structure, framework defination, theme, and layout creation for the application.

The other Advantages already shared on the previous post. I would like to bring them here so that We will have a general view on it.

  1. Let’s imagine, if you have many development teams and tring hard to make the teams work independently and parallely. However, if three or four teams are working a the significant changes of a complex application and that application does not support modularization. So when any team needs to deploy the changed to the Stagings or Production environments, they need to inform all the other teams to ensure there is no conflict between the teams. After deployed, the other teams need to merge the changes into their source code branches. Managing this situation is a nightmare for the project manager.
  2. The other scenario, if the application doesn’t support modularization, so any change even the small one you need to conduct the System Integration Test (SIT) and User Acceptant Test (UAT) for the whole system because of the impact and the efforts will charge back to the business. You know, the business users may surprise why the simple change is costly?
  3. Micro-Services adoption: as you know, Micro-service is new technology that structures an application as a collection of loosely coupled services, which implement business capabilities. The microservice architecture enables the continuous delivery/deployment of large, complex applications. It also enables an organization to evolve its technology stack.

Why’s Mef

I’ve worked on many Dependency Injections as Ninject, AutoFax, Unity, QuickInject and StructureMap.

The functionality of them is the same because they have been implemented based on Ioc, Dependency Injection concepts. Similarly, The Mef had been released sine the .Net Framework 4.0

Only one thing different between the libraries is the way to import the type mapping and export the objects from the DI container.

If you ask me Why I like Mef? The answer is because of Love. The crazy love without reason. I think the accepted answer is because She is Microsoft’s daughter.

Hope the answer satisfies you.

What is HBD?

HBD (Hoang Bao Duy) is just a lazy, foolish developer, and all his projects were supposed for personal used only.

All HBD projects had been developed by himself. Obviously, There are a lot of silly code in his projects. If you found it, please help to point it out, that is the way you help him grow.

He is not recommending to use his library on your company projects because the enterprise project framework should be verified carefully by the professional development and testing teams.

Saying that doesn’t mean the libaries were came up with poorly quality. Just want to share more information about HBD libraries, all had been tested carefully before publishing, beside that the vulnerability scanning, code quality scaning are applied by using Sonarqube.comWhitesource-Bolt. The unit tests, code UI tests also a part of his projects in order to ensure all functionality of the libraries are working as expected. Furthermore the author had trying his best to improve the libraries, add new features and hotfix the defects promptly.

However, if you want to implement something but don’t know how to start or you want to have some referent code then the HBD open source projects are for you.

Source Code

All HBD projects had been published in here.