pull/1/head
Ogoun 5 years ago
parent 1acc3dd90c
commit 64d594cc0c

@ -2,9 +2,11 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using ZeroLevel.Services.Config; using ZeroLevel.Services.Config;
using ZeroLevel.Services.Config.Implementation; using ZeroLevel.Services.Config.Implementation;
using ZeroLevel.Services.Reflection;
using ZeroLevel.Services.Serialization; using ZeroLevel.Services.Serialization;
namespace ZeroLevel namespace ZeroLevel
@ -14,14 +16,13 @@ namespace ZeroLevel
/// <summary> /// <summary>
/// Application folder path /// Application folder path
/// </summary> /// </summary>
public static string BaseDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); public static readonly string BaseDirectory;
public static string AppLocation = Assembly.GetEntryAssembly()?.Location; public static readonly string AppLocation;
public const string DEFAULT_SECTION_NAME = "_defaultsection"; public const string DEFAULT_SECTION_NAME = "_defaultsection";
#region Ctor #region Ctor
static Configuration() static Configuration()
{ {
_empty = new BaseConfiguration(); _empty = new BaseConfiguration();
@ -29,6 +30,12 @@ namespace ZeroLevel
_empty.Freeze(true); _empty.Freeze(true);
_emptySet.FreezeConfiguration(true); _emptySet.FreezeConfiguration(true);
DefaultSet = Configuration.CreateSet(); DefaultSet = Configuration.CreateSet();
var assembly = EntryAssemblyAttribute.GetEntryAssembly();
if (assembly != null)
{
BaseDirectory = Path.GetDirectoryName(assembly.Location);
AppLocation = assembly.Location;
}
} }
#endregion Ctor #endregion Ctor

@ -6,7 +6,7 @@ namespace ZeroLevel.Network
{ {
static BaseSocket() static BaseSocket()
{ {
MAX_FRAME_PAYLOAD_SIZE = Configuration.Default.FirstOrDefault<int>("MAX_FRAME_PAYLOAD_SIZE", DEFAULT_MAX_FRAME_PAYLOAD_SIZE); MAX_FRAME_PAYLOAD_SIZE = Configuration.Default?.FirstOrDefault<int>("MAX_FRAME_PAYLOAD_SIZE", DEFAULT_MAX_FRAME_PAYLOAD_SIZE) ?? DEFAULT_MAX_FRAME_PAYLOAD_SIZE;
} }
public static readonly IRouter NullRouter = new NullRouter(); public static readonly IRouter NullRouter = new NullRouter();

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace ZeroLevel.Services.Reflection
{
/// <summary>
/// For certain types of apps, such as web apps, <see cref="Assembly.GetEntryAssembly"/>
/// returns null. With the <see cref="EntryAssemblyAttribute"/>, we can designate
/// an assembly as the entry assembly by creating an instance of this attribute,
/// typically in the AssemblyInfo.cs file.
/// <example>
/// [assembly: EntryAssembly]
/// </example>
/// </summary>
[AttributeUsage(AttributeTargets.Assembly)]
public sealed class EntryAssemblyAttribute : Attribute
{
/// <summary>
/// Lazily find the entry assembly.
/// </summary>
private static readonly Lazy<Assembly> EntryAssemblyLazy = new Lazy<Assembly>(GetEntryAssemblyLazily);
/// <summary>
/// Gets the entry assembly.
/// </summary>
/// <returns>The entry assembly.</returns>
public static Assembly GetEntryAssembly()
{
return EntryAssemblyLazy.Value;
}
/// <summary>
/// Invoked lazily to find the entry assembly. We want to cache this value as it may
/// be expensive to find.
/// </summary>
/// <returns>The entry assembly.</returns>
private static Assembly GetEntryAssemblyLazily()
{
return Assembly.GetEntryAssembly() ?? FindEntryAssemblyInCurrentAppDomain();
}
/// <summary>
/// Finds the entry assembly in the current app domain.
/// </summary>
/// <returns>The entry assembly.</returns>
private static Assembly FindEntryAssemblyInCurrentAppDomain()
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var entryAssemblies = new List<Assembly>();
foreach (var assembly in assemblies)
{
// Note the usage of LINQ SingleOrDefault. The EntryAssemblyAttribute's AttrinuteUsage
// only allows it to occur once per assembly; declaring it more than once results in
// a compiler error.
var attribute =
assembly.GetCustomAttributes().OfType<EntryAssemblyAttribute>().SingleOrDefault();
if (attribute != null)
{
entryAssemblies.Add(assembly);
}
}
// Note that we use LINQ Single to ensure we found one and only one assembly with the
// EntryAssemblyAttribute. The EntryAssemblyAttribute should only be put on one assembly
// per application.
return entryAssemblies.SingleOrDefault();
}
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.