mirror of https://github.com/abpframework/abp.git
Browse Source
The `CrossPlatformParameterTypeComparer` handles type differences in .net-core and .net-framework by replacing the assembly names with placeholders. This implementation works in all environments because the `CrossPlatformParameterTypeComparer` inherits the default behavior and (once) checks the current framework.pull/2867/head
5 changed files with 129 additions and 3 deletions
@ -0,0 +1,43 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Reflection; |
||||
|
using System.Text; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Http.Modeling; |
||||
|
|
||||
|
namespace Volo.Abp.Http.Client.DynamicProxying |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Compares the server-side parameter-type with the local parameter-type considering differences between
|
||||
|
/// the .net-core and the traditional .net-framework.
|
||||
|
/// Inspired by 'https://github.com/akkadotnet/akka.net/pull/2947/files'
|
||||
|
/// </summary>
|
||||
|
public class CrossPlatformParameterTypeComparer : ParameterTypeComparer, ITransientDependency |
||||
|
{ |
||||
|
const string Placeholder = "%COREFX%"; |
||||
|
const string NetCoreLib = "System.Private.CoreLib"; |
||||
|
const string NetFxLib = "mscorlib"; |
||||
|
|
||||
|
static readonly bool runsOnNetFx; |
||||
|
|
||||
|
static CrossPlatformParameterTypeComparer() |
||||
|
{ |
||||
|
runsOnNetFx = string.IsNullOrEmpty(EnvironmentHelper.RuntimeNetCoreVersion); |
||||
|
} |
||||
|
|
||||
|
public override bool TypeMatches(MethodParameterApiDescriptionModel actionParameter, ParameterInfo methodParameter) |
||||
|
{ |
||||
|
if (runsOnNetFx) |
||||
|
{ |
||||
|
// Compare the server's DotnetCore type relative to the current, traditional framework type
|
||||
|
// by replacing framework assemblies with an placeholder string.
|
||||
|
return actionParameter.TypeAsString.Replace(NetCoreLib, Placeholder) |
||||
|
== methodParameter.ParameterType.GetFullNameWithAssemblyName().Replace(NetFxLib, Placeholder); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
return base.TypeMatches(actionParameter, methodParameter); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Reflection; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Volo.Abp.Http.Client.DynamicProxying |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Provide helpers to check the environment where the current ABP app is currently running
|
||||
|
/// </summary>
|
||||
|
static class EnvironmentHelper |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes the <see cref="EnvironmentHelper"/> class.
|
||||
|
/// </summary>
|
||||
|
static EnvironmentHelper() |
||||
|
{ |
||||
|
RuntimeNetCoreVersion = GetNetCoreVersion(); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the runtime net core version.
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// If the <see cref="RuntimeNetCoreVersion"/> is null the app is running on a .net Classic environment
|
||||
|
/// </remarks>
|
||||
|
public static string RuntimeNetCoreVersion { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the net core version.
|
||||
|
/// </summary>
|
||||
|
static string GetNetCoreVersion() |
||||
|
{ |
||||
|
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly; |
||||
|
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); |
||||
|
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App"); |
||||
|
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2) |
||||
|
{ |
||||
|
return assemblyPath[netCoreAppIndex + 1]; |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Reflection; |
||||
|
using System.Text; |
||||
|
using Volo.Abp.Http.Modeling; |
||||
|
|
||||
|
namespace Volo.Abp.Http.Client.DynamicProxying |
||||
|
{ |
||||
|
public interface IParameterTypeComparer |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Compares the given parameters.
|
||||
|
/// </summary>
|
||||
|
/// <param name="actionParameter">The parameter description retreived from the server (depends on the server's runtime)</param>
|
||||
|
/// <param name="methodParameter">The local parameter info (depends on the client's runtime)</param>
|
||||
|
/// <returns></returns>
|
||||
|
public bool TypeMatches(MethodParameterApiDescriptionModel actionParameter, ParameterInfo methodParameter); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Reflection; |
||||
|
using System.Text; |
||||
|
using Volo.Abp.Http.Modeling; |
||||
|
|
||||
|
namespace Volo.Abp.Http.Client.DynamicProxying |
||||
|
{ |
||||
|
public class ParameterTypeComparer : IParameterTypeComparer |
||||
|
{ |
||||
|
public virtual bool TypeMatches(MethodParameterApiDescriptionModel actionParameter, ParameterInfo methodParameter) |
||||
|
{ |
||||
|
return actionParameter.TypeAsString == methodParameter.ParameterType.GetFullNameWithAssemblyName(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue