コード例 #1
0
        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));
        }
コード例 #2
0
        public void MakeRequestUriTest()
        {
            var request = new HttpApiRequestMessage();
            var uri     = request.MakeRequestUri(new Uri("a", UriKind.RelativeOrAbsolute));

            Assert.Equal("a", uri.OriginalString);

            request = new HttpApiRequestMessage();
            uri     = request.MakeRequestUri(new Uri("http://a.com", UriKind.RelativeOrAbsolute));
            Assert.Equal("http://a.com", uri.OriginalString);

            request = new HttpApiRequestMessage();
            uri     = request.MakeRequestUri(new Uri("http://a.com/a", UriKind.RelativeOrAbsolute));
            Assert.Equal("http://a.com/a", uri.OriginalString);
        }
コード例 #3
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);
        }