Skip to content

Commit

Permalink
Move DomainServiceEndpointRoutePatternAttribute to Hosting (#515)
Browse files Browse the repository at this point in the history
* Fix locating linkedServerAssembly
* Move DomainServiceEndpointRoutePatternAttribute to Hosting
  • Loading branch information
Daniel-Svensson committed Jun 13, 2024
1 parent 705d5ae commit 94081df
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;

namespace OpenRiaServices.Server
namespace OpenRiaServices.Hosting.AspNetCore
{
#if NET
/// <summary>
/// Determine how endpoints routes (Uris to access DomainServices) are generated
/// </summary>
Expand All @@ -28,15 +27,15 @@ public enum EndpointRoutePattern
}

/// <summary>
/// Attribute to configure the pattern used for endpoint, see <see cref="Server.EndpointRoutePattern"/>
/// Attribute to configure the pattern used for endpoint, see <see cref="AspNetCore.EndpointRoutePattern"/>
/// </summary>
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false, Inherited = true)]
public sealed class DomainServiceEndpointRoutePatternAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="DomainServiceEndpointRoutePatternAttribute"/> class.
/// </summary>
/// <param name="endpointRoutePattern"><see cref="Server.EndpointRoutePattern"/> to use</param>
/// <param name="endpointRoutePattern"><see cref="AspNetCore.EndpointRoutePattern"/> to use</param>
public DomainServiceEndpointRoutePatternAttribute(EndpointRoutePattern endpointRoutePattern)
=> EndpointRoutePattern = endpointRoutePattern;

Expand All @@ -45,5 +44,4 @@ public DomainServiceEndpointRoutePatternAttribute(EndpointRoutePattern endpointR
/// </summary>
public EndpointRoutePattern EndpointRoutePattern { get; }
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,11 @@ internal static string GetEndOperationReturnType(DomainOperationEntry operation)
private string GetDomainServiceUri()
{
Type type = this.DomainServiceDescription.DomainServiceType;
#if NET

// Lookup DomainServiceEndpointRoutePatternAttribute first in same assembly as DomainService
// Then in the entry point assembly
// - Fallback
EndpointRoutePattern routePattern = type.Assembly.GetCustomAttribute<DomainServiceEndpointRoutePatternAttribute>()?.EndpointRoutePattern is { } endpointRoutePattern
? (EndpointRoutePattern)(int)(endpointRoutePattern)
: (EndpointRoutePattern)(int)this.ClientCodeGenerator.Options.DefaultEndpointRoutePattern;
// Otherwise fallback to default
EndpointRoutePattern routePattern = SharedCodeGenUtilities.TryGetRoutePatternFromAssembly(type.Assembly)
?? this.ClientCodeGenerator.Options.DefaultEndpointRoutePattern;

return routePattern switch
{
Expand All @@ -165,9 +163,6 @@ private string GetDomainServiceUri()
EndpointRoutePattern.FullName => type.FullName.Replace('.', '-'),
_ => throw new NotImplementedException(),
};
#else
return type.FullName.Replace('.', '-') + ".svc";
#endif
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<NoWarn>$(NoWarn);CS0618</NoWarn>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\OpenRiaServices.Tools\Framework\SharedCodeGenUtilities.cs" Link="Utilities\SharedCodeGenUtilities.cs" />
<Compile Include="..\..\OpenRiaServices.Tools\Framework\MetadataPipeline\AttributeBuilderException.cs" Link="CSharpGenerators\AttributeGenerationHelpers\AttributeBuilderException.cs" />
<Compile Include="..\..\OpenRiaServices.Tools\Framework\MetadataPipeline\AttributeDeclaration.cs" Link="CSharpGenerators\AttributeGenerationHelpers\AttributeDeclaration.cs" />
<Compile Include="..\..\OpenRiaServices.Tools\Framework\MetadataPipeline\CustomValidationCustomAttributeBuilder.cs" Link="CSharpGenerators\AttributeGenerationHelpers\CustomValidationCustomAttributeBuilder.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,12 @@ internal string GenerateCode(ClientCodeGenerationOptions options, SharedCodeServ
{
CodeGenerationHost host = new CodeGenerationHost(loggingService, sharedCodeService);

#if NET
// If the server assembly has a DomainServiceEndpointRoutePatternAttribute, we use it to set the default EndpointRoutePattern
Assembly linkedServerAssembly = AssemblyUtilities.LoadAssembly(parameters.ServerAssemblies.First() + ".dll", loggingService);
if (linkedServerAssembly?.GetCustomAttribute<DomainServiceEndpointRoutePatternAttribute>() is { } routeAttribute)
Assembly linkedServerAssembly = AssemblyUtilities.LoadAssembly(parameters.ServerAssemblies.First(), loggingService);
if (SharedCodeGenUtilities.TryGetRoutePatternFromAssembly(linkedServerAssembly) is { } pattern)
{
options.DefaultEndpointRoutePattern = (EndpointRoutePattern)(int)routeAttribute.EndpointRoutePattern;
options.DefaultEndpointRoutePattern = pattern;
}
#endif

return this.GenerateCode(host, options, parameters.ServerAssemblies, codeGeneratorName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,11 @@ private CodeTypeDeclaration GenEntityContainerInnerClass(CodeTypeDeclaration pro
private string GetDomainServiceUri()
{
Type type = _domainServiceDescription.DomainServiceType;
#if NET

// Lookup DomainServiceEndpointRoutePatternAttribute first in same assembly as DomainService
// Then in the entry point assembly
// - Fallback
EndpointRoutePattern routePattern = type.Assembly.GetCustomAttribute<DomainServiceEndpointRoutePatternAttribute>()?.EndpointRoutePattern is { } endpointRoutePattern
? (EndpointRoutePattern)(int)(endpointRoutePattern)
: base.ClientProxyGenerator.ClientProxyCodeGenerationOptions.DefaultEndpointRoutePattern;
// Otherwise fallback to default
EndpointRoutePattern routePattern = SharedCodeGenUtilities.TryGetRoutePatternFromAssembly(type.Assembly)
?? base.ClientProxyGenerator.ClientProxyCodeGenerationOptions.DefaultEndpointRoutePattern;

return routePattern switch
{
Expand All @@ -369,9 +367,6 @@ private string GetDomainServiceUri()
EndpointRoutePattern.FullName => type.FullName.Replace('.', '-'),
_ => throw new NotImplementedException(),
};
#else
return type.FullName.Replace('.', '-') + ".svc";
#endif
}

private void GenerateConstructors(CodeTypeDeclaration proxyClass, CodeTypeDeclaration contractInterface, EnableClientAccessAttribute enableClientAccessAttribute, CodeMethodInvokeExpression onCreatedExpression)
Expand Down
27 changes: 27 additions & 0 deletions src/OpenRiaServices.Tools/Framework/SharedCodeGenUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;

namespace OpenRiaServices.Tools
{
/// <summary>
/// A collection of code generation utilities.
/// </summary>
internal static partial class SharedCodeGenUtilities
{
public static EndpointRoutePattern? TryGetRoutePatternFromAssembly(Assembly assembly)
{
#if NET
if(assembly?.CustomAttributes.FirstOrDefault(cad => cad.AttributeType.FullName == "OpenRiaServices.Hosting.AspNetCore.DomainServiceEndpointRoutePatternAttribute")
is { ConstructorArguments: [var constructorArgument] } )
{
return (EndpointRoutePattern)Convert.ToInt32(constructorArgument.Value, CultureInfo.InvariantCulture);
}
return null;
#else
return EndpointRoutePattern.WCF;
#endif
}
}
}

0 comments on commit 94081df

Please sign in to comment.