Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

DotNet Connector Guide

The DotNet Connector allows you to call .NET code from a Mule flow.

DotNet Documents:

Prerequisites

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:

The .NET connector version 1.0 is incompatible with version 2.0. Please refer to the .NET connector migration guide if you are an existing user of .NET connector 1.0.

Installing the DotNet Connector in Anypoint Studio

You can install a connector in Anypoint Studio using the instructions in Installing a Connector from Anypoint Exchange.  

Example

This example introduces the DotNet 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.

HTTP Connector

Enables the example application to receive and send HTTP requests.

Set Payload Transformer

The set payload transformer sets the payload to whatever you define. The payload can be a literal string or a Mule Expression.

DataMapper

The DataMapper 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:

  1. Select File > New > Project.

  2. In the New Project dialog, select the Class Library template under Visual C#.  Name the Project “Test.SampleComponent”.

    dotnetcomponent

  3. Create a class named Sample with the following code:

    
                 
              
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    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;
           }
       }
    }
  4. Compile the project to create the Test.SampleComponent.dll assembly.

  5. 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.

  1. Select File > New > Mule Project.

  2. In the New Mule Project configuration menu, provide a name for this project: dotnet_demo.

  3. 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 DotNet Global Element

To create and configure a DotNet global element, follow these steps:

  1. Click the Global Elements tab at the base of the canvas, then click Create.

  2. Use DotNet Connector as filter to locate, then select the Global Type:

    global-type

  3. Choose DotNet: External Assembly and click OK.

  4. Configure the External Assembly global type:

    external-assembly

    Parameter Value

    Name

    Dot_Net_External_Assembly

    Enable DataSense

    True (select the check box)

    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

  1. Drag an HTTP connector into the canvas, then select it to open the properties editor console.

  2. Add a new HTTP Listener Configuration global element:

    1. In General Settings, click Add button:

      httplistener

    2. Configure the following HTTP parameters:

      httparameters

      Field Value

      Port

      8081

      Path

      dotnet

      Host

      localhost

      Exchange Patterns

      request-response

      Display Name

      HTTP (or any other name you prefer)

  3. Reference the HTTP Listener Configuration global element:

    httpreference

  4. Drag a set payload transformer into the canvas, then select it to open the properties editor console.

  5. Configure the required filter parameters as follows:

    set-payload-1

    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:

    
                
                         
                      
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
    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;
           }
        }
    }
  6. Drag a DataMapper from the palette, and place it into the canvas after the Set Payload transformer.

  7. Configure the parameters as follows:

    jsontoexecutecomplex

    Field Value

    Display Name

    JSON to ExecuteComplex (or any other name you prefer)

    Input

    Type

    JSON

    From Example

    True (Check)

    Sample

    Enter the path to the input.json sample file.

    Before you run this application, create a JSON sample file named input.json and copy the following content into it: 

    
                
                         
                      
    1
    
    "person" : { "name" : "bar", "lastName" :  "foo", "id" : 1, "myRide" : { "Model" : "Coupe", "Brand" : "GM", "Color" : { "Name" : "red", "RGB" : "123,220,213" }  } }}
  8. Click Create Mapping.

    datamapper-mappingscreen

  9. Drag the DotNet connector in the Palette, then place it into the canvas after the set payload transformer. Configure the DotNet connector as shown below.

    dotnet-connector-screen

    The “Type” drop down 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 name

    Test.SampleComponent.Sample.ExecuteComplex(Test.SampleComponent.Person person) 

    Display Name

    DotNet Connector (or any other name you prefer)

    Config Reference

    Dot_Net_Resource_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:

complete-application-flow


    
             
          
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" 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" version="EE-3.6.1" 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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.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/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd 
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd"> 
 <dotnet:externalConfig name="DotNet_External_Assembly" scope="Transient" path="C:\Samples\Test.SampleComponent.dll" doc:name="DotNet: External Assembly"/>
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" basePath="dotnet" doc:name="HTTP Listener Configuration"/>
<http:connector name="HTTP_HTTPS" cookieSpec="netscape" validateConnections="true" sendBufferSize="0" receiveBufferSize="0" receiveBacklog="0" clientSoTimeout="10000" serverSoTimeout="10000" socketSoLinger="0" doc:name="HTTP-HTTPS"/>
<data-mapper:config name="JSON_To_ExecuteComplex" transformationGraphPath="json_to_executecomplex.grf" doc:name="JSON_To_ExecuteComplex"/>
 <flow name="dotnet-demoFlow1" doc:name="dotnet-demoFlow1">
 <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
 <data-mapper:transform config-ref="JSON_To_ExecuteComplex" doc:name="JSON To ExecuteComplex" path="dotnet"/>
 <dotnet:execute config-ref="DotNet_External_Assembly" methodName="Test.SampleComponent.Sample, Test.SampleComponent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | ExecuteComplex(Test.SampleComponent.Person person) -> System.Object" 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:

  1. Right-click your application in the Package Explorer pane.

  2. Select Run As > Mule Application.

  3. Fire up a browser and go to http://localhost:8081/dotnet/?name=foo&age=10 to see the results.

    run-application

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 DotNet 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 DotNet 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 DotNet Connector allows .NET components to be used to provide custom logic to Mule flows.

See Also