YairHalberstadt / stronginject

compile time dependency injection for .NET
MIT License
845 stars 24 forks source link

Source not being generated #132

Closed trampster closed 3 years ago

trampster commented 3 years ago

I have a .net 5 WPF project that I tried to use StronInject on. But the injection code is not being generated.

My Container is as follows:

using JsonSrcGenInstantAnswer.ViewModels;
using StrongInject;

namespace JsonSrcGenInstantAnswer
{
   [Register(typeof(SearchViewModel))]
   [Register(typeof(MainWindow))]
   public partial class InstantAnswerContainer : IContainer<MainWindow>
   {
   }
}

However when I build the build fails I get the following errors in my build output (but strangely not in my Errors list)

1>C:\Work\JsonSrcGenInstantAnswer\JsonSrcGenInstantAnswer\InstantAnswerContainer.cs(8,50,8,72): error CS0535: 'InstantAnswerContainer' does not implement interface member 'IContainer<MainWindow>.Run<TResult, TParam>(Func<MainWindow, TParam, TResult>, TParam)'
1>C:\Work\JsonSrcGenInstantAnswer\JsonSrcGenInstantAnswer\InstantAnswerContainer.cs(8,50,8,72): error CS0535: 'InstantAnswerContainer' does not implement interface member 'IContainer<MainWindow>.Resolve()'
1>C:\Work\JsonSrcGenInstantAnswer\JsonSrcGenInstantAnswer\InstantAnswerContainer.cs(8,50,8,72): error CS0535: 'InstantAnswerContainer' does not implement interface member 'IDisposable.Dispose()'
YairHalberstadt commented 3 years ago

Which version of wpf are you using? I believe generators only work on 5.1 of the SDK

trampster commented 3 years ago

How do I change to 5.1 of the SDK?

my project file looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="search.png" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="StrongInject" Version="1.2.0" />
  </ItemGroup>

  <ItemGroup>
    <Resource Include="search.png">
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </Resource>
  </ItemGroup>

</Project>
trampster commented 3 years ago

Do you mean the .Net SDK? because the lastest version of that I can find is 5.0.8. My Visual Studio is upto date.

trampster commented 3 years ago

I found this issue which appears to have a work around https://github.com/dotnet/wpf/issues/3404

Which is to add the following to my project file:

<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>

Seems that this will be on by default in .net 6.

This is obviously not the fault of StrongInject so I'm closing this.

YairHalberstadt commented 3 years ago

It's also on by default in dotnet SDK 5.0.2

https://github.com/dotnet/sdk/pull/15465