This project is read-only.

VB.NET problem: error BC30652: Reference required to assembly SimpleInjector

Oct 16, 2012 at 7:48 AM

Hi,

I'm using the latest version of SimpleInjector that NuGet provides me and I'm unable to use the container in most scenarios because the build with msbuild fails (building with visual studio 2010 works fine).

The simplest scenario is the use of the SimpleInjector container inside a singleton. For this I have created a class named "CContainerSingleton" inside a "ContainerSingleton" project:

Imports System.Runtime.CompilerServices
Imports SimpleInjector

Public Class CContainerSingleton

  Private Shared s_oInstance As CContainerSingleton
  Private ReadOnly m_oContainer As Container

  ' Private constructor to force use as singleton
  Private Sub New()
    m_oContainer = New Container()
  End Sub

  <MethodImpl(MethodImplOptions.Synchronized)>
  Public Shared Function GetInstance() As CContainerSingleton
    If s_oInstance Is Nothing Then
      s_oInstance = New CContainerSingleton()
    End If
    Return s_oInstance
  End Function

  Public ReadOnly Property Container As Container
    Get
      Return m_oContainer
    End Get
  End Property

End Class
If I try to access the container or even pass it as a parameter to another method, the build will fail.
A simple class in a separate project like
Imports ContainerSingleton
Imports SimpleInjector

Public Class CUsingContainer

  Public Sub WillNotCompile()
    Dim oSingleton As CContainerSingleton = CContainerSingleton.GetInstance
    ' The following line causes: error BC30652: Reference required to assembly 'SimpleInjector, Version=1.5.0.12199, Culture=neutral, PublicKeyToken=984cb50dea722e99' containing the type 'SimpleInjector.Container'. Add one to your project. 
    Dim oContainer As Container = oSingleton.Container

  End Sub

End Class

Public Interface IFoo
  End Interface

Public Class CFoo
  Implements IFoo
End Class
will cause the following error when trying to build the solution with msbuild:
 C:\work\VS_Default\SimpleInjectorBuildProblems\SimpleInjectorBuildProblems\CUsingContainer.vb(9,35): error BC30652: Reference required to assembly 'SimpleInjector, Version=1.5.0.12199, Culture=neutral, PublicKeyToken=984cb50dea722e99' containing the type 'SimpleInjector.Container'. Add one to your project.
A reference to SimpleInjector however is present in both projects and maintained with NuGet.
Can anyone provide help?
sincerely,
creatom
Oct 16, 2012 at 8:15 AM

I'm not familiar with MSBuild, but the first thing that comes to mind is that the SimpleInjector.dll isn't copied. Take a look at the directory where the project is built. My guess is that there is no SimpleInjector.dll. It is unlikely that this is caused by Simple Injector; you will probably experience this with all NuGet packages that you install.

Oct 16, 2012 at 9:01 AM
dot_net_junkie wrote:

I'm not familiar with MSBuild, but the first thing that comes to mind is that the SimpleInjector.dll isn't copied. Take a look at the directory where the project is built. My guess is that there is no SimpleInjector.dll. It is unlikely that this is caused by Simple Injector; you will probably experience this with all NuGet packages that you install.

I am experiencing this problem only with SimpleInjector, no other NuGet package causes  these problems. There is a SimpleInjector.dll inside the /bin/Debug folder of the singleton-project (the /bin/Debug of the other project containing CUsingContainer is empty because the build fails). There also is one inside /packages/SimpleInjector.1.5.etc

Both project files have a reference to SimpleInjector:

<Reference Include="SimpleInjector">
  <HintPath>..\packages\SimpleInjector.1.5.0.12199\lib\net35\SimpleInjector.dll</HintPath>
</Reference>
Besides, the singleton project builds fine. It's only the project using the singleton (or rather doing anything with SimpleInjector.Container) which will fail to build with msbuild.

Oct 16, 2012 at 9:12 AM

Not completely related with your problem, but the fact that you are hiding the container in its own project is a smell of the Service Locator anti-pattern. What you are doing is only needed when you are directly referencing the container from application code (instead of using it only within the Composition Root). When reference the container directly from within your application project (for instance the Application_Start of your MVC project) you might even been able to resolve your issue.

Oct 16, 2012 at 10:37 AM

Unfortunately I see no other way than using a singleton to carry the SimpleInjector container because I am also using custom attributes which make use of it.

Even if I change the code so that the initialization happens in the using class

 Public Sub WillNotCompile()
    Dim oSingleton As CContainerSingleton = CContainerSingleton.GetInstance
    ' The following line causes: error BC30652: Reference required to assembly 'SimpleInjector, Version=1.5.0.12199, Culture=neutral, PublicKeyToken=984cb50dea722e99' containing the type 'SimpleInjector.Container'. Add one to your project. 
    'Dim oContainer As Container = oSingleton.Container
    Dim oContainer As New Container
    oSingleton.Container = oContainer

  End Sub

