Created
December 5, 2018 11:40
-
-
Save OlegKarasik/58745c29c3c80bc0806431b4c848db80 to your computer and use it in GitHub Desktop.
Verify Stateless Service life-cycle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static class Program | |
{ | |
private static void Main() | |
{ | |
try | |
{ | |
ServiceRuntime.RegisterServiceAsync("SlowStatelessServiceType", | |
context => new SlowStatelessService(context)).GetAwaiter().GetResult(); | |
Thread.Sleep(Timeout.Infinite); | |
} | |
catch | |
{ | |
throw; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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