Ejemplo n.º 1
0
 public static string ToJson <T>(this T obj)
 {
     return(JsConfig.PreferInterfaces
         ? JsonSerializer.SerializeToString(obj, AssemblyUtils.MainInterface <T>())
         : JsonSerializer.SerializeToString(obj));
 }
Ejemplo n.º 2
0
        static Env()
        {
            if (PclExport.Instance == null)
            {
                throw new ArgumentException("PclExport.Instance needs to be initialized");
            }

#if NETSTANDARD
            IsNetStandard = true;
            try
            {
                IsLinux    = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
                IsWindows  = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
                IsOSX      = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
                IsNetCore3 = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core 3");

                var fxDesc = RuntimeInformation.FrameworkDescription;
                IsMono    = fxDesc.Contains("Mono");
                IsNetCore = fxDesc.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase);
            }
            catch (Exception) {} //throws PlatformNotSupportedException in AWS lambda
            IsUnix = IsOSX || IsLinux;
            HasMultiplePlatformTargets = true;
            IsUWP = IsRunningAsUwp();
#elif NET45
            IsNetFramework = true;
            switch (Environment.OSVersion.Platform)
            {
            case PlatformID.Win32NT:
            case PlatformID.Win32S:
            case PlatformID.Win32Windows:
            case PlatformID.WinCE:
                IsWindows = true;
                break;
            }

            var platform = (int)Environment.OSVersion.Platform;
            IsUnix = platform == 4 || platform == 6 || platform == 128;

            if (File.Exists(@"/System/Library/CoreServices/SystemVersion.plist"))
            {
                IsOSX = true;
            }
            var osType = File.Exists(@"/proc/sys/kernel/ostype")
                ? File.ReadAllText(@"/proc/sys/kernel/ostype")
                : null;
            IsLinux = osType?.IndexOf("Linux", StringComparison.OrdinalIgnoreCase) >= 0;
            try
            {
                IsMono = AssemblyUtils.FindType("Mono.Runtime") != null;
            }
            catch (Exception) {}

            SupportsDynamic = true;
#endif

#if NETCORE2_1
            IsNetStandard   = false;
            IsNetCore       = true;
            IsNetCore21     = true;
            SupportsDynamic = true;
#endif

#if NETSTANDARD2_0
            IsNetStandard20 = true;
#endif

            if (!IsUWP)
            {
                try
                {
                    IsAndroid = AssemblyUtils.FindType("Android.Manifest") != null;
                    if (IsOSX && IsMono)
                    {
                        var runtimeDir = RuntimeEnvironment.GetRuntimeDirectory();
                        //iOS detection no longer trustworthy so assuming iOS based on some current heuristics. TODO: improve iOS detection
                        IsIOS = runtimeDir.StartsWith("/private/var") ||
                                runtimeDir.Contains("/CoreSimulator/Devices/");
                    }
                }
                catch (Exception) {}
            }

            SupportsExpressions = true;
            SupportsEmit        = !(IsUWP || IsIOS);

            if (!SupportsEmit)
            {
                ReflectionOptimizer.Instance = ExpressionReflectionOptimizer.Provider;
            }

            VersionString = ServiceStackVersion.ToString(CultureInfo.InvariantCulture);

            ServerUserAgent = "ServiceStack/"
                              + VersionString + " "
                              + PclExport.Instance.PlatformName
                              + (IsMono ? "/Mono" : "")
                              + (IsLinux ? "/Linux" : IsOSX ? "/OSX" : IsUnix ? "/Unix" : IsWindows ? "/Windows" : "/UnknownOS")
                              + (IsIOS ? "/iOS" : IsAndroid ? "/Android" : IsUWP ? "/UWP" : "");

            __releaseDate = new DateTime(2001, 01, 01);
        }
Ejemplo n.º 3
0
        static Env()
        {
            if (PclExport.Instance == null)
            {
                throw new ArgumentException("PclExport.Instance needs to be initialized");
            }

            var platformName = PclExport.Instance.PlatformName;

            if (platformName != PclExport.Platforms.Uwp)
            {
                IsMono = AssemblyUtils.FindType("Mono.Runtime") != null;

                IsIOS = AssemblyUtils.FindType("MonoTouch.Foundation.NSObject") != null ||
                        AssemblyUtils.FindType("Foundation.NSObject") != null;

                IsAndroid = AssemblyUtils.FindType("Android.Manifest") != null;

                try
                {
                    IsOSX = AssemblyUtils.FindType("Mono.AppKit") != null;
#if NET45
                    IsWindows = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("windir"));
                    if (File.Exists(@"/System/Library/CoreServices/SystemVersion.plist"))
                    {
                        IsOSX = true;
                    }
                    string osType = File.Exists(@"/proc/sys/kernel/ostype") ? File.ReadAllText(@"/proc/sys/kernel/ostype") : null;
                    IsLinux = osType?.IndexOf("Linux", StringComparison.OrdinalIgnoreCase) >= 0;
#endif
                }
                catch (Exception) {}
            }
            else
            {
                IsUWP = true;
            }

#if NETSTANDARD2_0
            IsNetStandard = true;
            try
            {
                IsLinux   = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux);
                IsWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
                IsOSX     = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX);
            }
            catch (Exception) {} //throws PlatformNotSupportedException in AWS lambda
            IsUnix = IsOSX || IsLinux;
            HasMultiplePlatformTargets = true;
#elif NET45
            IsNetFramework = true;
            var platform = (int)Environment.OSVersion.Platform;
            IsUnix  = platform == 4 || platform == 6 || platform == 128;
            IsLinux = IsUnix;
            if (Environment.GetEnvironmentVariable("OS")?.IndexOf("Windows", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                IsWindows = true;
            }
#endif
            SupportsExpressions = true;
            SupportsEmit        = !IsIOS;

            ServerUserAgent = "ServiceStack/" +
                              ServiceStackVersion + " "
                              + platformName
                              + (IsMono ? "/Mono" : "/.NET");

            VersionString = ServiceStackVersion.ToString(CultureInfo.InvariantCulture);

            __releaseDate = new DateTime(2001, 01, 01);

            PclExport.Instance.Init();
        }