Getting Object Reference not set to instance in serializetoJson

Pretty simple object… code below the stack here, why is GetDbConnection being called!!! Using 4.0.62 also tried older version 4.0.4x

at ServiceStack.ServiceStackHost.GetDbConnection (IRequest req) [0x00085] in :0
at ServiceStack.Service.get_Db () [0x00016] in :0
at (wrapper dynamic-method) System.Object:lambda_method (System.Runtime.CompilerServices.Closure,ShipNow.Common.UIMessage)
at ServiceStack.Text.Common.WriteType2[T,TSerializer].WriteProperties (System.IO.TextWriter writer, System.Object value) [0x0013e] in <filename unknown>:0 at ServiceStack.Text.Json.JsonWriter1[T].WriteRootObject (System.IO.TextWriter writer, System.Object value) [0x0000b] in :0
at ServiceStack.Text.JsonSerializer.SerializeToString[T] (ServiceStack.Text.T value) [0x000c8] in :0
at ShipNow.Common.UIMessage.toJson () [0x00003] in /Users/kevin/Projects/ShipNow/ShipNow.Common/UIMessage.cs:66

using System;
using ServiceStack;
using ServiceStack.Text;


namespace ShipNow.Common
{
	public class UIMessage : Service
	{
		
		private IServerEvents ServerEvents { get; set; }

		public bool successfull { get; set; }
		public string message { get; set; }
		public string objectData { get; set; }


		private string _channel { get; set; }
		private string _selector { get; set;}

		public UIMessage()
		{

		}

		public UIMessage(string channel, string selector, bool success, string _message, Object objData)
		{
			successfull = success;
			message = message;
			_channel = channel;
			_selector = selector;

			if (objData != null)
			{
				try
				{
					objectData = JsonSerializer.SerializeToString(objData);
				}
				catch (Exception e)
				{
					Util.ServiceLog.Error("Error serializing object for UIMEssage", e);

			}
			}


		
		}

		public void notify()
		{
			if (!_channel.IsNullOrEmpty() && !_selector.IsNullOrEmpty())
			{
				ServerEvents.NotifyChannel(_channel, _selector, this.toJson());
			}
			else {
				Util.ServiceLog.Warn("TRied to notify without a channel/selector");
			}

		}


		public string toJson()
		{
			try
			{
				return JsonSerializer.SerializeToString(this);
			}
			catch (Exception e)
			{
				Util.ServiceLog.Error("Error serializing object for UIMEssage", e);
				return "{\"message\":\"error serializing\"}";
			}
			//return JsonSerializer.SerializeToString(this);
		}
	}
}

Just realized its a Service subclass… not a simple object. will try to compartmentalize a little better was just being lazy getting the IServerEvents…

The StackTrace suggests you’re trying to serialize the entire UIMessage Service? You definitely don’t want to be calling:

return JsonSerializer.SerializeToString(this);

On a Service or anything else really since it suggests you’re trying to serialize a class with a behavior which will likely lead to issues like this. The result is what’s causing the Serializer to serialize Db public property which is what calls AppHost.GetDbConnection().

Ya, figured that out… can’t serialize it. Have another related question about getting ServerEvents client, will post a new topic…