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);
        }
Example #2
0
        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);
        }
Example #3
0
        private void AddHeaders(DaprHttpClientHandler handler)
        {
            //TenantId
            if (CurrentTenant.Id.HasValue)
            {
                //TODO: Use AbpAspNetCoreMultiTenancyOptions to get the key
                handler.AddHeader(TenantResolverConsts.DefaultTenantKey, CurrentTenant.Id.Value.ToString());
            }
            //Culture
            //TODO: Is that the way we want? Couldn't send the culture (not ui culture)
            var currentCulture = CultureInfo.CurrentUICulture.Name ?? CultureInfo.CurrentCulture.Name;

            if (!currentCulture.IsNullOrEmpty())
            {
                handler.AcceptLanguage(currentCulture);
            }
        }