Common Event Code passing EventItemContext

  • 24 February 2010
  • 10 replies
  • 4 views

Badge +1

I just 'inherited' a K2 workflow where 30 code-behind events are identical, but continue on to different activities. I'd like to be able to maintain those 30 events in just one place, rather than all 30. So, I tried to create code like:


public void Main(Project_5bd80e5cfda54243b372945364e45280.EventItemContext_5d0e69be76f940f7ac52232d6825257e K2) { WriteOracleUpdates(K2); }


 


public void WriteOracleUpdates(Project_5bd80e5cfda54243b372945364e45280.EventItemContext_5d0e69be76f940f7ac52232d6825257e K2) {


string connectionString = K2.StringTable["GBIP_ICO_Connection_String"];


//Lots of other code here


}


 


However, in attempting to call this code from other Events, I am running into type casting issues, giving me a 'Cannot Convert Type

Project_5bd80e5cfda54243b372945364e45280.



EventItemContext_65196629d1cb4a92a35c45659291c210

 



 



to

Project_5bd80e5cfda54243b372945364e45280.



EventItemContext_5d0e69be76f940f7ac52232d6825257e


Does anyone have any ideas how to get around this? I prefer not to have to pass every single variable, as I have 40 variables. Any help would be appreciated.


This all is brand new to me, and while I can follow through what's going on, my experience in C# and K2 is minimal at this point.


Thanks!


10 replies

Badge +8

You can work with the ServerEventContext object, it is the base class for the server events.


Update your server event code to use this:

 


 


WriteOracleUpdates(K2.GetServerContext()); // Call your generic function here


 


Then change your custom function to accept the correct type:


public void WriteOracleUpdates(ServerEventContext K2) {


string connectionString = K2.StringTable["GBIP_ICO_Connection_String"];


//Lots of other code here



}


 


 

Badge +1

Thank you!


That worked. - Now I just got to figure out how to put this into my K2 project, and connect to it from other events.

Badge +9

Depending on what your 'Lots of Code Here' is, you could put the common code into a referenced assembly OR reference it via the dynamic assembly service (which will give you a nice 'wizardy' way to interface with this method).  The dynamic assembly service complicates deployments a little, but I think the advantages of having that wizard interface during design time outweighs that.

Badge +1

Tim,


Since my code references the ServerEventContext, I'm having a bit of trouble figuring out how to create an assembly. I can't use "using SourceCode.KO;" in an independent assembly: Can't find namespace. - So I've tried adding a .cs class to the project, but can't add it to the project (maybe I just don't know how), and I tried an independent solution, but that's not recognizing the ServerEventContext. - On the bright side, I'm signed up for training in May (but am supposed to be done with it in March).


Thanks for your help,


Gunnar

Badge +8

I was able to easily create a simple class that utilizes the KO. I've added a reference to the SourceCode.KO assembly and called the relevant object as per below:



using System;
using System.Collections.Generic;
using System.Text;
using KO = SourceCode.KO;

namespace TestKO
{
    public class UseKO
{


        public void UpdateField(KO.ServerEventContext Event, String FieldName, String Value)


        {
            Event.ProcessInstance.DataFields[FieldName].Value = Value;
        }


    }


}



 


Then in the process, I added a reference to the custom assembly and used it in a server event as below:

TestKO.UseKO o = new TestKO.UseKO();
o.UpdateField(K2.GetServerContext(), "TestField", "New Value");


Badge +2

Hi, 


I have a problem in sending the ExceptionContext Object to custom assembly with the K2.GetServerContext().  @ the method of the custom assembly the SourceCode.KO.ExceptionContext converts into 'System.Runtime.Remoting.Proxies.__TransparentProxy'.


While getting the process Destination activity name & other details by type casting the object into SourceCode.KO.ExceptionContext throws exception - unable to type cast.


How can I receive the SourceCode.KO.ExceptionContext object in the custom assembly?


Thanks,


Surya.

Badge +1

Hi Surya,
I'm having the exact same issue type casting to SourceCode.KO.ExceptionContext, how did you resolve using this in your custom assembly?
Anyone else have an idea about how this is done?


Thanks Guys

Badge +1

Looks like I answered my question.
Just using K2.GetServerContext() without type casting worked.
And in the custom assembly defining like … Public Sub ProcessExceptionHandler(ByVal K2 As SourceCode.KO.ExceptionContext)
Worked.


Thanks

Badge +2

Hi Sampy,


Does your code worked for the Context type events - line rule, server event,client event etc., If possible can you please share your code!!!


Thanks,


Surya

Badge +1

Surya,
Sorry dint see your msg, was on my vacation!,
Well...Yes the code worked for ExceptionContext, not sure if K2 blackpearl 4.6 fixed this. In any case here is the code...



Call from Exception Rule event in k2:
    public partial class Exception_df7672b71d1c4010833b625bef9d4f5a : IWorkflowContext<HostContext>
    {
        #region - K2 Context -
        private HostContext _K2;
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public HostContext K2
        {
            get { return _K2; }
            set { _K2 = value; }
        }
        #endregion


        #region - Properties_ExecuteCode -
        private void Properties_ExecuteCode(object sender, EventArgs e)
        {


             try
            {
                K2.AddToErrorLog = K2.Configuration.IsErrorLog;
                K2.AddToServerLog = K2.Configuration.IsServerLog;


                My.Custom.Assembly oCA = new My.Custom.Assembly();
                oCA.ProcessMyExceptionHandler(K2.GetServerContext());
             ....
             ....
             ....
             }
             ....
             ....
             ....
     }            
            
            
Code in my custom dll (My.Custom.Assembly) (VB):            
    Public Sub ProcessMyExceptionHandler(ByVal K2 As SourceCode.KO.ExceptionContext)


        K2.AddToErrorLog = False


        Dim strErrMsg As String = "An exception was thrown during execution of a K2 workflow." & System.Environment.NewLine
        strErrMsg &= "Process Name: " & K2.ProcessInstance.Process.Name & System.Environment.NewLine
        strErrMsg &= "Process Folio: " & K2.ProcessInstance.Folio & System.Environment.NewLine            
             ....
             ....
             ....       
       
        Throw New ApplicationException(K2.ExceptionObject.ToString())
    End Sub
   


Hope this helps!!!

Reply