Often you will find a lot of legacy code residing in .NET code in the project ecosystem.
These codes can have some good business logic implemented and reinventing the wheel in Mule might sometimes
get messy and tiresome.
Mule has support for handling this scenario as well. We can use the MuleSoft Microsoft .NET Connector to use these legacy codes in a mule app.
Here, I will show an end-to-end method of integrating with .NET code.
So, let’s get started.
What is DLL ?
A DLL is a library that contains code and data that can be used by more than one program at the same time. This contains code that can be reused by many other programs thus adding a sense of reusability.
For Java folks: Consider this as JARs.👌
Creating the .NET DLL
You will need Visual Studio to create a DLL file. 💬
Let’s see the process:
1. We will create a C# Class Library
2. Next we will name the solution and create the application
3. Finally we will create a simple calculator app and code in C# as below:
4. On building the project we can see that the DLL files get created as \bin\Debug\netstandard2.0\CalculatorApp.dll
This completes the work needed for the .NET code. Now, we will use this in our Mule app.
We will create a new Mule App. I named it as netapp(you can name anything 😄)
Firstly, copy the generated DLL files to the src/resource directory of your mule app.
Copy the dll and pdb files as below:
This will be then referenced in the connector.
- Microsoft .NET Connector can be added from Exchange as below:
Once added we can see it in the Mule Palette.
- Next, we will create the connector config
Select Connection as Resource since we have our DLL present in the resource folder.
The scope can be singleton so that one instance is created for all application calls.
Next, add the file name in the path as below:
On clicking Test Connection, we should get the below response:
This completes our Connector configuration.
- Now, we will create the flows for our app and add Execute operation
Create a simple HTTP Listener and add Execute from the .NET Connector
- Now, let’s see what the execute operation looks like.
We will add the Connector configuration as the one created earlier and on the click refresh button for Method Info Type, we will find the details populated as above.
All the functions we wrote using Visual Studio for the Library are available in the method.
For this sum operation, we will select
int sum(int a, int b) (CalculatorApp.Calculator, CalculatorApp, Version=220.127.116.11, Culture=neutral, PublicKeyToken=null | sum(System.Int32 a, System.Int32 b) -> System.Int32)
For input to the method we can use the Arguments and I am passing two query parameters as input namely number1 and number2.
These values will be then passed to our DLL and it will return the response.
- Finally a Transform message to get back the response.
Now, let’s run the app and pass two query params.
- Now, we will test it using postman and pass 4, 5 as input
Thus, we can see the output coming as expected. This shows the capability of talking to a .NET DLL from a Mule app.
Similarly, we can complete all other calculator operations and the end to end application flow should look like below:
Point to note: The .NET Connector needs a .NET framework in order to execute.
Hope you liked it. Cheers!