/
User Storage

User Storage

"User Storage" (new in version 15) is a replacement for the "User Context" feature. "User Context" is an in-memory dictionary which allows users to place arbitrary data into the message context object. The "User Context" is not persisted and therefore is lost when a message reaches a queue device or the end of the channel. The "User Storage" feature builds upon the "User Context" and adds the option to persist referenced objects. This allows you add one or more arbitrary objects in front of a queue, and use them during processing after the queue. 

Objects must be JSON serializable in order to be persisted. We recommend the use of the DataContract and DataMember attributes on your classes.

Sample channel: UserStorageSample.cxn

Add object 'myObject' to the user storage with the key 'test'. Note the use of the UserStorageType.Persistent flag to indicate we want this to be saved along with the message.

context.UserStorage.Add(UserStorageType.Persistent, "test", myObject);

Read the object from the user storage:

// as an object
var myObject = context.UserStorage["test"];

// as a specific type
var myObject = context.UserStorage.Get<TypeOfMyObject>("test");

// as a jObject
var myObject = context.UserStorage.GetAsJObject("test");

For example, this custom code device (sitting before a queue) can add a user-defined object (FileWriterTest) to the user storage. In this case, the FileWriterTest object is defined in the global code.

using System;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using Connexion.Core;
using Connexion.Core.HL7;
using System.Threading.Tasks;
using System.Runtime.Serialization;

namespace Connexion.Device
{ 
  public partial class CustomDevice : BaseCustomDevice
  {
    private CancellationTokenSource m_CancellationToken;
    
    public override void Start()
    {
      m_CancellationToken = new CancellationTokenSource();
      Task.Run(() =>
      {
        var r = new Random();
        var msg = "Sample message";
        
        while(!m_CancellationToken.IsCancellationRequested)
        {          
          var fwt = new FileWriterTest 
          {
            filename = r.Next(0, 9999999).ToString(),
            random2 = r.Next(400, 500).ToString()
          };
          
          var context = MessageChannel.CreateMessageContext(msg);          
          context.UserStorage.Add(UserStorageType.Persistent, "test", fwt);          
          MessageChannel.PostOnChannel(context);
        }
      });
    }

    public override void Stop()
    {
      m_CancellationToken?.Cancel();
    }		
  }
}

When viewing messages with User Storage content in the queue, the attachments tab will display with the contents of the user context:

Most devices which support the UserContext collection also support the UserStorage collection. For example, the file writer device can read values from the user storage using JSON path syntax:

Related content

Concurrent File Writer device
Concurrent File Writer device
More like this
Object Registry Service (Version invariant configuration sharing)
Object Registry Service (Version invariant configuration sharing)
More like this
Runtime Value Storage
Runtime Value Storage
More like this
File Writer device
File Writer device
More like this
Simple Http Server
Simple Http Server
More like this
Logging in Custom Devices
Logging in Custom Devices
More like this