Http模块

匿名 (未验证) 提交于 2019-12-02 23:52:01

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;             }         }

   [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

automagicallyconfigures your application services as MVC API Controllers by convention.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!