/// <summary> /// Set timer notification of actor /// </summary> /// <param name="dueTime">due time, first event</param> /// <param name="period">every period</param> public void SetTimer(TimeSpan dueTime, TimeSpan period) { Verify.Assert(_timer == null, "Timer already running"); _timer = new Timer(TimerCallback, null, dueTime, period); ActorManager.Configuration.ActorStartTimer(_workContext.WithMethodName(), ActorKey); }
/// <summary> /// Send request /// </summary> /// <param name="context">work context</param> /// <param name="requestMessage">request message</param> /// <param name="token">cancellation tokens</param> /// <returns>state of HTTP response</returns> public async Task <HttpResponseMessage> SendAsync(IWorkContext context, HttpRequestMessage requestMessage, CancellationToken?token = null) { Verify.IsNotNull(nameof(context), context); Verify.IsNotNull(nameof(requestMessage), requestMessage); context = context.WithMethodName(); token = token ?? CancellationToken.None; HttpResponseMessage response; try { using (var scope = new TimerScope( () => NetEventSource.Log.ActivityStart(context), x => NetEventSource.Log.ActivityStop(context, durationMs: x))) { response = await _client.SendAsync(requestMessage, (CancellationToken)token); } } catch (Exception ex) { NetEventSource.Log.Error(context, nameof(SendAsync), ex); throw; } return(response); }
public async Task CertificateSetGetTest() { const string testData = "Test data, test, data"; IWorkContext context = _workContext.WithMethodName(); var builder = new ContainerBuilder(); builder.RegisterModule(new CertificateAutoFacModule()); ILifetimeScope container = builder.Build(); IActorManager manager = new ActorConfigurationBuilder() .AddCertificateModule(container) .Build() .ToActorManager(); using (ILifetimeScope scopeContainer = container.BeginLifetimeScope()) using (manager) { var key = new LocalCertificateKey(StoreLocation.LocalMachine, StoreName.My, Constants.JwtVaultTestCertificateThumbprint, true); ICertificateActor actor = await manager.CreateProxyAsync <ICertificateActor>(context, key.CreateActorKey()); byte[] rawBytes = Encoding.UTF8.GetBytes(testData); byte[] encryptedBytes = await actor.Encrypt(context, rawBytes); byte[] unencrypted = await actor.Decrypt(context, encryptedBytes); string result = Encoding.UTF8.GetString(unencrypted); result.Should().Be(testData); } await Verify.AssertExceptionAsync <ArgumentException>(async() => await manager.DeactivateAllAsync(_workContext)); }
/// <summary> /// Send request /// </summary> /// <param name="context">work context</param> /// <param name="requestMessage">request message</param> /// <returns>state of HTTP response</returns> private async Task <HttpResponseMessage> SendAsync(IWorkContext context, HttpRequestMessage requestMessage) { context.Verify(nameof(context)).IsNotNull(); requestMessage.Verify(nameof(requestMessage)).IsNotNull(); context = context.WithMethodName(); try { using var scope = new TelemetryActivityScope(context, requestMessage.RequestUri.ToString()); HttpResponseMessage message = await _client.SendAsync(requestMessage, context.CancellationToken); if (!ValidHttpStatusCodes.Any(x => message.StatusCode == x)) { if (EnsureSuccessStatusCode) { message.EnsureSuccessStatusCode(); } } return(message); } catch (Exception ex) { context.Telemetry.Error(context, $"{nameof(SendAsync)} error '{ex.Message}", ex); throw; } }
public async Task CertificateSetGetTest() { IWorkContext context = _workContext.WithMethodName(); var identityRepository = new IdentityInMemoryStore() .Set(_workContext, new IdentityPrincipal(new PrincipalId("*****@*****.**"), IdentityPrincipalType.User).With(new ApiKey("API Key1", DateTime.UtcNow.AddYears(1)))) .Set(_workContext, new IdentityPrincipal(new PrincipalId("*****@*****.**"), IdentityPrincipalType.User).With(new ApiKey("API Key2", DateTime.UtcNow.AddYears(1)))); var builder = new ContainerBuilder(); builder.RegisterModule(new IdentityActorAutoFacModule()); builder.RegisterInstance(identityRepository).As <IIdentityStore>(); ILifetimeScope container = builder.Build(); IActorManager manager = new ActorConfigurationBuilder() .AddIdentityModule(container) .Build() .ToActorManager(); using (ILifetimeScope scopeContainer = container.BeginLifetimeScope()) using (manager) { IIdentityActor clientActor1 = await manager.CreateProxyAsync <IIdentityActor>(context, new ActorKey("*****@*****.**")); IdentityPrincipal client1 = await clientActor1.Get(context); client1.Should().NotBeNull(); client1.PrincipalId.Value.Should().Be("*****@*****.**"); client1.ApiKey.Value.Should().Be("API Key1"); IIdentityActor clientActor2 = await manager.CreateProxyAsync <IIdentityActor>(context, new ActorKey("*****@*****.**")); IdentityPrincipal client2 = await clientActor2.Get(context); client2.Should().NotBeNull(); client2.PrincipalId.Value.Should().Be("*****@*****.**"); client2.ApiKey.Value.Should().Be("API Key2"); await clientActor2.Remove(context); (await clientActor2.Get(context)).Should().BeNull(); identityRepository.Get(context, new PrincipalId("*****@*****.**")).Should().BeNull(); await clientActor2.Set(context, new IdentityPrincipal(new PrincipalId("*****@*****.**"), IdentityPrincipalType.User)); (await clientActor2.Get(context)).Should().NotBeNull(); identityRepository.Get(context, new PrincipalId("*****@*****.**")).Should().NotBeNull(); } await Verify.AssertExceptionAsync <ArgumentException>(async() => await manager.DeactivateAllAsync(_workContext)); }
private void MetricsOutput(IWorkContext context) { context = context.WithMethodName(); IReadOnlyList <MetricSample> samples = _sampler.GetMetrics(true); if (samples.Count == 0) { context.Telemetry.Info(context, "Receive - empty metrics"); return; } int total = samples.Sum(x => x.Count); _messageCount += total; TimeSpan span = TimeSpan.FromSeconds(samples.Sum(x => x.Span.TotalSeconds)); context.Telemetry.Info(context, $"Receive: Total: {total}, Span: {span}, TPS:{total / span.TotalSeconds}"); }
public static async Task <T> TryDeserializeObjectAsync <T>(this HttpResponseMessage message, IWorkContext context) where T : class { Verify.IsNotNull(nameof(context), context); context = context.WithMethodName(); T data; try { var json = await message.Content.ReadAsStringAsync(); data = JsonConvert.DeserializeObject <T>(json); } catch { return(null); } return(data); }
public static async Task <T> DeserializeObjectAsync <T>(this HttpResponseMessage message, IWorkContext context) { Verify.IsNotNull(nameof(context), context); context = context.WithMethodName(); try { string json = await message.Content.ReadAsStringAsync(); if (typeof(T) == typeof(string)) { return((T)(object)json); } return(JsonConvert.DeserializeObject <T>(json)); } catch (Exception ex) { NetEventSource.Log.Error(context, nameof(DeserializeObjectAsync), ex); throw; } }
public static async Task <DebugDataResponse <T> > DeserializeObjectToDebugAsync <T>(this HttpResponseMessage message, IWorkContext context) { Verify.IsNotNull(nameof(context), context); context = context.WithMethodName(); try { string json = await message.Content.ReadAsStringAsync(); if (typeof(T) == typeof(string)) { return(new DebugDataResponse <T> { Value = (T)(object)json }); } JToken content = JObject.Parse(json); JToken debug = content["debug"]; if (debug == null) { return(new DebugDataResponse <T> { Value = JsonConvert.DeserializeObject <T>(json) }); } return(new DebugDataResponse <T> { Value = JsonConvert.DeserializeObject <T>(json), DebugEvents = debug.ToObject <DebugEventContractV1>(), }); } catch (Exception ex) { NetEventSource.Log.Error(context, nameof(DeserializeObjectToDebugAsync), ex); throw; } }
public static async Task <RestResponse <T> > GetResponseAsync <T>(this HttpResponseMessage message, IWorkContext context) where T : class { context.Verify(nameof(context)).IsNotNull(); context = context.WithMethodName(); try { string json = await message.Content.ReadAsStringAsync(); if (typeof(T) == typeof(string)) { return(new RestResponse <T>(message, (T)(object)json)); } T returnType = JsonConvert.DeserializeObject <T>(json); return(new RestResponse <T>(message, returnType)); } catch (Exception ex) { context.Telemetry.Error(context, $"{nameof(GetResponseAsync)} error '{ex.Message}", ex); throw; } }