The Labs \ Source Viewer \ SSCLI \ System.Runtime.Remoting \ WellKnownClientTypeEntry

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** File:    RemotingConfiguration.cs
  18. **
  19. ** Purpose: Classes for interfacing with remoting configuration
  20. **            settings
  21. **
  22. **
  23. ===========================================================*/
  24. using System;
  25. using System.Security;
  26. using System.Security.Permissions;
  27. using System.Runtime.Remoting.Activation;
  28. using System.Runtime.Remoting.Contexts;
  29. using StackCrawlMark = System.Threading.StackCrawlMark;
  30. using System.Runtime.Versioning;
  31. namespace System.Runtime.Remoting
  32. {
  33.     // Configuration - provides static methods interfacing with
  34.     // configuration settings.
  35.     [System.Runtime.InteropServices.ComVisible(true)]
  36.     public static class RemotingConfiguration
  37.     {
  38.         private static bool s_ListeningForActivationRequests = false;
  39.        
  40.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  41.         [ResourceExposure(ResourceScope.Machine)]
  42.         [ResourceConsumption(ResourceScope.Machine)]
  43.         [Obsolete("Use System.Runtime.Remoting.RemotingConfiguration.Configure(string fileName, bool ensureSecurity) instead.", false)]
  44.         public static void Configure(string filename)
  45.         {
  46.                 /*ensureSecurity*/            Configure(filename, false);
  47.         }
  48.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  49.         [ResourceExposure(ResourceScope.Machine)]
  50.         [ResourceConsumption(ResourceScope.Machine)]
  51.         public static void Configure(string filename, bool ensureSecurity)
  52.         {
  53.             RemotingConfigHandler.DoConfiguration(filename, ensureSecurity);
  54.            
  55.             // Set a flag in the VM to mark that remoting is configured
  56.             // This will enable us to decide if activation for MBR
  57.             // objects should go through the managed codepath
  58.             RemotingServices.InternalSetRemoteActivationConfigured();
  59.            
  60.         }
  61.         // Configure
  62.         public static string ApplicationName {
  63.             get {
  64.                 if (!RemotingConfigHandler.HasApplicationNameBeenSet())
  65.                     return null;
  66.                 else
  67.                     return RemotingConfigHandler.ApplicationName;
  68.             }
  69.            
  70.             [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  71.             set { RemotingConfigHandler.ApplicationName = value; }
  72.         }
  73.         // ApplicationName
  74.        
  75.         // The application id is prepended to object uri's.
  76.         public static string ApplicationId {
  77.             [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  78.             get { return Identity.AppDomainUniqueId; }
  79.         }
  80.         // ApplicationId
  81.         public static string ProcessId {
  82.             [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  83.             get { return Identity.ProcessGuid; }
  84.         }
  85.        
  86.         public static CustomErrorsModes CustomErrorsMode {
  87.             get { return RemotingConfigHandler.CustomErrorsMode; }
  88.            
  89.             [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  90.             set { RemotingConfigHandler.CustomErrorsMode = value; }
  91.         }
  92.        
  93.        
  94.         public static bool CustomErrorsEnabled(bool isLocalRequest)
  95.         {
  96.             switch (CustomErrorsMode) {
  97.                 case CustomErrorsModes.Off:
  98.                     return false;
  99.                 case CustomErrorsModes.On:
  100.                    
  101.                     return true;
  102.                 case CustomErrorsModes.RemoteOnly:
  103.                    
  104.                     return (!isLocalRequest);
  105.                 default:
  106.                    
  107.                     return true;
  108.             }
  109.         }
  110.        
  111.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  112.         public static void RegisterActivatedServiceType(Type type)
  113.         {
  114.             ActivatedServiceTypeEntry entry = new ActivatedServiceTypeEntry(type);
  115.             RemotingConfiguration.RegisterActivatedServiceType(entry);
  116.         }
  117.         // RegisterActivatedServiceType
  118.        
  119.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  120.         public static void RegisterActivatedServiceType(ActivatedServiceTypeEntry entry)
  121.         {
  122.             RemotingConfigHandler.RegisterActivatedServiceType(entry);
  123.            
  124.             // make sure we're listening for activation requests
  125.             // (all registrations for activated service types will come through here)
  126.             if (!s_ListeningForActivationRequests) {
  127.                 s_ListeningForActivationRequests = true;
  128.                 ActivationServices.StartListeningForRemoteRequests();
  129.             }
  130.         }
  131.         // RegisterActivatedServiceType
  132.        
  133.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  134.         public static void RegisterWellKnownServiceType(Type type, string objectUri, WellKnownObjectMode mode)
  135.         {
  136.             WellKnownServiceTypeEntry wke = new WellKnownServiceTypeEntry(type, objectUri, mode);
  137.             RemotingConfiguration.RegisterWellKnownServiceType(wke);
  138.         }
  139.         // RegisterWellKnownServiceType
  140.        
  141.        
  142.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  143.         public static void RegisterWellKnownServiceType(WellKnownServiceTypeEntry entry)
  144.         {
  145.             RemotingConfigHandler.RegisterWellKnownServiceType(entry);
  146.         }
  147.         // RegisterWellKnownServiceType
  148.        
  149.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  150.         public static void RegisterActivatedClientType(Type type, string appUrl)
  151.         {
  152.             ActivatedClientTypeEntry acte = new ActivatedClientTypeEntry(type, appUrl);
  153.             RemotingConfiguration.RegisterActivatedClientType(acte);
  154.         }
  155.         // RegisterActivatedClientType
  156.        
  157.        
  158.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  159.         public static void RegisterActivatedClientType(ActivatedClientTypeEntry entry)
  160.         {
  161.             RemotingConfigHandler.RegisterActivatedClientType(entry);
  162.            
  163.             // all registrations for activated client types will come through here
  164.             RemotingServices.InternalSetRemoteActivationConfigured();
  165.         }
  166.         // RegisterActivatedClientType
  167.        
  168.        
  169.        
  170.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  171.         public static void RegisterWellKnownClientType(Type type, string objectUrl)
  172.         {
  173.             WellKnownClientTypeEntry wke = new WellKnownClientTypeEntry(type, objectUrl);
  174.             RemotingConfiguration.RegisterWellKnownClientType(wke);
  175.         }
  176.         // RegisterWellKnownClientType
  177.        
  178.        
  179.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  180.         public static void RegisterWellKnownClientType(WellKnownClientTypeEntry entry)
  181.         {
  182.             RemotingConfigHandler.RegisterWellKnownClientType(entry);
  183.            
  184.             // all registrations for wellknown client types will come through here
  185.             RemotingServices.InternalSetRemoteActivationConfigured();
  186.         }
  187.         // RegisterWellKnownClientType
  188.        
  189.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  190.         public static ActivatedServiceTypeEntry[] GetRegisteredActivatedServiceTypes()
  191.         {
  192.             return RemotingConfigHandler.GetRegisteredActivatedServiceTypes();
  193.         }
  194.        
  195.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  196.         public static WellKnownServiceTypeEntry[] GetRegisteredWellKnownServiceTypes()
  197.         {
  198.             return RemotingConfigHandler.GetRegisteredWellKnownServiceTypes();
  199.         }
  200.        
  201.        
  202.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  203.         public static ActivatedClientTypeEntry[] GetRegisteredActivatedClientTypes()
  204.         {
  205.             return RemotingConfigHandler.GetRegisteredActivatedClientTypes();
  206.         }
  207.        
  208.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  209.         public static WellKnownClientTypeEntry[] GetRegisteredWellKnownClientTypes()
  210.         {
  211.             return RemotingConfigHandler.GetRegisteredWellKnownClientTypes();
  212.         }
  213.        
  214.        
  215.         // This is used at the client end to check if an activation needs
  216.         // to go remote.
  217.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  218.         public static ActivatedClientTypeEntry IsRemotelyActivatedClientType(Type svrType)
  219.         {
  220.             return RemotingConfigHandler.IsRemotelyActivatedClientType(svrType);
  221.         }
  222.        
  223.         // This is used at the client end to check if an activation needs
  224.         // to go remote.
  225.        
  226.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  227.         public static ActivatedClientTypeEntry IsRemotelyActivatedClientType(string typeName, string assemblyName)
  228.         {
  229.             return RemotingConfigHandler.IsRemotelyActivatedClientType(typeName, assemblyName);
  230.         }
  231.        
  232.        
  233.         // This is used at the client end to check if a "new Foo" needs to
  234.         // happen via a Connect() under the covers.
  235.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  236.         public static WellKnownClientTypeEntry IsWellKnownClientType(Type svrType)
  237.         {
  238.             return RemotingConfigHandler.IsWellKnownClientType(svrType);
  239.         }
  240.        
  241.         // This is used at the client end to check if a "new Foo" needs to
  242.         // happen via a Connect() under the covers.
  243.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  244.         public static WellKnownClientTypeEntry IsWellKnownClientType(string typeName, string assemblyName)
  245.         {
  246.             return RemotingConfigHandler.IsWellKnownClientType(typeName, assemblyName);
  247.         }
  248.        
  249.         // This is used at the server end to check if a type being activated
  250.         // is explicitly allowed by the server.
  251.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  252.         public static bool IsActivationAllowed(Type svrType)
  253.         {
  254.             return RemotingConfigHandler.IsActivationAllowed(svrType);
  255.         }
  256.        
  257.     }
  258.     // class Configuration
  259.    
  260.    
  261.     //
  262.     // The following classes are used to register and retrieve remoted type information
  263.     //
  264.    
  265.     // Base class for all configuration entries
  266.     [System.Runtime.InteropServices.ComVisible(true)]
  267.     public class TypeEntry
  268.     {
  269.         string _typeName;
  270.         string _assemblyName;
  271.         RemoteAppEntry _cachedRemoteAppEntry = null;
  272.        
  273.         protected TypeEntry()
  274.         {
  275.             // Forbid creation of this class by outside users...
  276.         }
  277.        
  278.         public string TypeName {
  279.             get { return _typeName; }
  280.             set { _typeName = value; }
  281.         }
  282.        
  283.         public string AssemblyName {
  284.             get { return _assemblyName; }
  285.             set { _assemblyName = value; }
  286.         }
  287.        
  288.         internal void CacheRemoteAppEntry(RemoteAppEntry entry)
  289.         {
  290.             _cachedRemoteAppEntry = entry;
  291.         }
  292.         internal RemoteAppEntry GetRemoteAppEntry()
  293.         {
  294.             return _cachedRemoteAppEntry;
  295.         }
  296.        
  297.     }
  298.    
  299.     [System.Runtime.InteropServices.ComVisible(true)]
  300.     public class ActivatedClientTypeEntry : TypeEntry
  301.     {
  302.         string _appUrl;
  303.         // url of application to activate the type in
  304.         // optional data
  305.         IContextAttribute[] _contextAttributes = null;
  306.        
  307.        
  308.         public ActivatedClientTypeEntry(string typeName, string assemblyName, string appUrl)
  309.         {
  310.             if (typeName == null)
  311.                 throw new ArgumentNullException("typeName");
  312.             if (assemblyName == null)
  313.                 throw new ArgumentNullException("assemblyName");
  314.             if (appUrl == null)
  315.                 throw new ArgumentNullException("appUrl");
  316.            
  317.             TypeName = typeName;
  318.             AssemblyName = assemblyName;
  319.             _appUrl = appUrl;
  320.         }
  321.         // ActivatedClientTypeEntry
  322.         public ActivatedClientTypeEntry(Type type, string appUrl)
  323.         {
  324.             if (type == null)
  325.                 throw new ArgumentNullException("type");
  326.             if (appUrl == null)
  327.                 throw new ArgumentNullException("appUrl");
  328.            
  329.             TypeName = type.FullName;
  330.             AssemblyName = type.Module.Assembly.nGetSimpleName();
  331.             _appUrl = appUrl;
  332.         }
  333.         // ActivatedClientTypeEntry
  334.         public string ApplicationUrl {
  335.             get { return _appUrl; }
  336.         }
  337.        
  338.         public Type ObjectType {
  339.             get {
  340.                 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  341.                 return RuntimeType.PrivateGetType(TypeName + ", " + AssemblyName, false, false, ref stackMark);
  342.             }
  343.         }
  344.        
  345.         public IContextAttribute[] ContextAttributes {
  346.             get { return _contextAttributes; }
  347.             set { _contextAttributes = value; }
  348.         }
  349.        
  350.        
  351.         public override string ToString()
  352.         {
  353.             return "type='" + TypeName + ", " + AssemblyName + "'; appUrl=" + _appUrl;
  354.         }
  355.        
  356.     }
  357.     // class ActivatedClientTypeEntry
  358.    
  359.     [System.Runtime.InteropServices.ComVisible(true)]
  360.     public class ActivatedServiceTypeEntry : TypeEntry
  361.     {
  362.         // optional data
  363.         IContextAttribute[] _contextAttributes = null;
  364.        
  365.        
  366.         public ActivatedServiceTypeEntry(string typeName, string assemblyName)
  367.         {
  368.             if (typeName == null)
  369.                 throw new ArgumentNullException("typeName");
  370.             if (assemblyName == null)
  371.                 throw new ArgumentNullException("assemblyName");
  372.             TypeName = typeName;
  373.             AssemblyName = assemblyName;
  374.         }
  375.        
  376.         public ActivatedServiceTypeEntry(Type type)
  377.         {
  378.             if (type == null)
  379.                 throw new ArgumentNullException("type");
  380.             TypeName = type.FullName;
  381.             AssemblyName = type.Module.Assembly.nGetSimpleName();
  382.         }
  383.        
  384.         public Type ObjectType {
  385.             get {
  386.                 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  387.                 return RuntimeType.PrivateGetType(TypeName + ", " + AssemblyName, false, false, ref stackMark);
  388.             }
  389.         }
  390.        
  391.         public IContextAttribute[] ContextAttributes {
  392.             get { return _contextAttributes; }
  393.             set { _contextAttributes = value; }
  394.         }
  395.        
  396.        
  397.         public override string ToString()
  398.         {
  399.             return "type='" + TypeName + ", " + AssemblyName + "'";
  400.         }
  401.        
  402.     }
  403.     // class ActivatedServiceTypeEntry
  404.    
  405.     [System.Runtime.InteropServices.ComVisible(true)]
  406.     public class WellKnownClientTypeEntry : TypeEntry
  407.     {
  408.         string _objectUrl;
  409.        
  410.         // optional data
  411.         string _appUrl = null;
  412.         // url of application to associate this object with
  413.        
  414.         public WellKnownClientTypeEntry(string typeName, string assemblyName, string objectUrl)
  415.         {
  416.             if (typeName == null)
  417.                 throw new ArgumentNullException("typeName");
  418.             if (assemblyName == null)
  419.                 throw new ArgumentNullException("assemblyName");
  420.             if (objectUrl == null)
  421.                 throw new ArgumentNullException("objectUrl");
  422.            
  423.             TypeName = typeName;
  424.             AssemblyName = assemblyName;
  425.             _objectUrl = objectUrl;
  426.         }
  427.        
  428.         public WellKnownClientTypeEntry(Type type, string objectUrl)
  429.         {
  430.             if (type == null)
  431.                 throw new ArgumentNullException("type");
  432.             if (objectUrl == null)
  433.                 throw new ArgumentNullException("objectUrl");
  434.            
  435.             TypeName = type.FullName;
  436.             AssemblyName = type.Module.Assembly.nGetSimpleName();
  437.             _objectUrl = objectUrl;
  438.         }
  439.        
  440.         public string ObjectUrl {
  441.             get { return _objectUrl; }
  442.         }
  443.        
  444.         public Type ObjectType {
  445.             get {
  446.                 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  447.                 return RuntimeType.PrivateGetType(TypeName + ", " + AssemblyName, false, false, ref stackMark);
  448.             }
  449.         }
  450.        
  451.         public string ApplicationUrl {
  452.             get { return _appUrl; }
  453.             set { _appUrl = value; }
  454.         }
  455.        
  456.         public override string ToString()
  457.         {
  458.             string str = "type='" + TypeName + ", " + AssemblyName + "'; url=" + _objectUrl;
  459.             if (_appUrl != null)
  460.                 str += "; appUrl=" + _appUrl;
  461.             return str;
  462.         }
  463.        
  464.     }
  465.     // class WellKnownClientTypeEntry
  466.    
  467.     [System.Runtime.InteropServices.ComVisible(true)]
  468.     public class WellKnownServiceTypeEntry : TypeEntry
  469.     {
  470.         string _objectUri;
  471.         WellKnownObjectMode _mode;
  472.        
  473.         // optional data
  474.         IContextAttribute[] _contextAttributes = null;
  475.        
  476.         public WellKnownServiceTypeEntry(string typeName, string assemblyName, string objectUri, WellKnownObjectMode mode)
  477.         {
  478.             if (typeName == null)
  479.                 throw new ArgumentNullException("typeName");
  480.             if (assemblyName == null)
  481.                 throw new ArgumentNullException("assemblyName");
  482.             if (objectUri == null)
  483.                 throw new ArgumentNullException("objectUri");
  484.            
  485.             TypeName = typeName;
  486.             AssemblyName = assemblyName;
  487.             _objectUri = objectUri;
  488.             _mode = mode;
  489.         }
  490.        
  491.         public WellKnownServiceTypeEntry(Type type, string objectUri, WellKnownObjectMode mode)
  492.         {
  493.             if (type == null)
  494.                 throw new ArgumentNullException("type");
  495.             if (objectUri == null)
  496.                 throw new ArgumentNullException("objectUri");
  497.            
  498.             TypeName = type.FullName;
  499.             AssemblyName = type.Module.Assembly.FullName;
  500.             _objectUri = objectUri;
  501.             _mode = mode;
  502.         }
  503.        
  504.         public string ObjectUri {
  505.             get { return _objectUri; }
  506.         }
  507.        
  508.         public WellKnownObjectMode Mode {
  509.             get { return _mode; }
  510.         }
  511.        
  512.         public Type ObjectType {
  513.             get {
  514.                 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  515.                 return RuntimeType.PrivateGetType(TypeName + ", " + AssemblyName, false, false, ref stackMark);
  516.             }
  517.         }
  518.        
  519.         public IContextAttribute[] ContextAttributes {
  520.             get { return _contextAttributes; }
  521.             set { _contextAttributes = value; }
  522.         }
  523.        
  524.        
  525.         public override string ToString()
  526.         {
  527.             return "type='" + TypeName + ", " + AssemblyName + "'; objectUri=" + _objectUri + "; mode=" + _mode.ToString();
  528.         }
  529.        
  530.     }
  531.     // class WellKnownServiceTypeEntry
  532.     internal class RemoteAppEntry
  533.     {
  534.         string _remoteAppName;
  535.         string _remoteAppURI;
  536.         internal RemoteAppEntry(string appName, string appURI)
  537.         {
  538.             BCLDebug.Assert(appURI != null, "Bad remote app URI");
  539.             _remoteAppName = appName;
  540.             _remoteAppURI = appURI;
  541.         }
  542.         internal string GetAppURI()
  543.         {
  544.             return _remoteAppURI;
  545.         }
  546.     }
  547.     // class RemoteAppEntry
  548.     [System.Runtime.InteropServices.ComVisible(true)]
  549.     public enum CustomErrorsModes
  550.     {
  551.         On,
  552.         Off,
  553.         RemoteOnly
  554.     }
  555.    
  556. }
  557. // namespace System.Runtime.Remoting

Developer Fusion