Class only implementation

15 views
Skip to first unread message

ma...@dagleyinnovation.com

unread,
Mar 7, 2017, 8:18:10 AM3/7/17
to Autofac
Hi I am new Autofac but I want to provide a class that will create a connection object and sort it for the life time of the Application. it may be the that i can not do this and i have to inject the module from my Presentation layer where i have a application start method.

see my example below

Public Class session
Implements IContainerProviderAccessor
Shared _containerProvider As IContainerProvider
Private _IUserSessionService As IUserSessionService
Public Property Usersession As IUserSessionService
Get
Return _IUserSessionService
End Get
Set(value As IUserSessionService)
_IUserSessionService = value
End Set
End Property

Public ReadOnly Property ContainerProvider As IContainerProvider Implements IContainerProviderAccessor.ContainerProvider
Get
Return _containerProvider
End Get
End Property


Public Function GetConnection() As String
Dim UserSessionDetail As New UserSessionDetails
' Do we have a container if not register one
If IsNothing(_containerProvider) Then
RegisterConnection()
Dim UserSessionDetail As New UserSessionDetails
UserSessionDetail.connection_string = GoAndGetOneFromOtherSource
Usersession.AddSession(UserSessionDetail)
Return UserSessionDetail.connection_string
Else
Usersession.GetUserSession()
Return UserSessionDetail.connection_string
End If
End Function


Private Sub RegisterConnection()
Dim builder As ContainerBuilder = New ContainerBuilder()

builder.RegisterType(Of UserSessionService).As(Of IUserSessionService).InstancePerRequest()

'Auto Fac Binding
_containerProvider = New ContainerProvider(builder.Build())

End Sub


End Class

this compiles but errros on Usersession.AddSession(UserSessionDetail) with object not set to an instance of the object, what am I doing wrong?

Travis Illig

unread,
Mar 7, 2017, 11:28:08 AM3/7/17
to Autofac
Hi, Mark!

A better place to ask "How do I...?" sorts of questions is StackOverflow. It's a whole Q&A site dedicated to answering questions just like this. The benefit of asking there is that there are far more people monitoring StackOverflow (including the people on this list) than there are just here on the mailing list. Ask your question there and add the "autofac" tag. Also add tags and info about the type of application you're writing. Without some additional context (which should be part of your SO question) it may be hard to figure out what's going on. For example, include in your question:
  • What is UserSessionService? Where is it declared?
  • When is AddSession being called?
  • Is there an exception? What's the full message with stack trace?
  • Where is this Session class getting used?
Reply all
Reply to author
Forward
0 new messages