Skip to content

Commit

Permalink
Merge pull request #9905 from abpframework/liangshiwei/services-proxy
Browse files Browse the repository at this point in the history
Add C# and JavaScript Static Client Proxy Generation
  • Loading branch information
hikalkan committed Sep 15, 2021
2 parents bb8ba8c + dab6833 commit b65122a
Show file tree
Hide file tree
Showing 223 changed files with 13,154 additions and 502 deletions.
5 changes: 5 additions & 0 deletions common.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@
</None>
</ItemGroup>

<ItemGroup Condition="$(AssemblyName.EndsWith('HttpApi.Client'))">
<EmbeddedResource Include="**\*generate-proxy.json" />
<Content Remove="**\*generate-proxy.json" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public ApplicationApiDescriptionModel CreateApiModel(ApplicationApiDescriptionMo

var controllerModel = moduleModel.GetOrAddController(
_options.ControllerNameGenerator(controllerType, setting),
FindGroupName(controllerType) ?? apiDescription.GroupName,
controllerType,
_modelOptions.IgnoredInterfaces
);
Expand Down Expand Up @@ -113,6 +114,14 @@ public ApplicationApiDescriptionModel CreateApiModel(ApplicationApiDescriptionMo
allowAnonymous = false;
}

var implementFrom = controllerType.FullName;

var interfaceType = controllerType.GetInterfaces().FirstOrDefault(i => i.GetMethods().Any(x => x.ToString() == method.ToString()));
if (interfaceType != null)
{
implementFrom = TypeHelper.GetFullNameHandlingNullableAndGenerics(interfaceType);
}

var actionModel = controllerModel.AddAction(
uniqueMethodName,
ActionApiDescriptionModel.Create(
Expand All @@ -121,7 +130,8 @@ public ApplicationApiDescriptionModel CreateApiModel(ApplicationApiDescriptionMo
apiDescription.RelativePath,
apiDescription.HttpMethod,
GetSupportedVersions(controllerType, method, setting),
allowAnonymous
allowAnonymous,
implementFrom
)
);

Expand Down Expand Up @@ -351,6 +361,19 @@ private string GetRemoteServiceName(Type controllerType, [CanBeNull] Conventiona
return ModuleApiDescriptionModel.DefaultRemoteServiceName;
}

private string FindGroupName(Type controllerType)
{
var controllerNameAttribute =
controllerType.GetCustomAttributes().OfType<ControllerNameAttribute>().FirstOrDefault();

if (controllerNameAttribute?.Name != null)
{
return controllerNameAttribute.Name;
}

return null;
}

[CanBeNull]
private ConventionalControllerSetting FindSetting(Type controllerType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected virtual void ApplyForControllers(ApplicationModel application)
{
RemoveDuplicateControllers(application);

foreach (var controller in application.Controllers)
foreach (var controller in GetControllers(application))
{
var controllerType = controller.ControllerType.AsType();

Expand All @@ -71,11 +71,16 @@ protected virtual void ApplyForControllers(ApplicationModel application)
}
}

protected virtual IList<ControllerModel> GetControllers(ApplicationModel application)
{
return application.Controllers;
}

protected virtual void RemoveDuplicateControllers(ApplicationModel application)
{
var controllerModelsToRemove = new List<ControllerModel>();

foreach (var controllerModel in application.Controllers)
foreach (var controllerModel in GetControllers(application))
{
if (!controllerModel.ControllerType.IsDefined(typeof(ExposeServicesAttribute), false))
{
Expand All @@ -90,7 +95,7 @@ protected virtual void RemoveDuplicateControllers(ApplicationModel application)
var exposeServicesAttr = ReflectionHelper.GetSingleAttributeOrDefault<ExposeServicesAttribute>(controllerModel.ControllerType);
if (exposeServicesAttr.IncludeSelf)
{
var exposedControllerModels = application.Controllers
var exposedControllerModels = GetControllers(application)
.Where(cm => exposeServicesAttr.ServiceTypes.Contains(cm.ControllerType))
.ToArray();

Expand All @@ -109,7 +114,7 @@ protected virtual void RemoveDuplicateControllers(ApplicationModel application)
continue;
}

var baseControllerModels = application.Controllers
var baseControllerModels = GetControllers(application)
.Where(cm => baseControllerTypes.Contains(cm.ControllerType))
.ToArray();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System.Net.Http;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http.Client;
using Volo.Abp.Http.Client.DynamicProxying;
using Volo.Abp.Http.Client.Proxying;

namespace Volo.Abp.AspNetCore.TestBase.DynamicProxying
{
[Dependency(ReplaceServices = true)]
public class AspNetCoreTestDynamicProxyHttpClientFactory : IDynamicProxyHttpClientFactory, ITransientDependency
public class AspNetCoreTestProxyHttpClientFactory : IProxyHttpClientFactory, ITransientDependency
{
private readonly ITestServerAccessor _testServerAccessor;

public AspNetCoreTestDynamicProxyHttpClientFactory(
public AspNetCoreTestProxyHttpClientFactory(
ITestServerAccessor testServerAccessor)
{
_testServerAccessor = testServerAccessor;
Expand Down
15 changes: 14 additions & 1 deletion framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
using Volo.Abp.Cli.Commands;
using Volo.Abp.Cli.Http;
using Volo.Abp.Cli.LIbs;
using Volo.Abp.Cli.ServiceProxying;
using Volo.Abp.Cli.ServiceProxying.Angular;
using Volo.Abp.Cli.ServiceProxying.CSharp;
using Volo.Abp.Cli.ServiceProxying.JavaScript;
using Volo.Abp.Domain;
using Volo.Abp.Http;
using Volo.Abp.IdentityModel;
using Volo.Abp.Json;
using Volo.Abp.Json.SystemTextJson;
Expand All @@ -16,7 +21,8 @@ namespace Volo.Abp.Cli
typeof(AbpDddDomainModule),
typeof(AbpJsonModule),
typeof(AbpIdentityModelModule),
typeof(AbpMinifyModule)
typeof(AbpMinifyModule),
typeof(AbpHttpModule)
)]
public class AbpCliCoreModule : AbpModule
{
Expand Down Expand Up @@ -58,6 +64,13 @@ public override void ConfigureServices(ServiceConfigurationContext context)
options.Commands["create-migration-and-run-migrator"] = typeof(CreateMigrationAndRunMigratorCommand);
options.Commands["install-libs"] = typeof(InstallLibsCommand);
});

Configure<AbpCliServiceProxyOptions>(options =>
{
options.Generators[JavaScriptServiceProxyGenerator.Name] = typeof(JavaScriptServiceProxyGenerator);
options.Generators[AngularServiceProxyGenerator.Name] = typeof(AngularServiceProxyGenerator);
options.Generators[CSharpServiceProxyGenerator.Name] = typeof(CSharpServiceProxyGenerator);
});
}
}
}
10 changes: 9 additions & 1 deletion framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Volo.Abp.Cli
using System;

namespace Volo.Abp.Cli
{
public static class CliUrls
{
Expand Down Expand Up @@ -33,5 +35,11 @@ public static string GetNuGetPackageInfoUrl(string apiKey, string packageId)
{
return $"{NuGetRootPath}{apiKey}/v3/package/{packageId}/index.json";
}

public static string GetApiDefinitionUrl(string url)
{
url = url.EnsureEndsWith('/');
return $"{url}api/abp/api-definition";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
using System.Text;
using Microsoft.Extensions.Options;
using Volo.Abp.Cli.ServiceProxying;
using Volo.Abp.DependencyInjection;

namespace Volo.Abp.Cli.Commands
{
public class GenerateProxyCommand : ProxyCommandBase
public class GenerateProxyCommand : ProxyCommandBase<GenerateProxyCommand>
{
public const string Name = "generate-proxy";

protected override string CommandName => Name;

protected override string SchematicsCommandName => "proxy-add";
public GenerateProxyCommand(
IOptions<AbpCliServiceProxyOptions> serviceProxyOptions,
IHybridServiceScopeFactory serviceScopeFactory)
: base(serviceProxyOptions, serviceScopeFactory)
{
}

public GenerateProxyCommand(CliService cliService)
: base(cliService)
public override string GetUsageInfo()
{
var sb = new StringBuilder(base.GetUsageInfo());

sb.AppendLine("");
sb.AppendLine("Examples:");
sb.AppendLine("");
sb.AppendLine(" abp generate-proxy -t ng");
sb.AppendLine(" abp generate-proxy -t js -m identity -o Pages/Identity/client-proxies.js -url https://localhost:44302/");
sb.AppendLine(" abp generate-proxy -t csharp --folder MyProxies/InnerFolder -url https://localhost:44302/");

return sb.ToString();
}

public override string GetShortDescription()
{
return "Generates Angular service proxies and DTOs to consume HTTP APIs.";
return "Generates client service proxies and DTOs to consume HTTP APIs.";
}
}
}

0 comments on commit b65122a

Please sign in to comment.