Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Cannot use GetRequiredService<>() when using microsoft.winforms.designer.sdk\1.6.0 #13296

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

Closed
rednaxLamington opened this issue Apr 11, 2025 · 3 comments
Assignees
Labels
area-VSDesigner Windows Forms out-of-proc designer related issues
Milestone

Comments

@rednaxLamington
Copy link

rednaxLamington commented Apr 11, 2025

WinFormsApp1.zip

Environment

Version 17.13.6

.NET version

.NET 8

Did this work in a previous version of Visual Studio and/or previous .NET release?

No response

Issue description

Using the package microsoft.winforms.designer.sdk make the method extension GetRequiredService<>() in Microsoft.Extensions.DependencyInjection unusable which leads to the following error when the application starts:

System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.WinForms.Utilities.Shared, Version=1.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.'

Steps to reproduce

I created a class library containing a winforms usercontrol with several components on it. One of those components (a panel) acts as a docking container, so I included microsoft.winforms.designer.sdk and inherited a class from the ParentControlDesigner.
This just works fine and I have no complaints about this.

So, next I created a winforms application using a hostbuilder. To resolve 'form1' I use GetRequiredService() and this is where it stops working.

GetRequiredService<>() is ambiguous. It's defined in Microsoft.Extensions.DependencyInjection (the one I want to use) but also in the system namespace (declared in Microsoft.WinForms.Utilities.Shared.dll).
The problem is that this dll file is never loaded when I start the application.

So when I start the application I get the load exception.

I've have included a bare-mimimal project to reproduce this error.

Diagnostics


@rednaxLamington rednaxLamington added the untriaged The team needs to look at this issue in the next triage label Apr 11, 2025
@github-actions github-actions bot added the area-VSDesigner Windows Forms out-of-proc designer related issues label Apr 11, 2025
@merriemcgaw merriemcgaw removed the untriaged The team needs to look at this issue in the next triage label Apr 15, 2025
@merriemcgaw merriemcgaw added this to the VS release milestone Apr 15, 2025
@Shyam-Gupta
Copy link
Member

Shyam-Gupta commented Apr 16, 2025

@rednaxLamington Microsoft.WinForms.Utilities.Shared.dll is a design-time assembly. It is not available at runtime.
Microsoft.Winforms.Designer.Sdk NuGet package should be used with design-time assemblies only which means you will need to separate your design time code into a separate project, bundle it into a NuGet package, and then reference this NuGet package in your WinForms .NET application. More details about it are available in following wikis:
a. https://devblogs.microsoft.com/dotnet/custom-controls-for-winforms-out-of-process-designer/
b. https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls-design/designer-overview?view=netdesktop-9.0
c. https://github.com/microsoft/winforms-designer-extensibility/blob/main/docs/sdk/control-library-nuget-package-spec.md

@rednaxLamington
Copy link
Author

I know it's a design-time assembly. I also tried to move the component to a winforms class library (which is always a good thing to do). But because of the transitive behavior of SDK projects, this design time assembly will also be included in the main project.

To be forced to create a package for your component is in my opinion way to complex, difficult to maintain and certainly not practical.

For now I found another way of dealing with this problem. When you create your controls in separate class library, you can block the transitive behavior with making the assets private:

  <ItemGroup>
	  <PackageReference Include="Microsoft.WinForms.Designer.SDK" Version="1.6.0" >
		  <PrivateAssets>all</PrivateAssets>
	  </PackageReference>
  </ItemGroup>

It's a dirty hack, but as long the Microsoft.WinForms.Designer.SDK package is not updated, this will work.

@merriemcgaw
Copy link
Member

There will be an update to the Microsoft.WinFormsDesigner.SDK but unfortunately, I am unable to give a timeframe as yet. I'm glad you were able to find a workaround! For now, I will close this issue since you are not blocked. We will keep this in mind however for the next SDK update.

@merriemcgaw merriemcgaw closed this as not planned Won't fix, can't repro, duplicate, stale May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-VSDesigner Windows Forms out-of-proc designer related issues
Projects
None yet
Development

No branches or pull requests

3 participants