The Following Code Snippets display how JAMS can be Integrated into a Custom .NET Solution through C# and VB.NET
C#
using System; using System.Collections.Generic; using System.IO; using System.Text; using MVPSI.JAMS.Host; namespace JAMSIntegratedDll { public class JAMSExecute : IJAMSHost { private FileStream fs; private FinalResults results; #region IJAMSHost Members public void Initialize(IServiceProvider serviceProvider, Dictionary<string, object> attributes, Dictionary<string, object> parameters) { results = new FinalResults(); //Initialize the text file. try { fs = new FileStream(parameters["FileName"].ToString(), FileMode.Create); } catch (Exception ex) { //An error occured during Initialize method WriteErrorInfo(ex, "Initialize"); } } public FinalResults Execute(IServiceProvider serviceProvider, Dictionary<string, object> attributes, Dictionary<string, object> parameters) { //If there were no errors during Initialize if (results.FinalSeverity != 3) { //Execute the job. try { var encoding = new ASCIIEncoding(); byte[] bytes = encoding.GetBytes(parameters["Message"].ToString()); fs.Write(bytes, 0, bytes.Length); //No errors occured results.FinalSeverity = 0; results.FinalStatus = "The operation completed successfully"; results.FinalStatusCode = 0; } catch (Exception ex) { //An error occured during execution WriteErrorInfo(ex, "Execute"); } } return results; } public void Cleanup(IServiceProvider serviceProvider, Dictionary<string, object> attributes, Dictionary<string, object> parameters) { //Dispose of the FileStream if (fs != null) { fs.Dispose(); } } #endregion private void WriteErrorInfo(Exception ex, string methodName) { //Any messages written to the Console will appear in the JAMS Job log Console.WriteLine(ex.ToString()); results.FinalSeverity = 3; results.FinalStatus = "Execption occured during " + methodName; results.FinalStatusCode = 1; } } }
VB.NET
Imports System.IO Imports MVPSI.JAMS.Host Imports System.Text Public Class JAMSExecute Implements IJAMSHost Private fs As FileStream Private results As FinalResults Public Sub Initialize (ByVal serviceProvider As IServiceProvider, ByVal attributes As Dictionary(Of String, Object), _ ByVal parameters As Dictionary(Of String, Object)) Implements IJAMSHost.Initialize results = New FinalResults() 'Initialize the text file. Try fs = New FileStream (parameters ("FileName").ToString(), FileMode.Create) Catch ex As Exception 'An error occured during Initialize method WriteErrorInfo (ex, "Initialize") End Try End Sub Public Function Execute (ByVal serviceProvider As IServiceProvider, ByVal attributes As Dictionary(Of String,Object), _ ByVal parameters As Dictionary(Of String,Object)) As FinalResults _ Implements IJAMSHost.Execute 'If there were no errors during Initialize If results.FinalSeverity <> 3 Then 'Execute the job. Try Dim convertEncoding As ASCIIEncoding = New ASCIIEncoding() Dim bytes As Byte() = convertEncoding.GetBytes (parameters ("Message").ToString()) fs.Write (bytes, 0, bytes.Length) 'No errors occured results.FinalSeverity = 0 results.FinalStatus = "The operation completed successfully" results.FinalStatusCode = 0 Catch ex As Exception 'An error occured during execution WriteErrorInfo (ex, "Execute") End Try End If Return results End Function Public Sub Cleanup (ByVal serviceProvider As IServiceProvider, ByVal attributes As Dictionary(Of String,Object), _ ByVal parameters As Dictionary(Of String,Object)) Implements IJAMSHost.Cleanup 'Dispose of the FileStream If fs IsNot Nothing Then fs.Dispose() End If End Sub Private Sub WriteErrorInfo (ByVal ex As Exception, ByVal methodName As String) 'Any messages written to the Console will appear in the JAMS Job log Console.WriteLine (ex.ToString()) results.FinalSeverity = 3 results.FinalStatus = "Execption occured during " + methodName results.FinalStatusCode = 1 End Sub End Class
Comments