“InitializeLifetimeService is not supported on this proxy” when accessing remote object












1















I'm trying to execute a method from an object which is loaded from an assembly that is dynamically generated with CompilerResults.CompiledAssembly. However, I need this assembly to have restricted permissions because the methods it contains have an unexpected behavior.



I've searched a lot and the Microsoft docs suggest to use the security features provided by .NET Framework 4. This is one of the many sources I've read about sandboxing, which uses a different AppDomain with restricted permissions. Theoretically, I can create instances from the class within the assembly that is loaded in the restricted AppDomain and this instance will be considered a remote object.



To use the new object as a reference, a proxy is created internally when you try to access the object if the class extends the MarshalByRefObject class. In my case, the class which extends MarshalByRefObject is at the top of the hierarchy.



A simplified version of the code based on the link I've mentioned before is:



var permissionSet = ...;
var dllPath = ...;
// Create the AppDomainSetup
var info = new AppDomainSetup
{
// Set the path to the assembly to load.
ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
};
var strongName = assembly.Evidence.GetHostEvidence<StrongName>();
// Create the domain
var sandboxDomain = AppDomain.CreateDomain("RestrictedDomain", null, info, permissionSet, new StrongName { strongName });
var handle = Activator.CreateInstanceFrom(sandboxDomain, dllPath, "MyNamespace.MyClass");
var myLoadedTypeInstance = handle.Unwrap() as MyClass;


The code above works, the assembly is loaded in the AppDomain (only can do it referencing it's dll location, not it's name). But when I try to use a method from myLoadedTypeInstance with two serializable parameters I get an exception. The call is something like myLoadedTypeInstance.MyMethod(param1, param2) and the exception is:



"Method InitializeLifetimeService is not supported on this proxy, this can happen if the method is not marked with OperationContractAttribute or if the interface type is not marked with ServiceContractAttribute."



Some details:




  • I don't know if it matters, but the first parameter is an objects which contains a property of type TChannel, created with ChannelFactory<T>.CreateChannel().

  • It confuses me that the code fails when initializing lifetime service, but if I use RemotingServices.GetLifetimeService(myLoadedTypeInstance) as ILease I can access all the remote object's lifetime info.

  • I've also tried to treat it as a remote object explicitly using Activator.GetObject() with the URL provided by RemotingServices.GetObjectUri(myLoadedTypeInstance), which is redundant because I end up creating two instances of the object. But it was a desperate move which had the same ending.

  • I cannot debug this with VisualStudio as it's running in a separate process.










