01 December, 2010

Microsoft Dynamics 10 eConnect C# 4.0 .NET Serialization In Memory


*Note* - This code was written for Dynamics GP 10. If you are using Dynamics GP 2010 see this updated article.

I was recently tasked with creating an eConnect integration between Microsoft Dynamics GP 10 and a custom SharePoint 2010 Portal designed to replace the GP Item Maintenance process with workflow and departmental routing.

Most of the eConnect examples I see online serialize the eConnect object to a file before loading it into an XmlDocument. This can cause unnecessary permissions concerns, and worse, if your application runs under the context of IIS or SharePoint you may not have a file system available to you at all. This was the case in my scenario. A much better approach is to serialize the object in memory.

Below is an example of eConnect serialization in memory using Microsoft C# .NET 4.0.

Assuming you have already installed the eConnect SDK 10, reference the following assemblies:

Microsoft.Dynamics.GP.eConnect.dll
Microsoft.Dynamics.GP.eConnect.MiscRoutines.dll
Microsoft.Dynamics.GP.eConnect.Serialization.dll
System.EnterpriseServices.dll

And add the following using statements:

using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Dynamics.GP.eConnect;
using Microsoft.Dynamics.GP.eConnect.Serialization;
Here is the code to serialize the eConnectType object in memory and load it into an XmlDocument which is then passed to the eConnect_EntryPoint method:


string connectionString = string.Empty; // Set up a valid connection string.

eConnectMethods econnectMethods = new eConnectMethods();
eConnectType eConnectType = new eConnectType();

// Instantiate and populate the specific eConnect types needed.
// Don't forget to add them to eConnectType once they are set up.

// We're done building the object model, now serialize it in memory.
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xmlSerializer = new XmlSerializer(eConnectType.GetType());
xmlSerializer.Serialize(memoryStream, eConnectType);
memoryStream.Position = 0;

// Create a new XmlDocument from the in memory serialized object model.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(memoryStream);
memoryStream.Close();

econnectMethods.eConnect_EntryPoint(connectionString, EnumTypes.ConnectionStringType.SqlClient, xmlDocument.OuterXml, EnumTypes.SchemaValidationType.None, string.Empty);

Hope it helps!


2 comments:

Mariano Gomez said...

Bryan,

Great to see you posting! This is a really cool article and I am sure I will see more from you soon.

MG.-
Mariano Gomez, MVP

Stanley Glass said...

BTW you can do this with .NET 2 as well. the Memory Serialization has been around for a while. I have been doing this in my code for some time, but for some reason your code gave me the idea of refactoring the actual process that serializes the object. Kind of a "DUH!" moment I guess.