Skip to content

Instantly share code, notes, and snippets.

@OlegKarasik
Created December 5, 2018 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OlegKarasik/58745c29c3c80bc0806431b4c848db80 to your computer and use it in GitHub Desktop.
Save OlegKarasik/58745c29c3c80bc0806431b4c848db80 to your computer and use it in GitHub Desktop.
Verify Stateless Service life-cycle
internal static class Program
{
private static void Main()
{
try
{
ServiceRuntime.RegisterServiceAsync("SlowStatelessServiceType",
context => new SlowStatelessService(context)).GetAwaiter().GetResult();
Thread.Sleep(Timeout.Infinite);
}
catch
{
throw;
}
}
}
public sealed class SlowCommunicationListener : ICommunicationListener
{
public async Task<string> OpenAsync(
CancellationToken cancellationToken)
{
await Task.Delay(10000);
return string.Empty;
}
public async Task CloseAsync(
CancellationToken cancellationToken)
{
await Task.Delay(10000);
}
public void Abort()
{
Thread.Sleep(5000);
}
}
internal sealed class SlowStatelessService : StatelessService
{
public SlowStatelessService(StatelessServiceContext context)
: base(context)
{
}
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[]
{
new ServiceInstanceListener(serviceContext => new SlowCommunicationListener())
};
}
protected override async Task OnOpenAsync(CancellationToken cancellationToken)
{
await Task.Delay(20000);
await base.OnOpenAsync(cancellationToken);
}
protected override async Task RunAsync(CancellationToken cancellationToken)
{
for (; ; )
{
if (cancellationToken.IsCancellationRequested)
{
break;
}
Thread.Sleep(100);
}
await Task.Delay(10000);
await base.RunAsync(cancellationToken);
}
protected override async Task OnCloseAsync(CancellationToken cancellationToken)
{
await Task.Delay(10000);
await base.OnCloseAsync(cancellationToken);
}
protected override void OnAbort()
{
base.OnAbort();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment