C#, CRM, Microsoft CRM Dynamics, Microsoft Dynamics CRM

TCP/IP based Integrations in CRM Dynamics On Prem

We recently had a requirement to Integrate with a System using MML(Man Machine Language) Commands with CRM Dynamics.

Sending MML commands to systems is done using TCP/IP protocol. Let’s first see the Sequence Diagram for the Integration:

sequencediagram

 

Here is the Source code for a Sample Console App we used for a POC, same code can be used from a Windows service or a Desktop application if required with few tweeks:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace EMATestingConsoleApp
{
    class Program
    {
        // Receiving byte array  
        static byte[] bytes = new byte[1024];
        static Socket senderSock;

        static void Main(string[] args)
        {
            Console.WriteLine("Connect to server? Press any key to continue connecting..");
            Console.ReadKey();

            try
            {
                // Create one SocketPermission for socket access restrictions 
                SocketPermission permission = new SocketPermission(
                    NetworkAccess.Connect,    // Connection permission 
                    TransportType.Tcp,        // Defines transport types 
                    "",                       // Gets the IP addresses 
                    SocketPermission.AllPorts // All ports 
                    );

                // Ensures the code to have permission to access a Socket 
                permission.Demand();

                var ipAddr = IPAddress.Parse(System.Configuration.ConfigurationManager.AppSettings["ipaddress"]);
                var port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["port"]);
                // Creates a network endpoint 
                IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, port);

                // Create one Socket object to setup Tcp connection 
                senderSock = new Socket(
                    ipAddr.AddressFamily,// Specifies the addressing scheme 
                    SocketType.Stream,   // The type of socket  
                    ProtocolType.Tcp     // Specifies the protocols  
                    );

                senderSock.NoDelay = false;   // Using the Nagle algorithm 

                // Establishes a connection to a remote host 
                senderSock.Connect(ipEndPoint);
                Console.WriteLine("Socket connected to " + senderSock.RemoteEndPoint.ToString());

                Console.WriteLine("Enter command to be sent to EMA");
                

                try
                {
                    // Sending message 
                    //<Client Quit> is the sign for end of data 
                    string theMessageToSend = Console.ReadLine();
                    byte[] msg = Encoding.Unicode.GetBytes(theMessageToSend + "<Client Quit>");

                    // Sends data to a connected Socket. 
                    int bytesSend = senderSock.Send(msg);

                    ReceiveDataFromServer();

                
                }
                catch (Exception exc) { Console.WriteLine(exc.ToString()); }

                Console.ReadLine();
            }
            catch (Exception exc) { Console.WriteLine(exc.ToString()); }

        }

        private static void ReceiveDataFromServer()
        {
            try
            {
                // Receives data from a bound Socket. 
                int bytesRec = senderSock.Receive(bytes);

                // Converts byte array to string 
                String theMessageToReceive = Encoding.Unicode.GetString(bytes, 0, bytesRec);

                // Continues to read the data till data isn't available 
                while (senderSock.Available > 0)
                {
                    bytesRec = senderSock.Receive(bytes);
                    theMessageToReceive += Encoding.Unicode.GetString(bytes, 0, bytesRec);
                }

                Console.WriteLine("The server reply: " + theMessageToReceive);
                Console.WriteLine("Press any key to disconnect");
                Console.ReadKey();

                // Disables sends and receives on a Socket. 
                senderSock.Shutdown(SocketShutdown.Both);

                //Closes the Socket connection and releases all resources 
                senderSock.Close();
            }
            catch (Exception exc) { Console.WriteLine(exc.ToString()); }
        }

      

    }
}

 

 

 

Advertisements
Standard