Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps

Hosting ASP.NET WEB API to Azure Service Fabric using Containers

5.00/5 (2 votes)
4 Jul 2018CPOL3 min read 13.3K  
How to host ASP.NET Web API to Azure Service Fabric using containers

Introduction

In this article, we will be hosting ASP.NET WEB API to Azure Service Fabric by the following three steps:

  1. Create ASP.NET Web API
  2. Create a Docker image locally for ASP.NET WEB API and push it to Docker hub
  3. Create container using image and publish it to Azure Service Fabric cluster

Prerequisites

A development computer running:

  • Visual Studio 2017
  • Service Fabric SDK and tools.
  • Docker for Windows. Get Docker CE for Windows (stable). After installing and starting Docker, right-click on the tray icon and select Switch to Windows containers. This is required to run Docker images based on Windows.

1. Create ASP.NET Web API

  • Visual Studio -> File -> New Project -> ASP.NET Web Application

    Image 1

  • Select Web API

    Image 2

  • Browse the application and you will be able to see sample WEB API running on the browser.

    Image 3

2. Create a Docker Image Locally for ASP.NET WEB API and Push it to Docker Hub

  • Here comes the interesting part when you need to create a Docker image (based on Windows). So, let's first create a docker file inside the solution. Add a new file called DockerFile in the project and add the below lines:
    FROM microsoft/aspnet:4.7.1-windowsservercore-1709
    ARG source
    WORKDIR /inetpub/wwwroot
    COPY ${source:-obj/Docker/publish} .
  • The above Docker file instructs to copy all the files from the current project publish folder to the working directory inside the container.
  • So now, you have the docker file; you can build an image via docker build from PowerShell (change directory to the location where the docker file is created).
  • Give it a name like apiimage/aspwebapi' and optional tag like 'latest'.
    docker build -t apiimage/aspwebapi:latest

    Image 4

    Image 5

  • Once done, you can execute the docker images command from PowerShell to check that the image is created.
    docker images

    Image 6

  • Run the image locally using the below command:
    docker run -d –name aspwebimage apiimage/aspwebapi:latest

    Image 7

  • Run the following command to check your running local image:
    docker ps

    Image 8

  • Run the following command to get IP of local docker image:
    docker inspect –format=’{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ [Container ID]

    Image 9

  • Browse the IP Address to check whether the docker image hosted in docker is running:

    Image 10

  • Now, you have the image on your local machine. So you need to publish it to some place from where Azure Service Fabric can download or rather anyone else can download and use your app. You can either publish it to public docker hub repository or publish it to Azure Container repository.
  • Follow the steps given below to publish to your repository.
    1. docker login --username docker4asf --password ************
    2. docker push docker4asf/apiimage

    Image 11

    Image 12

    Image 13

  • Once uploaded, you now have the image on the Docker Hub as shown in the below image. We can use this image to deploy our Service Fabric service.

    Image 14

3. Create Container using Image and Publish It to Azure Service Fabric Cluster

  • File -> New Project(Cloud) -> Service Fabric Application

    Image 15

  • Select Container, and provide image name

    Image 16

  • Add port number in ServiceManifest.xml file as shown below:

    Image 17

  • Add policies in ApplicationManifest.xml file as shown below:
    C++
    <Policies>
          <ContainerHostPolicies CodePackageRef="Code" Isolation="process">
            <!-- See https://aka.ms/I7z0p9 for how to encrypt your repository password -->
            <RepositoryCredentials AccountName="docker4asf" 
             Password="**********" PasswordEncrypted="false" />
            <PortBinding ContainerPort="80" EndpointRef="apiimageContainerTypeEndpoint" />
          </ContainerHostPolicies>
        </Policies>
  • EndpointRef in ApplicationManifest file must be the same as Endpoint name from the servicemanifest.xml file.
  • Publish the application.

    Image 18

  • Once the application is successfully published, it will be hosted in Azure Service Fabric cluster. Go to Azure Service Fabric cluster.

    Image 19

  • Container application hosted in local cluster and created Endpoint as shown below:

    Image 20

  • Browse the endpoint, and you can browse ASP.NET WEB API which is hosted in Azure Service Fabric.

    Image 21

Conclusion

In this article, we have learned how to host ASP.NET WEB API to Azure Service Fabric using containers.

Thank you for reading. I hope you liked my article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)