Hello AWS Microservice Extractor for .NET, Let's Work on that Legacy App
Tags: aws, dotnet, microservices, migration
Published: 2021-12-08 15:23:30

#Background
Organizations are increasingly facing challenges while modernizing their legacy applications especially those their business depends on for day-to-day operations. Many are actively looking to progressively decompose large monoliths (legacy or not) into microservices as these systems become too large to deal with.

There are different strategies, techniques and patterns teams can use to break down a monolithic application. Recently AWS introduced the [AWS Microservice Extractor for .NET](https://aws.amazon.com/microservice-extractor/), a new tool that helps customers on their path to extract microservices from their monolithic applications. It simplifies the process of refactoring applications into independent services using source code analysis and visual representations.

In this tutorial, we look at how we can use the [AWS Microservice Extractor for .NET](https://aws.amazon.com/microservice-extractor/) on a real-world application to extract a service.

At the time of writing the AWS Microservice Extractor for .NET supports only .NET Framework CLR 4.0+ ASP.NET web service applications hosted on IIS and the extraction only supports CLR 4.7.0+. Therefore I decided to use the source of one of the Web API projects I worked on in the past which was built on .NET 4.6.1 and which I've upgraded to 4.7.2. Luckily the upgrade was smooth.

> I do not own the source code of this application and I was only a contributor. Therefore I made sure not to share any code. I also disabled data sharing during analysis.

It is only worth noting that AWS Microservice Extractor for .NET is available for use at no cost which is phenomenal.

#Preparations
To prepare the environment, I followed the instructions in the [documentation](https://docs.aws.amazon.com/microservice-extractor/latest/userguide/microservice-extractor-prerequisites.html).

I created a new IAM user and attached a custom IAM policy

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Resource": [
        "*"
      ],
      "Action": [
        "serviceextract:GetConfig"
      ]
    },
    {
      "Sid": "SectionForMetricsService",
      "Effect": "Allow",
      "Action": "execute-api:invoke",
      "Resource": [
        "arn:aws:execute-api:us-east-1:*:*/prod/POST/put-metric-data",
        "arn:aws:execute-api:us-east-1:*:*/prod/POST/put-log-data"
      ]
    }
  ]
}
```
I then configured my AWS CLI profile and installed the AWS Microservice Extractor for .NET tool.

![Setup the tool](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1cyttdbd3xme95ha558s.png)

I did not need to do the rest of the steps since my Windows VM already had Visual Studio 2019 installed with multiple versions of the .NET framework.

After the quick configuration the setup was successful.

![Setup successful](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zr0gkbu9josufdi6lcy5.png)

#Onboarding
After downloading the application code on my machine I was ready to start the analysis.

I clicked on Onboard application and I've added the solution file which points to the Web API application. I did not select the runtime profiling data.

![Onboard application](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w19896i0eojajdlzq2sa.png)

![Onboarding in progress](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3h4outqlfr8927sjpat.png)

After a few minutes, the application was onboarded successfully.

> It is worth noting that the tool includes analysis for .NET core portability which is great.

![Onboarding successful](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtwsmcfgnes3ariv92hr.png)

#Visualizing
Now comes the exciting part. My application was analyzed and I was ready to see the visual representation.

The purpose of this visualization exercise is to learn more about the application components, dependencies and metrics to "quickly" identify which could be refactored into smaller service. 

![Visualize the app high level](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jn8dq5yw0ca62rzhsext.png)

Alright, this was a bit surprising at first glance with all the different connections. After going through the [documentation](https://docs.aws.amazon.com/microservice-extractor/latest/userguide/microservice-extractor-use-visualization.html) I was able to navigate the interface slightly better and identity some of the services I have. I used the filter to look up my SpeakerService.

![Use the filter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ulalpnwfr00ae3kuttgg.png)

To understand the dependencies of the service, I clicked on it and I was presented with a nice view of the edges. Superb!

![Visualize edges](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1wgznbe2nkqb5akh3hc.png) 

Isolating the service in a separate part of the graph was no trivial task. After some battling, I managed to get the components grouped and ready for extraction.

![Group service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tcg5xdryf4anllenwas4.png) 

#Extracting
Exhilarated and curious, I hit the extract group as service button.

![Extract group as service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1w6uvre36thikhsrs5h7.png)

I entered a few details about the service and then select which invocation method i want to use.

![Service details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5f1p4h4rjo9wbddgtr80.png)

![Invocation method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q9tcpqv7gqeys5j5rtsr.png)

A few rounds of trial and error between the visual designer and the extractor and I was still getting this. The extraction halted at 60%.

```
Command failed: error while building and applying rules to transform the MicroService: error while updating build files for microservice: unable to remove some projects from sln
```
It was one of the `infrastructure` projects that it couldn't remove from the solution.

Nevertheless, the extraction of the `SpeakerService` completed according to the application details screen and also the logs.

![Service extracted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kepa3g4g0t2z0d1z4nwo.png)

The log file can be found in the installation directly under /logs/drift-extraction.log.

The extracted service was located in the installation directory.

![Extracted service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nipf23zk8ko6l67gn2qd.png)

Opening the solution of the ApiIntegrationService in Visual Studio revealed multiple missing "infrastructure" projects which is probably why the extraction failed.

I can take it from here and add the missing dependencies and get the service in a build-able state.

#Known Limits and Concerns
This [documentation](https://docs.aws.amazon.com/microservice-extractor/latest/userguide/microservice-extractor-limits.html) article mentions a few limits that you need to know before you start using it.

When extracting a microservice, you need to decide what to do with the building blocks and "shared" code. Do you publish those to nuget package (i.e. internal feed) and reference them, do you use code duplication by duplicating only what's needed etc. It's your call. The extractor however did not do very well with shared projects for me. I was not sure if I need to include all of these dependencies in the group. I believe there is additional manual work to be done.

#Conclusion
AWS Microservice Extractor for .NET is a fantastic tool. It offers great analysis and visualization capabilities with somehow limited or dissatisfying extraction capabilities (at least for me). 

It still provides a lot of assistance in your migration and can get you started on the right path. Any help is greatly required and appreciated. 

You still need to touch both the old application and the new extracted service, maybe add some missing dependencies.
 
Not the glorious ending I was hoping for but nonetheless the start of a compelling journey. The tool was recently released and I expect great things for the future. 

P.S: I will update the article with any new findings and if I manage to get a smooth extraction for the service.