Contents


Before start discussion, I would like to recommend following reading from my dear friend Sumit Jolly:
Abstract

This article explains the basics of Azure API Management to manage our Web API.

Introduction

This article explains all the basics of Azure Management to manage our Web API. Here we will not explain the Azure Management, how it works and how to create Virtual Machine, Create Cloud App and host Apps with Azure. Our focus is only on Managing Web APIs using Azure Management.

Here we will:

Pre-requisites

To work with this article, we need:

Let's develop a simple ASP.NET WEB API

To work with this article we need WEB APIs, so, as a first step, let's develop a very simple web API using the following procedure.

Step 1: Create ASP.NET WEB API Project

To start we are using a simple ASP.Net project as in the following:

Step 2: Visiting Folder Structure

After the preceding step, we will see that our project template is ready and the following is the default folder structure provided by the template:

Step 3: Installing FluentNHibernate Support

To install FluentNHibernate support to the project:

Wait until FluentNHibernate has been installed.

Step 4: Working on Models, Mappings and Repository Pattern

In this step, we will create a Model and its mapping and will add a repository.

Adding Models and their Mappings

To add a model, right-click on the Models folder from the Solution Explorer and choose class and name it "ServerData".

  1. public class ServerData
  2. {
  3. public virtual int Id { get; set; }
  4. public virtual DateTime InitialDate { get; set; }
  5. public virtual DateTime EndDate { get; set; }
  6. public virtual int OrderNumber { get; set; }
  7. public virtual bool IsDirty { get; set; }
  8. public virtual string IP { get; set; }
  9. public virtual int Type { get; set; }
  10. public virtual int RecordIdentifier { get; set; }
  11. }

Adding Model Mappings

Add its mapping class, right-click on the Models folder from Solution Explorer and choose class and name it "ServerDataMap".

  1. public class ServerDataMap : ClassMap<ServerData>
  2. {
  3. public ServerDataMap()
  4. {
  5. Table("ServerData");
  6. Id(x => x.Id, "Id").GeneratedBy.Identity().UnsavedValue(0);
  7. Map(x => x.InitialDate);
  8. Map(x => x.EndDate);
  9. Map(x => x.OrderNumber);
  10. Map(x => x.IsDirty);
  11. Map(x => x.IP).Length(11);
  12. Map(x => x.Type).Length(1);
  13. Map(x => x.RecordIdentifier);
  14. }
  15. }
Do not forget to add the following namespace:
  1. using FluentNHibernate.Mapping;
Adding Fluent NHibernate support to project

In this class we need to configure NHibernate and build all sessionfactory so, our application will interact with the database:

  1. private static void CreateSessionFactory()
  2. {
  3. _sessionFactory = Fluently.Configure()
  4. .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString).ShowSql)
  5. .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ServerData>())
  6. .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, false))
  7. .BuildSessionFactory();
  8. }

We will discuss all that stuff in details since they are beyond the scope of this article.

Now, we are ready with our Repository to start playing.

P.S.: refer: msdn for more details on the Repository Pattern.

Step 5: Add WEB API Controller

In this step, we will add an API controller and will add all the necessary resources.

We all are set to go now:

  1. public IEnumerable<ServerData> GetServerData()
  2. {
  3. return serverDataRepository.GetAll();
  4. }

In the preceding method, why we add a "Get" suffix, this is a good practice to add "Get" with your method name, by convention it maps to a GET request.

This method does not have any parameter, so, you can say this maps to a URI that does not contain an "id" parameter.

Will the preceding method work? Of course, it works but it's not an ideal solution or we can say it's not quite complete. Why is that?

In the preceding method, we miss the following from the HTTP response:

From the preceding, we can understand that the WEB API matches this method to a PUT request, the preceding method has two parameters, id and serverdata. So, it is taken from the URI path and the serverdata is deserialized from the request body.

P.S.: By default the Web API framework takes simple parameter types from the route and complex types from the request body.

Step 6: Set default result output type

We need a result in JSON by default, let's add the following line, either in the Global.asx.cs file or in the file where you are registering routes.

  1. //return JSON response by default
  2. config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
Deploy our services to Azure

Let's now deploy our preceding created services to our Azure server.

Working with AZURE MANAGEMENT

We are now ready with our WEB API to host/manage these with AZURE MANAGEMENT. If you do not have an Azure subscription then try a trial one from here.

Create instance of API Management

First, we need to create an API Management instance. Use the following procedure:

In this screen, what did we do?

(P.S.: Here we selected both the HTTP and HTTPS protocols and I will explain this in future sections/series of articles.)

The API section has the following six tabs.

Add an operation

In our Web APIs we have the following resources:

In the preceding screen, we specified:

We are done with our APIs work. We have hosted our Web API with Azure API Management. They are now ready to be used or consumed by developers.

Developers will use the Azure API Management developer portal to use/see all the APIs related documentations.

Unfortunately, we have not yet registered any user as a developer, let's use the following procedure to register our API products.

(P.S.: we will discuss this in a future session; that is, how to set up various products and mange users with the Azure API Management.)

Subscribe to our created API products

To create subscribers, we can do it by invitation and with the use of the Administrator Portal of API Management.



Here, we are using the Developer portal where we have already invited/shared our development portal links to our end-users or targeted audience for development.

Discussing API MANAGEMENT Developer Portal

This section briefly explains the Developer portal.



This will be the default Home page for every developer. Developers can go through the app APIs documentation, your API Blog (if created) and other applications (if you are providing any for any Product).

Since we are not going into details of the Developer Portal, let's sign up as a Subscriber.

Testing APIs

We can test our hosted APIs via an external Client or using the Console from within the API Developer Poartal.

Conclusion

This article explained the creation of an ASP.NET Web API, hosting to Azure and management using the Azure API Management.

In future sessions/articles we will explain more about advanced topics/features of Azure API Management.

The complete source code is available at: Github.