仅限Autofac类实现

时间:2017-03-07 19:18:36

标签: vb.net autofac

我正在尝试实现一个类,它在第一次调用时从数据库中获取一些数据,随后对该类的调用将在应用程序的生命周期内返回相同的数据,即使这样调用了此类的新实例。这可能吗?

以下是我的示例尝试,问题是我得到Object not set to an instance of the Object

Public Class session
  Implements IContainerProviderAccessor

  Shared _containerProvider As IContainerProvider
  Private _IUserSessionService As IUserSessionService ' interface to stores data
  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 

1 个答案:

答案 0 :(得分:0)

您需要在 Autofac 中将SessionStore注册为单身,然后将其注入SessionStore类构造函数中(我使用Session代替{{1}与本机Session类发生冲突。 这样,每次获得SessionStore实例时,它都只会使用IUserSessionService实现的一个实例。

您的SessionStore将如下所示:

public class SessionStore : ISessionStore
{
    public SessionStore(IUserSessionService userSessionService)
    {
        this._userSessionService = userSessionService;
    }    

    private readonly IUserSessionService _userSessionService; 

    public String ConnectionString
    {
        get 
        {
            // do what you want with _userSessionService
        }
    }
}

,您的注册将如下所示。

builder.RegisterType<XUserSessionService>().As<IUserSessionService>().SingleInstance();
builder.RegisterType<SessionStore>().As<ISessionStore>().InstancePerRequest(); 

要获取ISessionStore实例,您可以在Page对象中使用属性注入

有关详细信息,请参阅文档中的structuring pages and user controls for Dependency Injection

// MyPage.aspx.cs
public partial class MyPage : Page
{
  // This property will be set for you by the PropertyInjectionModule.
  public ISessionStore SessionStore { get; set; }

  protected void Page_Load(object sender, EventArgs e)
  {
    // Now you can use the property that was set for you.
    label1.Text = this.SessionStore.ConnectionString;
  }
}

如果您尝试访问页面外的会话,请访问IContainerProviderAccessor

ILifetimeScope lifetimeScope = ((IContainerProviderAccessor)HttpContext.Current.ApplicationInstance).ContainerProvider.RequestLifetime;
ISession session = lifetimeScope.Resolve<ISessionStore>();