Hi guys,
I have a strange issue. This is my AppHost
public class MaresAppHost : AppSelfHostBase
{
public MaresAppHost(Assembly[] assembliesWithServices)
: base("Mares API", assembliesWithServices)
{
MaresEnvironment.Instance.RegisterLicense();
}
public string HostUrl => $"http://*:{ApplicationVariables.Instance.APIPort}/";
public override void Configure(Container container)
{
ServiceEnvironment.Container = container;
Plugins.Add(new ServerEventsFeature());
//Plugins.Add(new SwaggerFeature());
//registro tutte le rotte
Routes.AddFromAssembly(base.ServiceAssemblies.ToArray());
var redisHost = ApplicationVariables.Instance.RedisHost;
if (!string.IsNullOrEmpty(redisHost))
{
container.Register<IRedisClientsManager>(
new RedisManagerPool(redisHost));
container.Register<IServerEvents>(c =>
new RedisServerEvents(c.Resolve<IRedisClientsManager>()));
container.Resolve<IServerEvents>().Start();
}
//Configs the host
SetConfig(new HostConfig
{
DebugMode = true,
EnableFeatures = Feature.All,
});
//Prevents Json serialized to add __type part on json string for anonymous types serialization
//Read this: -http://stackoverflow.com/questions/18842685/servicestack-sessions-doesnt-work-when-using-jsconfig-excludetypeinfo
JsConfig.ExcludeTypeInfo = true;
JsConfig.DateHandler = DateHandler.ISO8601;
}
}
and this is my “wrapper” around the ServiceEventClient
public class MaresApiProxy : IDisposable
{
#region Constructors
public MaresApiProxy():this("http://localhost:50000")
{ }
public MaresApiProxy(string url)
{
this.Url = url;
this.EventClient = new ServerEventsClient(url);
}
#endregion
#region Fields & Properties
public string Url { get; private set; }
public ServerEventsClient EventClient { get; private set; }
protected static string SsidCookie { get; set; }
public string Status => this.EventClient?.Status;
public bool? IsStopped => this.EventClient?.IsStopped;
public DateTime? LastPulseAt => this.EventClient?.LastPulseAt;
#endregion
#region Methods
internal async Task<T> PostNotification<T>(T obj)
where T : INotificationBase, new()
{
JsonServiceClient client = null;
try
{
client = new JsonServiceClient(this.Url);
return await client.PostAsync<T>(obj);
}
catch (WebServiceException ex)
{
return this.GetServiceExceptionResponseStatus<T>(ex);
}
catch (Exception ex)
{
return this.GetExceptionResponseStatus<T>(ex);
}
finally
{
if (client != null)
{
client.Dispose();
client = null;
}
}
}
internal T Post<T>(T obj)
where T : IEntityResult, new()
{
JsonServiceClient client = null;
try
{
client = new JsonServiceClient(this.Url);
return client.Post<T>(obj);
}
catch (WebServiceException ex)
{
return this.GetServiceExceptionResponseStatus<T>(ex);
}
catch (Exception ex)
{
return this.GetExceptionResponseStatus<T>(ex);
}
finally
{
if (client != null)
{
client.Dispose();
client = null;
}
}
}
internal T Get<T, K>(K request)
where T : IEntityResult, new()
{
JsonServiceClient client = null;
try
{
client = new JsonServiceClient(Url);
T response = client.Get<T>(request);
return response;
}
catch (WebServiceException ex)
{
return this.GetServiceExceptionResponseStatus<T>(ex);
}
catch (Exception ex)
{
return this.GetExceptionResponseStatus<T>(ex);
}
finally
{
if (client != null)
{
client.Dispose();
client = null;
}
}
}
internal async Task<T> GetAsync<T, K>(K request)
where T : IEntityResult, new()
{
JsonServiceClient client = null;
try
{
client = new JsonServiceClient(Url);
T response = await client.GetAsync<T>(request);
return response;
}
catch (WebServiceException ex)
{
return this.GetServiceExceptionResponseStatus<T>(ex);
}
catch (Exception ex)
{
return this.GetExceptionResponseStatus<T>(ex);
}
finally
{
if (client != null)
{
client.Dispose();
client = null;
}
}
}
protected T GetServiceExceptionResponseStatus<T>(WebServiceException ex)
where T : IEntityResult, new()
{
T errorResponse = new T();
errorResponse.HasError = true;
errorResponse.StatusCode = ex.StatusCode;
if (ex.ResponseStatus != null)
errorResponse.ResponseStatus = ex.ResponseStatus;
else
errorResponse.ResponseStatus = new ResponseStatus()
{
ErrorCode = ex.ErrorCode,
Message = ex.Message,
StackTrace = ex.StackTrace
};
return errorResponse;
}
protected T GetExceptionResponseStatus<T>(Exception ex)
where T : IEntityResult, new()
{
T errorResponse = new T();
errorResponse.HasError = true;
errorResponse.StatusCode = 500;
errorResponse.ResponseStatus = new ResponseStatus()
{
ErrorCode = "500",
Message = ex.Message,
StackTrace = ex.StackTrace
};
return errorResponse;
}
public void Dispose()
{
if (this.EventClient == null) return;
this.EventClient.Dispose();
this.EventClient = null;
}
public void Start()
{
this.EventClient.Start();
}
public void StartListening(string channel)
{
this.EventClient.Update(new[] { channel }, null);
}
public void StopListening(string channel)
{
this.EventClient.Update(null, new string[] { channel });
}
public void AddListener<T>(Action<T> listenerAction)
{
string name = typeof(T).Name;
this.EventClient.Handlers[name] = (client, msg) =>
{
T obj = msg.Json.FromJson<T>();
listenerAction(obj);
};
}
#endregion
}
and this is what happen: there are 2 applications, 1 pubblisher and 1 subscriber. The pubblisher push notifications every 500ms and the subscriber display that data on the screen. During the night the subscriber stop to receive notifications, even if the pubblisher still push them, I tryed restarting the subscriber application without luck, restarting pubblisher application without luck, once restarted the apphost everything start to work again. AppHost was active and fully functional, in fact every call to its methods works perfectly, only notifications were not working.
What is the best practice to keep long connection active? I checked the pc, and there aren’t any power off options
Best regards
Enrico