with Container being just a simple property of the CContainerSingleton-class (of type SimpleInjector.Container), it still fails on the line

    oSingleton.Container = oContainer
not on the line before. I could even use the container to register or resolve types but I cannot pass it as an argument

Oct 16, 2012 at 10:41 AM

Unfortunately, I'm unable to help you with the compile issue. However, I'm really interested to give some feedback on your attribute approach, since that solution might be sub-optimal. If you wish to get some feedback, please create a new thread/discussion and we'll takee it from there.

Oct 16, 2012 at 11:08 AM
dot_net_junkie wrote:

Unfortunately, I'm unable to help you with the compile issue. However, I'm really interested to give some feedback on your attribute approach, since that solution might be sub-optimal. If you wish to get some feedback, please create a new thread/discussion and we'll takee it from there.

For this follow-up discussion, I've created the discussion at https://simpleinjector.codeplex.com/discussions/399542


The same code compiles just fine when using C#. (using C# is not an option btw)

Oct 16, 2012 at 12:31 PM

The error message implies that the main project should contain a reference to the SimpleInjector.dll. Try adding it to your web application project (or where you get the compile error).

Oct 16, 2012 at 12:33 PM
dot_net_junkie wrote:

The error message implies that the main project should contain a reference to the SimpleInjector.dll. Try adding it to your web application project (or where you get the compile error).

There is a reference. See my second post. If there wouldn't be a reference, I wouldn't be able to

Dim oContainer As New Container()

Mar 20, 2013 at 10:41 AM
The problem still exists with v1.6.1 - when trying to access properties with type SimpleInjector.Container across (VB) project boundaries, compilation will fail.
A project/class offering properties (eg. for a unit testing framework) compiles fine.
A different project/class using these properties does not (example: ProjectA.TestBase uses SimpleInjector.Container, offers Property. ProjectB.SomeTestclass inherits from TestBase - works fine until a line of code tries to access the Container-Property!)

Works fine (Project A):
Imports SimpleInjector ' no error

Public MustInherit Class TestBase 

Protected Property Container As Container

End Class
Fails to build (Project B):
Imports SimpleInjector ' no error

Public Class SomeTestclass
Inherits TestBase ' compiles just fine with this

Public Sub SetUp()

Container.RegisterSingle(Of ISomeInterface)(SomeInstance) ' Line X, causes error when building.

End Sub

Public Sub TestSomething()
Dim oSomething as ISomeInterface = Container.GetInstance(Of ISomeInterface)() ' Line Y, also causes build-error
End Sub

End Class
If the two lines X and Y are removed, then it builds fine. If it were a reference-problem then it should not be able to build due to the inheritance to the base-class (and the imports-statement).

Therefore this is NOT a problem with missing references since everything builds fine when the property is not accessed. This is still a problem happening only with SimpleInjector and VB. The problem might not be reproducible with C# (in fact, in a a different project I encapsulate the SimpleInjector in a C# project which is then used in a VB project just fine - but that can't be an option to wrap the entire SimpleInjector.Container every time)

P.S.: I can't use v2.0 or above because some projects are still running .NET 3.5
Mar 20, 2013 at 11:21 AM
I'm not sure how this problem could actually be related to SimpleInjector.dll or the way it is packaged with NuGet. Have you tried including SimpleInjector.dll as plain old reference instead of NuGet package, and checking this assembly in into source control in the solution folder? Perhaps your Build server has problems pulling in the NuGet package for some reason.

Note that nothing has changed in the way Simple Injector 2 is packaged through NuGet, so I expect that you run into the same problems with Simple Injector 2.
Mar 20, 2013 at 11:25 AM
The reference is there, it is copied to the output directory and can be referenced just fine. MSBuild just fails when anything SimpleInjector.Container is accessed across project boundaries (declaration works fine, ie: no reference problems).
I'm strongly suspecting that there is an inherent problem with MSBuild when it comes to C# and VB interoperability which just happens to manifest itself with SimpleInjector.
Mar 20, 2013 at 11:40 AM
I don't know what the problem is. I haven't seen anybody else having problems with this. However, I must admit that I don't expect Simple Injector being used that much on VB projects. VB + DI + NuGet + MSBuild is not a combination I see every day.

Unfortunately, there's not much I can do about this at this moment. But if the root cause is in the way SimpleInjector.dll is built, please let me know. In that case we might be able to change the Simple Injector build scripts and roll out a version that fixes this problem.
Mar 20, 2013 at 11:53 AM
The problem can easily be reconstructed by creating a Solution with two VB projects in it. Any use (as return type, property type, parameter type, ...) across the boundaries of the project will result in MSBuild/Vbc failing to compile.

VB might not be as common as C#, but it is still a valid .NET language.
Mar 20, 2013 at 1:44 PM
I'm not saying VB is not a valid language, but it's just a problem I've never seen other users run into. Unfortunately, my resources are limited, so unless you can tell me exactly what is wrong with the SimpleInjector.dll and how I can fix it, there is not much I can do for you.