Http模块

1、Volo.Abp.Http.Abstractions

配置

public class ApiDescriptionModelOptions
    {
        public HashSet<Type> IgnoredInterfaces { get; }

        public ApiDescriptionModelOptions()
        {
            IgnoredInterfaces = new HashSet<Type>
            {
                typeof(ITransientDependency),
                typeof(ISingletonDependency),
                typeof(IDisposable),
                typeof(IAvoidDuplicateCrossCuttingConcerns)
            };
        }
    }

2、Volo.Abp.Http

 [DependsOn(typeof(AbpHttpAbstractionsModule))]
    [DependsOn(typeof(AbpJsonModule))]
    public class AbpHttpModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpApiProxyScriptingOptions>(options =>
            {
                options.Generators[JQueryProxyScriptGenerator.Name] = typeof(JQueryProxyScriptGenerator);
            });
        }
    }

 

  public interface IApiDescriptionModelProvider
    {
        ApplicationApiDescriptionModel CreateApiModel();
    }

 

ApplicationApiDescriptionModel,存储ModuleApiDescriptionModel字典

ModuleApiDescriptionModel,存储ControllerApiDescriptionModel字典

ControllerApiDescriptionModel,存储ActionApiDescriptionModel字典

ActionApiDescriptionModel,名字,方法名,url,支持版本,IList<MethodParameterApiDescriptionModel>,IList<ParameterApiDescriptionModel>,

ReturnValueApiDescriptionModel

查看下面方法

  private async Task<ApplicationApiDescriptionModel> GetFromServerAsync(string baseUrl)
        {
            using (var client = _httpClientFactory.Create())
            {
                var response = await client.GetAsync(baseUrl + "api/abp/api-definition");
                if (!response.IsSuccessStatusCode)
                {
                    throw new AbpException("Remote service returns error!");
                }

                var content = await response.Content.ReadAsStringAsync();

                var result = JsonConvert.DeserializeObject(
                    content,
                    typeof(ApplicationApiDescriptionModel),
                    new JsonSerializerSettings
                    {
                        ContractResolver = new CamelCasePropertyNamesContractResolver()
                    });

                return (ApplicationApiDescriptionModel)result;
            }
        }

 

3、 Volo.Abp.Http.Client

   [DependsOn(
        typeof(AbpHttpModule),
        typeof(AbpCastleCoreModule),
        typeof(AbpThreadingModule),
        typeof(AbpMultiTenancyModule)
        )]
    public class AbpHttpClientModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();
            Configure<RemoteServiceOptions>(configuration);
        }
    }

授权

   public interface IRemoteServiceHttpClientAuthenticator
    {
        Task Authenticate(RemoteServiceHttpClientAuthenticateContext context);
    }

RemoteServiceHttpClientAuthenticateContext

 public RemoteServiceHttpClientAuthenticateContext(
            HttpClient client, 
            HttpRequestMessage request,
            RemoteServiceConfiguration remoteService,
            string remoteServiceName)
        {
            Client = client;
            Request = request;
            RemoteService = remoteService;
            RemoteServiceName = remoteServiceName;
        }

拦截器

 private async Task<string> MakeRequestAsync(IAbpMethodInvocation invocation)
        {
            using (var client = HttpClientFactory.Create())
            {
                var clientConfig = ClientOptions.HttpClientProxies.GetOrDefault(typeof(TService)) ?? throw new AbpException($"Could not get DynamicHttpClientProxyConfig for {typeof(TService).FullName}.");
                var remoteServiceConfig = RemoteServiceOptions.RemoteServices.GetConfigurationOrDefault(clientConfig.RemoteServiceName);

                var action = await ApiDescriptionFinder.FindActionAsync(remoteServiceConfig.BaseUrl, typeof(TService), invocation.Method);
                var apiVersion = GetApiVersionInfo(action);
                var url = remoteServiceConfig.BaseUrl + UrlBuilder.GenerateUrlWithParameters(action, invocation.ArgumentsDictionary, apiVersion);

                var requestMessage = new HttpRequestMessage(action.GetHttpMethod(), url)
                {
                    Content = RequestPayloadBuilder.BuildContent(action, invocation.ArgumentsDictionary, JsonSerializer, apiVersion)
                };

                AddHeaders(invocation, action, requestMessage, apiVersion);

                await ClientAuthenticator.Authenticate(
                    new RemoteServiceHttpClientAuthenticateContext(
                        client,
                        requestMessage,
                        remoteServiceConfig,
                        clientConfig.RemoteServiceName
                    )
                );

                var response = await client.SendAsync(requestMessage, GetCancellationToken());

                if (!response.IsSuccessStatusCode)
                {
                    await ThrowExceptionForResponseAsync(response);
                }

                return await response.Content.ReadAsStringAsync();
            }
        }

 

4、Volo.Abp.Http.Client.IdentityModel

 public IdentityClientConfiguration(
            string authority,
            string scope,
            string clientId, 
            string clientSecret, 
            string grantType = OidcConstants.GrantTypes.ClientCredentials,
            string userName = null,
            string userPassword = null)
        {
            this[nameof(Authority)] = authority;
            this[nameof(Scope)] = scope;
            this[nameof(ClientId)] = clientId;
            this[nameof(ClientSecret)] = clientSecret;
            this[nameof(GrantType)] = grantType;
            this[nameof(UserName)] = userName;
            this[nameof(UserPassword)] = userPassword;
        }

    public class IdentityClientConfigurationDictionary : Dictionary<string, IdentityClientConfiguration>
    {
        public const string DefaultName = "Default";

        public IdentityClientConfiguration Default
        {
            get => this.GetOrDefault(DefaultName);
            set => this[DefaultName] = value;
        }
    }

  public class IdentityClientOptions
    {
        public IdentityClientConfigurationDictionary IdentityClients { get; set; }

        public IdentityClientOptions()
        {
            IdentityClients = new IdentityClientConfigurationDictionary();
        }
    }

IIdentityModelAuthenticationService

 public async Task<bool> TryAuthenticateAsync(
            [NotNull] HttpClient client,
            string identityClientName = null)
        {
            var accessToken = await GetAccessTokenOrNullAsync(identityClientName);
            if (accessToken == null)
            {
                return false;
            }

            SetAccessToken(client, accessToken);
            return true;

        }

 

Auto API Controllers

You normally create Controllers to expose application services as HTTP API endpoints. Thus allowing browser or 3rd-party clients to call them via AJAX. ABP can automagicallyconfigures your application services as MVC API Controllers by convention.

posted on 2019-07-25 10:56  dollymi  阅读(771)  评论(0编辑  收藏  举报

导航