Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to retieve value from Session["propertyName"] #37

Closed
umeshone opened this issue Mar 8, 2017 · 2 comments
Closed

Unable to retieve value from Session["propertyName"] #37

umeshone opened this issue Mar 8, 2017 · 2 comments

Comments

@umeshone
Copy link

umeshone commented Mar 8, 2017

Hi Oleg,

I have declared the project in the Main() method as follows:-

var project = 
        new ManagedProject("MyProduct", 
            new Dir(@"C:\",new Dir(@"MyWindowService", new DirFiles(@"..\MyWindowsService\bin\Release\*.*"))),
            new ElevatedManagedAction(CustomActions.InstallService,
                                          Return.check,
                                          When.After,
                                          Step.InstallFiles,
                                          Condition.Always),
            new ElevatedManagedAction(CustomActions.UnInstallService,
                                      Return.check,
                                      When.Before,
                                      Step.RemoveFiles,
                                      Condition.BeingRemoved));

I am assigning some value to Session["propertyName"] in the next button click event of the CustomDialog as follows:-

void next_Click(object sender, EventArgs e)
{
     MsiRuntime.Session["ConnectionString"] = connectionStringValue;
}

I am trying to access this value in the Custom Action InstallService method and Msi_AfterInstall method as follows:-

[CustomAction]
public static ActionResult InstallService(Session session)
{
      var connectionString = session["ConnectionString"];
       .........
}

static void Msi_AfterInstall(SetupEventArgs e)
{
      var connectionString=e.Session["ConnectionString"];
      ...........
}

But the value is not accessible and its throwing below error for both the methods when I am trying to get the value from Session["propertyName"].

Errors:-
Info: Exception thrown by custom action:
Info: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.Deployment.WindowsInstaller.InstallerException: Cannot access session details from a non-immediate custom action
at Microsoft.Deployment.WindowsInstaller.Session.ValidateSessionAccess()
at Microsoft.Deployment.WindowsInstaller.Session.get_Item(String property)
at MySetup.CustomActions.InstallService(Session session) in H:\MySetup\CustomActions.cs:line 21
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object parameters, Object arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture)
at Microsoft.Deployment.WindowsInstaller.CustomActionProxy.InvokeCustomAction(Int32 sessionHandle, String entryPoint, IntPtr remotingDelegatePtr)

Info: Calling custom action WixSharp!WixSharp.ManagedProjectActions.WixSharp_AfterInstall_Action
Info: WixSharp aborted the session because of the error:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.Deployment.WindowsInstaller.InstallerException: Cannot access session details from a non-immediate custom action
at Microsoft.Deployment.WindowsInstaller.Session.ValidateSessionAccess()
at Microsoft.Deployment.WindowsInstaller.Session.get_Item(String property)
at MySetup.Program.Msi_AfterInstall(SetupEventArgs e)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object parameters, Object arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture)
at WixSharp.ManagedProject.InvokeClientHandler(String info, SetupEventArgs eventArgs)
at WixSharp.ManagedProject.InvokeClientHandlers(Session session, String eventName, IShellView UIShell)

Am I following some wrong syntax?
Please advise. Thanks in advance.

@oleg-shilo
Copy link
Owner

The problem you are experiencing is due to the fact that MSI architecture does not allow you accessing the session properties from the deferred actions (see (Deferred Actions)[https://github.com/oleg-shilo/wixsharp/wiki/Deployment-scenarios#deferred-actions]). The DeferredActions and "Managed Setup/Setup Events" samples also shows the connection_string use-case:

project.DefaultDeferredProperties += ",ConnectionString";
...
static void Msi_AfterInstall(SetupEventArgs e)
{
      var connectionString=e.Session.Property("ConnectionString");
      ...........
}

@vasa911
Copy link

vasa911 commented Mar 28, 2017

@umeshone
You can also use CustomActionData to retrieve properties inside deferred actions;

You need to declare action with Property, that you can set this property in Session where you need.

//action declaration 
var installServiceAction = new ElevatedManagedAction( CustomActions.InstallService,
                                                     Return.check,
                                                     When.After,
                                                     Step.InstallFiles,
                                                     Condition.Always);

installServiceAction.UsesProperties = "CONNECTIONSTRING=[CONzNECTIONSTRING]";

And retrieve property using CustomActionData

//get property inside action
[CustomAction]
public static ActionResult InstallService(Session session)
{
       CustomActionData data = session.CustomActionData;
       var connectionString = data ["CONNECTIONSTRING"];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants