Reputation: 16358
I have a "theme repository" to store theme objects
ITheme
Public Interface ITheme
Property Name As String
Property Url As String
End Interface
IThemeRepository
Public Interface IThemeRepository
Property Themes As IList(Of ITheme)
Sub Add(theme As ITheme)
End Interface
During my application setup I want to add some themes to the repository and then I want anything to be able to access the repository as a sort of in-memory database. This means that the repository needs to have a singleton lifestyle. Even with the following code, a new instance of ThemeRepository is always instantiated:
RepositoriesInstaller
Public Class RepositoriesInstaller
Implements IWindsorInstaller
Public Sub Install(container As Castle.Windsor.IWindsorContainer,
store As Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore) _
Implements IWindsorInstaller.Install
container.Register(Classes.FromAssembly(Assemblies.Infrastructure) _
.BasedOn(Of IThemeRepository) _
.WithServiceDefaultInterfaces _
.LifestyleSingleton)
End Sub
End Class
ThemeRepository
Public Class ThemeRepository
Implements IThemeRepository
Public Sub New()
Themes = New List(Of ITheme)
End Sub
Public Sub Add(theme As ITheme) Implements IThemeRepository.Add
Themes.Add(theme)
End Sub
Public Property Themes As IList(Of ITheme) Implements IThemeRepository.Themes
End Class
A ThemeRepository
service is always found successfully, but it's not a singleton. A new instance of ThemeRepository
is always resolved. I.e. New()
is always called on ThemeRepository
. What's going on?
(C# or VB.NET code are acceptable in answers)
Upvotes: 0
Views: 275
Reputation: 16358
The cause of this strange behaviour was, of course, a silly mistake on my part.
Through a poor coding mistake I was registering my dependencies on every Http request. This meant that everything would work as expected on the first request but then fail after that.
Don't initialise and set up your Castle Windsor container more than once.
Upvotes: 1