From c88dee28536a1a86bb742a23200d1f82bd413da4 Mon Sep 17 00:00:00 2001 From: Jesus Fernandez Date: Thu, 11 Jul 2024 20:19:46 +0300 Subject: [PATCH] Sample: Assembly resolution (#11) --- Directory.Packages.props | 1 + PowerAutomate.Desktop.sln | 10 ++++ .../AssemblyResolutionAction.cs | 48 +++++++++++++++++++ .../Modules.AssemblyResolution.Actions.csproj | 16 +++++++ 4 files changed, 75 insertions(+) create mode 100644 samples/Modules.AssemblyResolution.Actions/AssemblyResolutionAction.cs create mode 100644 samples/Modules.AssemblyResolution.Actions/Modules.AssemblyResolution.Actions.csproj diff --git a/Directory.Packages.props b/Directory.Packages.props index 56a7572..a3feca1 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -4,6 +4,7 @@ false + diff --git a/PowerAutomate.Desktop.sln b/PowerAutomate.Desktop.sln index da0eb44..cc5af5c 100644 --- a/PowerAutomate.Desktop.sln +++ b/PowerAutomate.Desktop.sln @@ -41,6 +41,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "GettingStarted", "GettingSt EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modules.GettingStarted.Actions", "samples\Modules.GettingStarted.Actions\Modules.GettingStarted.Actions.csproj", "{1D4838E3-22A7-4D65-91CF-2395778EE92F}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AssemblyResolution", "AssemblyResolution", "{8EC670BB-BC22-44B4-8228-8F053188C908}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modules.AssemblyResolution.Actions", "samples\Modules.AssemblyResolution.Actions\Modules.AssemblyResolution.Actions.csproj", "{146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -87,6 +91,10 @@ Global {1D4838E3-22A7-4D65-91CF-2395778EE92F}.Debug|Any CPU.Build.0 = Debug|Any CPU {1D4838E3-22A7-4D65-91CF-2395778EE92F}.Release|Any CPU.ActiveCfg = Release|Any CPU {1D4838E3-22A7-4D65-91CF-2395778EE92F}.Release|Any CPU.Build.0 = Release|Any CPU + {146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -108,6 +116,8 @@ Global {EE7E70E2-7838-4B4E-BCC1-C74051ACBBA5} = {3B78C634-DFD6-43DA-A30E-33AC42224BD4} {A5E3C936-7C1F-4719-9EAA-37F4AE439EF7} = {64A2FE14-920C-4A2F-8F4A-28B77CC84BA4} {1D4838E3-22A7-4D65-91CF-2395778EE92F} = {A5E3C936-7C1F-4719-9EAA-37F4AE439EF7} + {8EC670BB-BC22-44B4-8228-8F053188C908} = {64A2FE14-920C-4A2F-8F4A-28B77CC84BA4} + {146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD} = {8EC670BB-BC22-44B4-8228-8F053188C908} EndGlobalSection GlobalSection(SharedMSBuildProjectFiles) = preSolution Modules.Actions.Shared\Modules.Actions.Shared.projitems*{7e19e2f1-6de7-4a0e-aed8-7cea38b166af}*SharedItemsImports = 13 diff --git a/samples/Modules.AssemblyResolution.Actions/AssemblyResolutionAction.cs b/samples/Modules.AssemblyResolution.Actions/AssemblyResolutionAction.cs new file mode 100644 index 0000000..e0284dc --- /dev/null +++ b/samples/Modules.AssemblyResolution.Actions/AssemblyResolutionAction.cs @@ -0,0 +1,48 @@ +// -------------------------------------------------------------- +// Copyright (c) Jesus Fernandez. All Rights Reserved. +// -------------------------------------------------------------- + +using System; +using System.Diagnostics; +using System.IO; +using System.Reflection; +using Azure.Core; +using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK; +using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK.Attributes; + +namespace PowerAutomate.Desktop.Modules.AssemblyResolution.Actions; + +[Action] +public class AssemblyResolutionAction : ActionBase +{ + public override void Execute(ActionContext context) + { + Debugger.Launch(); + + try + { + AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; + + var accessToken = new AccessToken(); + Debug.WriteLine($"Access token expires on {accessToken.ExpiresOn}"); + } + finally + { + AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve; + } + } + + private static Assembly? CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) + { + try + { + var assemblyName = new AssemblyName(args.Name); + var directoryName = AppDomain.CurrentDomain.BaseDirectory; + return !string.IsNullOrEmpty(directoryName) ? Assembly.LoadFile(Path.Combine(directoryName, $"{assemblyName.Name}.dll")) : null; + } + catch (Exception) + { + return null; + } + } +} \ No newline at end of file diff --git a/samples/Modules.AssemblyResolution.Actions/Modules.AssemblyResolution.Actions.csproj b/samples/Modules.AssemblyResolution.Actions/Modules.AssemblyResolution.Actions.csproj new file mode 100644 index 0000000..09afbc9 --- /dev/null +++ b/samples/Modules.AssemblyResolution.Actions/Modules.AssemblyResolution.Actions.csproj @@ -0,0 +1,16 @@ + + + + GettingStarted + net472 + latest + enable + true + + + + + + + +