private async Task MakeRequestAsync(IAbpMethodInvocation invocation) { // 获取Actor配置 var actorProxyConfig = DaprActorProxyOptions.ActorProxies.GetOrDefault(typeof(TService)) ?? throw new AbpException($"Could not get DynamicDaprActorProxyConfig for {typeof(TService).FullName}."); var remoteServiceConfig = DaprActorOptions.RemoteActors.GetConfigurationOrDefault(actorProxyConfig.RemoteServiceName); var actorProxyOptions = new ActorProxyOptions { HttpEndpoint = remoteServiceConfig.BaseUrl }; // 自定义请求处理器 // 添加请求头用于传递状态 // TODO: Actor一次只能处理一个请求,使用状态管理来传递状态的可行性? var httpClientHandler = new DaprHttpClientHandler(); // 身份认证处理 await ActoryProxyAuthenticator.AuthenticateAsync( new DaprActorProxyAuthenticateContext( httpClientHandler, remoteServiceConfig, actorProxyConfig.RemoteServiceName)); AddHeaders(httpClientHandler); // 代理工厂 var proxyFactory = new ActorProxyFactory(actorProxyOptions, (HttpMessageHandler)httpClientHandler); await MakeRequestAsync(invocation, proxyFactory, remoteServiceConfig); }
private async Task MakeRequestAsync(IAbpMethodInvocation invocation) { // 获取Actor配置 var actorProxyConfig = DaprActorProxyOptions.ActorProxies.GetOrDefault(typeof(TService)) ?? throw new AbpException($"Could not get DynamicDaprActorProxyConfig for {typeof(TService).FullName}."); var remoteServiceConfig = DaprServiceOptions.RemoteServices.GetConfigurationOrDefault(actorProxyConfig.RemoteServiceName); // Actors的定义太多, 可以考虑使用默认的 BaseUrl 作为远程地址 if (remoteServiceConfig.BaseUrl.IsNullOrWhiteSpace()) { throw new AbpException($"Could not get BaseUrl for {actorProxyConfig.RemoteServiceName} Or Default."); } var actorProxyOptions = new ActorProxyOptions { HttpEndpoint = remoteServiceConfig.BaseUrl }; // 自定义请求处理器 // 添加请求头用于传递状态 // TODO: Actor一次只能处理一个请求,使用状态管理来传递状态的可行性? var httpClientHandler = new DaprHttpClientHandler(); AddHeaders(httpClientHandler); httpClientHandler.PreConfigure(async(requestMessage) => { // 占位 var httpClient = HttpClientFactory.Create(AbpDaprActorsModule.DaprHttpClient); await ClientAuthenticator.Authenticate( new RemoteServiceHttpClientAuthenticateContext( httpClient, requestMessage, remoteServiceConfig, actorProxyConfig.RemoteServiceName)); // 标头 if (requestMessage.Headers.Authorization == null && httpClient.DefaultRequestHeaders.Authorization != null) { requestMessage.Headers.Authorization = httpClient.DefaultRequestHeaders.Authorization; } }); // 代理工厂 var proxyFactory = new ActorProxyFactory(actorProxyOptions, (HttpMessageHandler)httpClientHandler); await MakeRequestAsync(invocation, proxyFactory); }
public void CreateProxyWithNoRemoting_WithApiToken() { var actorId = new ActorId("abc"); var handler = new TestHttpClientHandler(); var options = new ActorProxyOptions { DaprApiToken = "test_token", }; var factory = new ActorProxyFactory(options, handler); var proxy = factory.Create(actorId, "TestActor"); var task = proxy.InvokeMethodAsync("SetCountAsync", 1, new CancellationToken()); handler.Requests.TryDequeue(out var entry).Should().BeTrue(); var headerValues = entry.Request.Headers.GetValues("dapr-api-token"); headerValues.Should().Contain("test_token"); }
public TActorInterface CreateActorProxy <TActorInterface>(ActorId actorId, string actorType, ActorProxyOptions options = null) where TActorInterface : IActor { if (typeof(TActorInterface) != typeof(IInventoryActor)) { throw new ApplicationException(); } if (Actor is not null) { return((TActorInterface)Actor); } Actor = _serviceProvider.GetRequiredService <IInventoryActor>(); return((TActorInterface)Actor); }
public async Task CreateProxyWithNoRemoting_WithApiToken() { await using var client = TestClient.CreateForMessageHandler(); var actorId = new ActorId("abc"); var options = new ActorProxyOptions { DaprApiToken = "test_token", }; var request = await client.CaptureHttpRequestAsync(async handler => { var factory = new ActorProxyFactory(options, handler); var proxy = factory.Create(actorId, "TestActor"); await proxy.InvokeMethodAsync("SetCountAsync", 1, new CancellationToken()); }); request.Dismiss(); var headerValues = request.Request.Headers.GetValues("dapr-api-token"); headerValues.Should().Contain("test_token"); }
public ActorProxy Create(ActorId actorId, string actorType, ActorProxyOptions options = null) { throw new NotSupportedException(); }
public object CreateActorProxy(ActorId actorId, Type actorInterfaceType, string actorType, ActorProxyOptions options = null) { throw new NotSupportedException(); }
public object CreateActorProxy(ActorId actorId, Type actorInterfaceType, string actorType, ActorProxyOptions options = null) { throw new NotImplementedException(Message); }
public TActorInterface CreateActorProxy <TActorInterface>(ActorId actorId, string actorType, ActorProxyOptions options = null) where TActorInterface : IActor { throw new NotImplementedException(Message); }
public ActorProxy Create(ActorId actorId, string actorType, ActorProxyOptions options = null) { throw new NotImplementedException(Message); }