share|improve this question



























    1















    I'm trying to execute a method from an object which is loaded from an assembly that is dynamically generated with CompilerResults.CompiledAssembly. However, I need this assembly to have restricted permissions because the methods it contains have an unexpected behavior.



    I've searched a lot and the Microsoft docs suggest to use the security features provided by .NET Framework 4. This is one of the many sources I've read about sandboxing, which uses a different AppDomain with restricted permissions. Theoretically, I can create instances from the class within the assembly that is loaded in the restricted AppDomain and this instance will be considered a remote object.



    To use the new object as a reference, a proxy is created internally when you try to access the object if the class extends the MarshalByRefObject class. In my case, the class which extends MarshalByRefObject is at the top of the hierarchy.



    A simplified version of the code based on the link I've mentioned before is:



    var permissionSet = ...;
    var dllPath = ...;
    // Create the AppDomainSetup
    var info = new AppDomainSetup
    {
    // Set the path to the assembly to load.
    ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
    };
    var strongName = assembly.Evidence.GetHostEvidence<StrongName>();
    // Create the domain
    var sandboxDomain = AppDomain.CreateDomain("RestrictedDomain", null, info, permissionSet, new StrongName { strongName });
    var handle = Activator.CreateInstanceFrom(sandboxDomain, dllPath, "MyNamespace.MyClass");
    var myLoadedTypeInstance = handle.Unwrap() as MyClass;


    The code above works, the assembly is loaded in the AppDomain (only can do it referencing it's dll location, not it's name). But when I try to use a method from myLoadedTypeInstance with two serializable parameters I get an exception. The call is something like myLoadedTypeInstance.MyMethod(param1, param2) and the exception is:



    "Method InitializeLifetimeService is not supported on this proxy, this can happen if the method is not marked with OperationContractAttribute or if the interface type is not marked with ServiceContractAttribute."



    Some details:




    • I don't know if it matters, but the first parameter is an objects which contains a property of type TChannel, created with ChannelFactory<T>.CreateChannel().

    • It confuses me that the code fails when initializing lifetime service, but if I use RemotingServices.GetLifetimeService(myLoadedTypeInstance) as ILease I can access all the remote object's lifetime info.

    • I've also tried to treat it as a remote object explicitly using Activator.GetObject() with the URL provided by RemotingServices.GetObjectUri(myLoadedTypeInstance), which is redundant because I end up creating two instances of the object. But it was a desperate move which had the same ending.

    • I cannot debug this with VisualStudio as it's running in a separate process.










    share|improve this question

























      1












      1








      1


      1






      I'm trying to execute a method from an object which is loaded from an assembly that is dynamically generated with CompilerResults.CompiledAssembly. However, I need this assembly to have restricted permissions because the methods it contains have an unexpected behavior.



      I've searched a lot and the Microsoft docs suggest to use the security features provided by .NET Framework 4. This is one of the many sources I've read about sandboxing, which uses a different AppDomain with restricted permissions. Theoretically, I can create instances from the class within the assembly that is loaded in the restricted AppDomain and this instance will be considered a remote object.



      To use the new object as a reference, a proxy is created internally when you try to access the object if the class extends the MarshalByRefObject class. In my case, the class which extends MarshalByRefObject is at the top of the hierarchy.



      A simplified version of the code based on the link I've mentioned before is:



      var permissionSet = ...;
      var dllPath = ...;
      // Create the AppDomainSetup
      var info = new AppDomainSetup
      {
      // Set the path to the assembly to load.
      ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
      };
      var strongName = assembly.Evidence.GetHostEvidence<StrongName>();
      // Create the domain
      var sandboxDomain = AppDomain.CreateDomain("RestrictedDomain", null, info, permissionSet, new StrongName { strongName });
      var handle = Activator.CreateInstanceFrom(sandboxDomain, dllPath, "MyNamespace.MyClass");
      var myLoadedTypeInstance = handle.Unwrap() as MyClass;


      The code above works, the assembly is loaded in the AppDomain (only can do it referencing it's dll location, not it's name). But when I try to use a method from myLoadedTypeInstance with two serializable parameters I get an exception. The call is something like myLoadedTypeInstance.MyMethod(param1, param2) and the exception is:



      "Method InitializeLifetimeService is not supported on this proxy, this can happen if the method is not marked with OperationContractAttribute or if the interface type is not marked with ServiceContractAttribute."



      Some details:




      • I don't know if it matters, but the first parameter is an objects which contains a property of type TChannel, created with ChannelFactory<T>.CreateChannel().

      • It confuses me that the code fails when initializing lifetime service, but if I use RemotingServices.GetLifetimeService(myLoadedTypeInstance) as ILease I can access all the remote object's lifetime info.

      • I've also tried to treat it as a remote object explicitly using Activator.GetObject() with the URL provided by RemotingServices.GetObjectUri(myLoadedTypeInstance), which is redundant because I end up creating two instances of the object. But it was a desperate move which had the same ending.

      • I cannot debug this with VisualStudio as it's running in a separate process.










      share|improve this question














      I'm trying to execute a method from an object which is loaded from an assembly that is dynamically generated with CompilerResults.CompiledAssembly. However, I need this assembly to have restricted permissions because the methods it contains have an unexpected behavior.



      I've searched a lot and the Microsoft docs suggest to use the security features provided by .NET Framework 4. This is one of the many sources I've read about sandboxing, which uses a different AppDomain with restricted permissions. Theoretically, I can create instances from the class within the assembly that is loaded in the restricted AppDomain and this instance will be considered a remote object.



      To use the new object as a reference, a proxy is created internally when you try to access the object if the class extends the MarshalByRefObject class. In my case, the class which extends MarshalByRefObject is at the top of the hierarchy.



      A simplified version of the code based on the link I've mentioned before is:



      var permissionSet = ...;
      var dllPath = ...;
      // Create the AppDomainSetup
      var info = new AppDomainSetup
      {
      // Set the path to the assembly to load.
      ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
      };
      var strongName = assembly.Evidence.GetHostEvidence<StrongName>();
      // Create the domain
      var sandboxDomain = AppDomain.CreateDomain("RestrictedDomain", null, info, permissionSet, new StrongName { strongName });
      var handle = Activator.CreateInstanceFrom(sandboxDomain, dllPath, "MyNamespace.MyClass");
      var myLoadedTypeInstance = handle.Unwrap() as MyClass;


      The code above works, the assembly is loaded in the AppDomain (only can do it referencing it's dll location, not it's name). But when I try to use a method from myLoadedTypeInstance with two serializable parameters I get an exception. The call is something like myLoadedTypeInstance.MyMethod(param1, param2) and the exception is:



      "Method InitializeLifetimeService is not supported on this proxy, this can happen if the method is not marked with OperationContractAttribute or if the interface type is not marked with ServiceContractAttribute."



      Some details:




      • I don't know if it matters, but the first parameter is an objects which contains a property of type TChannel, created with ChannelFactory<T>.CreateChannel().

      • It confuses me that the code fails when initializing lifetime service, but if I use RemotingServices.GetLifetimeService(myLoadedTypeInstance) as ILease I can access all the remote object's lifetime info.

      • I've also tried to treat it as a remote object explicitly using Activator.GetObject() with the URL provided by RemotingServices.GetObjectUri(myLoadedTypeInstance), which is redundant because I end up creating two instances of the object. But it was a desperate move which had the same ending.

      • I cannot debug this with VisualStudio as it's running in a separate process.







      c# sandbox appdomain remoting object-lifetime






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 15:16









      spassarospassaro

      61




      61
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396093%2finitializelifetimeservice-is-not-supported-on-this-proxy-when-accessing-remote%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396093%2finitializelifetimeservice-is-not-supported-on-this-proxy-when-accessing-remote%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)