VsixCommunity / Community.VisualStudio.Toolkit.DependencyInjection

Adds Dependency Injection tooling for the Community.VisualStudio.Toolkit
Other
14 stars 7 forks source link

Updating DI and Community Toolkit to any version >17.0.430 #7

Closed kscavitt closed 1 year ago

kscavitt commented 1 year ago

Hello,

Not sure if this should be in this repo or the main Community.VisualStudio.Toolkit repo issue list.

I'm seeing dependency issues when trying to update a vsix package I created to use the latest version of Community.VisualStudio.Toolkit (17.0.475)

From my activity.xml file. image

I was looking at the Dependencies and notice that the Community,VisualStudio.DependencyInjection.Core has a dependency on Community.VisualStudio.Toolkit >= 17.0.430. I assumed that nuget was choosing the lowest dependency and converted the project to use a package.config and the DependencyVersion Highest in my Nuget.Config, but my package still fails to load..

I can see the latest version in my vsix file if I rename and unzip the file. image

Test Repo: https://github.com/kscavitt/VsixExtensionTest/

StevenRasmussen commented 1 year ago

Hello, what version of Visual Studio are you using to test this? I just ran the code in your test repo in VS 2022 and it appeared to work without any issues. If you are developing an extension for VS 2019 (and developing in VS 2019) then you will need to update the references to use the v16.x NuGet packages instead of v17. The versions of the NuGet packages correlate with the minimum version of Visual Studio that you are targeting.

kscavitt commented 1 year ago

Hello, what version of Visual Studio are you using to test this? I just ran the code in your test repo in VS 2022 and it appeared to work without any issues. If you are developing an extension for VS 2019 (and developing in VS 2019) then you will need to update the references to use the v16.x NuGet packages instead of v17. The versions of the NuGet packages correlate with the minimum version of Visual Studio that you are targeting.

VS 2022 Pro: Version 17.3.6

You were able to run the extension in my linked project? It builds just fine. It's when I try to debug/run it that I get an error that points me to the ActivityLog.xml file in the experimental versions of VS 2022.

StevenRasmussen commented 1 year ago

You were able to run the extension in my linked project? It builds just fine. It's when I try to debug/run it that I get an error that points me to the ActivityLog.xml file in the experimental versions of VS 2022.

I was able to debug it and did not receive any errors.

kscavitt commented 1 year ago

You were able to run the extension in my linked project? It builds just fine. It's when I try to debug/run it that I get an error that points me to the ActivityLog.xml file in the experimental versions of VS 2022.

I was able to debug it and did not receive any errors.

This is totally strange. I just setup a new machine with VS 2022 and followed the installation setup guide and I get the same results when I try to launch my extension from View>Other Windows>My Tool Window

image

Checking the xml file. It failed to find Community.VisualStudio.Toolkit 17.0.430. CreateInstance failed for package [TestVSIXProject1Package]Source: 'mscorlib' Description: Could not load file or assembly 'Community.VisualStudio.Toolkit, Version=17.0.430.0, Culture=neutral, PublicKeyToken=79441d341a79572c' or one of its dependencies. The system cannot find the file specified. System.IO.FileNotFoundException: Could not load file or assembly 'Community.VisualStudio.Toolkit, Version=17.0.430.0, Culture=neutral, PublicKeyToken=79441d341a79572c' or one of its dependencies. The system cannot find the file specified. File name: 'Community.VisualStudio.Toolkit, Version=17.0.430.0, Culture=neutral, PublicKeyToken=79441d341a79572c' at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type) at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase) at System.Activator.CreateInstanceFromInternal(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo) at System.AppDomain.CreateInstanceFrom(String assemblyFile, String typeName) === Pre-bind state information === LOG: DisplayName = Community.VisualStudio.Toolkit, Version=17.0.430.0, Culture=neutral, PublicKeyToken=79441d341a79572c (Fully-specified) LOG: Appbase = file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/ LOG: Initial PrivatePath = NULL Calling assembly : Community.VisualStudio.Toolkit.DependencyInjection.Core, Version=17.0.13.0, Culture=neutral, PublicKeyToken=79441d341a79572c. === LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Users\kscav\AppData\Local\Microsoft\VisualStudio\17.0_1de195baExp\devenv.exe.config LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config. LOG: Post-policy reference: Community.VisualStudio.Toolkit, Version=17.0.430.0, Culture=neutral, PublicKeyToken=79441d341a79572c LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PublicAssemblies/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PublicAssemblies/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PrivateAssemblies/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PrivateAssemblies/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/CommonExtensions/Platform/Debugger/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/CommonExtensions/Platform/Debugger/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/x64/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/x64/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/Community.VisualStudio.Toolkit.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PublicAssemblies/Community.VisualStudio.Toolkit.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PublicAssemblies/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PrivateAssemblies/Community.VisualStudio.Toolkit.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PrivateAssemblies/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow/Community.VisualStudio.Toolkit.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/CommonExtensions/Platform/Debugger/Community.VisualStudio.Toolkit.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/CommonExtensions/Platform/Debugger/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/Community.VisualStudio.Toolkit.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/x64/Community.VisualStudio.Toolkit.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/x64/Community.VisualStudio.Toolkit/Community.VisualStudio.Toolkit.EXE.

