public override async Task OnRequestAsync(ApiRequestContext context)
        {
            await Task.CompletedTask;
            IServiceProvider sp           = context.HttpContext.ServiceProvider;
            HostProvider     hostProvider = sp.GetRequiredService <HostProvider>();
            //服务名也可以在接口配置时挂在Properties中
            string host = hostProvider.ResolveService(this.Name);
            HttpApiRequestMessage requestMessage = context.HttpContext.RequestMessage;

            //和原有的Uri组合并覆盖原有Uri
            //并非一定要这样实现,只要覆盖了RequestUri,即完成了替换
            requestMessage.RequestUri = requestMessage.MakeRequestUri(new Uri(host));
        }
Ejemplo n.º 2
0
        public override Task OnRequestAsync(ApiRequestContext context)
        {
            var options = context.HttpContext.HttpApiOptions;

            //获取注册时为服务配置的服务名
            options.Properties.TryGetValue("serviceName", out object serviceNameObj);
            string                serviceName    = serviceNameObj as string;
            IServiceProvider      sp             = context.HttpContext.ServiceProvider;
            HostProvider          hostProvider   = sp.GetRequiredService <HostProvider>();
            string                host           = hostProvider.ResolveService(serviceName);
            HttpApiRequestMessage requestMessage = context.HttpContext.RequestMessage;

            //和原有的Uri组合并覆盖原有Uri
            //并非一定要这样实现,只要覆盖了RequestUri,即完成了替换
            requestMessage.RequestUri = requestMessage.MakeRequestUri(new Uri(host));
            return(Task.CompletedTask);
        }