.NET Connector Guide
The .NET Connector, referred to as DotNet connector herein and in Mule application code, allows you to call .NET code from a Mule flow.
.NET Connector Documentation:
Before You Begin
This document assumes that you are familiar with Mule, the Anypoint Studio interface, and Global Elements. Further, it is assumed that you are familiar with the Microsoft .NET Framework.
To complete this tutorial, you need the following components installed on your machine:
-
Anypoint Studio, the graphical interface to Mule. See Download and Launch Anypoint Studio for details
The .NET connector version 1.0 is incompatible with version 2.0. Refer to the .NET connector migration guide if you are an existing user of .NET connector 1.0. |
Installing the .NET Connector in Anypoint Studio
-
In Anypoint Studio, click the Exchange icon in the Studio taskbar.
-
Click Login in Anypoint Exchange.
-
Search for the connector and click Install.
-
Follow the prompts to install the connector.
When Studio has an update, a message displays in the lower right corner, which you can click to install the update.
Example Mule Application Using .NET Connector
This example introduces the .NET Connector and shows you how to configure it to execute a method in a .NET assembly.
The example uses the following Mule message processors:
Component | Description |
---|---|
DotNet Connector |
Execute Microsoft .NET code from inside a Mule flow. |
Enables the example application to receive and send HTTP requests. |
|
The set payload transformer sets the payload to whatever you define. The payload can be a literal string or a Mule Expression. |
|
DataWeave is a Mule transformer that delivers simple, yet powerful, visual design of complex data transformations for use in Mule flows. |
Step 1. Creating a .NET Component
To create the .NET component that will be called from Mule, start Visual Studio and create a new C# Class Library project.
To create a new project:
-
Select File > New > Project.
-
In the New Project dialog, select the Class Library template under Visual C#. Name the Project “Test.SampleComponent”.
-
Create a class named
Sample
with the following code:namespace Test.SampleComponent { public class Sample { public object ExecuteComplex(Person person) { person.Name += " updated from .net"; person.MyRide.Brand = person.MyRide.Brand.Replace("GM", Chevrolet"); person.MyRide.Model = person.MyRide.Model + " - " + "400x"; person.MyRide.ExteriorColor.Name += "ISH"; person.MyRide.ExteriorColor.RGB = "no clue"; return person; } } }
-
Compile the project to create the
Test.SampleComponent.dll
assembly. -
Copy the .dll file to the
C:\Sample
directory.
Step 2. Creating a New Anypoint Studio Project
To begin building this application, start Anypoint Studio and create a new project.
-
Select File > New > Mule Project.
-
In the New Mule Project configuration menu, provide a name for this project:
dotnet_demo
. -
Click Finish.
A new project opens with a blank canvas for building the flow, and the palette with Message Processors to the right.
Step 3. Creating a .NET Global Element
To create and configure a .NET global element, follow these steps:
-
Click the Global Elements tab at the base of the canvas, then click Create.
-
Use DotNet Connector as filter to locate, then select the Global Type:
-
Choose DotNet: External Assembly and click OK.
-
Configure the External Assembly global type:
Parameter Value Name
DotNet_External_Assembly
Scope
Transient
Grant Full Trust to the .NET assembly
True (select the check box)
Declared methods only
True (select the check box)
Assembly: Path
Path to the Test.SampleComponent.dll file
You will reference this global element when configuring the DotNet connector.
Step 4. Creating a Demo Flow with Studio Visual Editor
-
Drag an HTTP connector onto the canvas, then select it to open the properties editor console.
-
Add a new HTTP Listener Configuration global element:
-
In Basic Settings, click the Add button:
-
Configure the following HTTP parameters:
Field Value Port
8081
Path
dotnet
Host
localhost
Exchange Patterns
request-response
Display Name
HTTP (or any other name you prefer)
-
-
Reference the HTTP Listener Configuration global element you created in the previous step:
-
Drag a Set payload transformer onto the canvas, then select it to open the properties editor console.
-
Configure the required filter parameters as follows:
Field Value Value
{ "name" : "bar", "lastName" : "foo", "id" : 1, "myRide" : { "Model" : "Coupe", "Brand" : "GM", "Color" : { "Name" : "red", "RGB" : "123,220,213" } } }}
Display Name
Set Payload (or any other name you prefer)
The string you enter in the Value field represents a serialized JSON object for a Person class, defined in Java:
namespace Test.SampleComponent{ public class Person { public string Name { get; set; } public int Id { get; set; } public string LastName { get; set; } public Car MyRide { get; set; } } public class Car { public string Model { get; set; } public string Brand { get; set; } public Color ExteriorColor { get; set; } } }
-
Create a JSON sample file in your project named input.json and copy the following content into it for the DataWeave to use as example input.
"person" : { "name" : "bar", "lastName" : "foo", "id" : 1, "myRide" : { "Model" : "Coupe", "Brand" : "GM", "Color" : { "Name" : "red", "RGB" : "123,220,213" } } }}
-
Drag a Transform Message component from the palette, and place it into the canvas after the Set Payload transformer.
-
In the Transform Message component, click Define metadata.
-
Click the "Add" button to enter a type id for the JSON sample file you are going to reference. This will be the label for your input.
-
Similarly define your output metadata back in the Transform Message properties editor if the metadata from the .NET connector has not already been sensed by DataSense.
-
Drag the .NET connector onto the palette, then place it into the canvas after the set payload transformer. Configure the DotNet connector as shown below.
The “Type” dropdown in the .NET connector properties is the .NET type that will be reflected upon to see which method it should call. The “Method” reference is the method on the type that was selected in the “Type” dropdown which will be invoked by the connector.
Field Value Operation
Execute
Method
Test.SampleComponent.Sample.ExecuteComplex(Test.SampleComponent.Person person)
Display Name
DotNet Connector (or any other name you prefer)
Connector Configuration
DotNet_External_Assembly
Note that the Config Reference field references the DotNet global element created previously.
After completing the above steps, your application flow should look like this:
Creating a Demo Flow with XML Code
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:dotnet="http://www.mulesoft.org/schema/mule/dotnet" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/dotnet http://www.mulesoft.org/schema/mule/dotnet/current/mule-dotnet.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<dotnet:externalConfig name="DotNet__External_Assembly" scope="Transient" path="C:\Samples\Test.SampleComponent.dll" doc:name="DotNet: External Assembly"/>
<flow name="dotnet-connectorFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<set-payload value="{ "name" : "bar", "lastName" : "foo", "id" : 1, "myRide" : { "Model" : "Coupe", "Brand" : "GM", "Color" : { "Name" : "red", "RGB" : "123,220,213" } } }}" doc:name="Set Payload"/>
<dw:transform-message metadata:id="518e1209-f93d-4bba-bd59-1d6607d40135" doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
}]]></dw:set-payload>
</dw:transform-message>
<dotnet:execute config-ref="DotNet__External_Assembly" methodName="Test.SampleComponent.Sample.ExecuteComplex(Test.SampleComponent.Person person) " doc:name="DotNet"/>
</flow>
</mule>
Step 5. Running the Application
You are now ready to run the project! First, you can test run the application from Studio:
-
Right-click your application in the Package Explorer pane.
-
Select Run As > Mule Application.
-
Fire up a browser and go to
http://localhost:8081/dotnet/?name=foo&age=10
to see the JSON response.
Step 6. About the Example Application
The flow you built in Anypoint Studio contains message processors – including the HTTP Connector, Data Mapper, Set Payload Transformer and the .NET Connector — and it is the "Mule messages" that carry data between these message processors.
A Mule message contains the following components:
-
Payload: The actual data contained in the message
-
Properties: Message metadata, which can include user-defined parameters
In this example, we can see the .NET connector was able to receive parameters from Mule, and to create and return a new message payload that was routed by Mule back to the caller. The .NET Connector allows .NET components to be used to provide custom logic to Mule flows.
See Also
-
For code samples that illustrate more advanced scenarios, refer to the dotnet-connector-samples.zip and the dotnet-connector-sdk.zip.