StevenRasmussen commented 1 year ago

I never tried opening up the tool window. Is that when you’re getting the error? I can try that tomorrow and see what happens for me.

kscavitt commented 1 year ago

I never tried opening up the tool window. Is that when you’re getting the error? I can try that tomorrow and see what happens for me.

Yes. I get the error when I try to load the tool window. This project is just the basic template that come with the Extensibility Essentials 2022 install but modified to use dependency injection via Community.VisualStudio.Toolkit.DependencyInjection.

image

It should show the Click Me button. (it works fine with Community.VisualStudio.Toolkit 17.0.430.0) image

KC250330 commented 1 year ago

I thought I should probably add the steps to reproduce.

Steps to Reproduce

  1. Create a new project using the VSIX Project w/Tool Window (Community) template called VSIXProject1.
  2. Add the following Nuget packages. a. Community.VisualStudio.Toolkit.DependencyInjection.Core.17 b. Community.VisualStudio.Toolkit.DependencyInjection.Microsoft.17
  3. Modify the MyToolWindowCommand.cs file to inherit from BaseDICommand instead of BaseCommand.
  4. Modify the VSIXProject1Package.cs. a. Inherit from MicrosoftDIToolkitPackage<VSIXProject1Package> instead of ToolkitPackage b. Remove the current InitializeAsync method and replace it with the following.

          protected override void InitializeServices(IServiceCollection services)
          {
              // Register all commands in the assembly.
              services.RegisterCommands(ServiceLifetime.Singleton);
           }
    
          protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
          {
              // Ensure that you first call the base.InitializeAsync method.
              await base.InitializeAsync(cancellationToken, progress);
    
          }
  5. Update the Nuget package Community.VisualStudio.Toolkit to anything above 17.0.430, In my case I updated to the latest.(17.0.475)
  6. Run in an experimental VS 2022 instance.
  7. Open the tool window View>Other Windows>My Tool Windows

You will see a message similar to the following. image

If you browse to the path of the ActivityLog.xml file listed in the similar message above, open it in edge, scroll to the bottom and you should see something like the picture below.

image

Any help is greatly appreciated. KC

StevenRasmussen commented 1 year ago

@KC250330 - I was able to reproduce the issue and it appears that it comes down to an issue with binding redirection. The way that Visual Studio loads the extensions prevents it from using automatic binding redirection. See here. I was able to overcome this by adding the attribute as described in that SO question to the assembly.cs file:

[assembly: ProvideBindingRedirection(AssemblyName = "Community.VisualStudio.Toolkit",
    NewVersion = "17.0.475.0", OldVersionLowerBound = "17.0.430.0", OldVersionUpperBound = "17.0.451.0")]

One thing I did notice is that it appears you have a lot of unnecessary references to VS SDK related packages in that project. You should only need to reference the Community.VisualStudio.Toolkit package and the DI package.

KC250330 commented 1 year ago

@StevenRasmussen - Thanks for the investigation. That fixed the "Community.VisualStudio.Toolkit" version issue. However it's now complaining about "Microsoft.Extensions.DependencyInjection".

Do you happen to know of a guide that describes how to figure out what the binding redirect versions should look like? More specifically how to find the NewVersion, OldVersionLowerBound, OldVersionUpperBound? I would like to figure out how to address these types of issues in the future.

image

StevenRasmussen commented 1 year ago

I unfortunately don't have anything as far as a guide goes for determining what the binding redirect should look like. What I did for the one above is simply look at what version of the package was installed and used that as the NewVersion. I then just picked the version for the package that was expected as the OldVersionLowerBound (this is in the error message). Then, looking at what versions are available for the package, I picked the version right below what was installed as the OldVersionUpperBound.

Regarding this error in general: There appears to be two ways to solve it:

  1. Install the version that the error message tells you is not able to be found (this is probably the preferred solution and may work for the Microsft.Extensions.DependencyInjection error you're now facing).
  2. Or, add a binding redirect attribute for the version that is installed.

Hopefully that helps :)

KC250330 commented 1 year ago

Community.VisualStudio.Toolkit

@StevenRasmussen - I can live with the Microsoft.Extensions.DependcyInjection 5.0.2 version. I am able to get my real extension to build, and run now using your advice for adding the ProvideBindingRedirection to the AssemblyInfo.cs file.

Thank you for all your help! KC

kscavitt commented 1 year ago

The issue was resolved by adding the following binding redirect into the AssemblyInfo.cs file.

[assembly: ProvideBindingRedirection(AssemblyName = "Community.VisualStudio.Toolkit",
    NewVersion = "17.0.475.0", OldVersionLowerBound = "17.0.430.0", OldVersionUpperBound = "17.0.451.0")]