使用VB.NET将Webforms Autofac参数传递给构造函数 [英] Webforms Autofac parameter to constructor using VB.NET

查看:93
本文介绍了使用VB.NET将Webforms Autofac参数传递给构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想做我认为应该是这样一个简单的任务...使用Autofac将参数传递给构造函数!

So, i want to do what I feel should be such a simple task... pass in a parameter to a constructor using Autofac!

但是,我设法解决了这个问题,但是只是不认为这是正确的,我觉得我在修改Autofac指南中推荐的代码过多

However, I have managed to get a work around working, but just dont think this is correct, I feel i am chaning too much of the recommended code from the Autofac guides

我很高兴在C#或VB.net中得到答案,这无关紧要,代码的位置都是相同的

I am more than happy for answers in C# or VB.net it doesnt matter, the location of code is all the same

所以,这是我的设置(我不介意整洁,只是想暂时让它工作)

So, here is my setup (im not fussed about neatness, just trying to get this to work for now)

在我的global.asax中,我有:

In my global.asax I have:

'***Note, the autofac guide had this a a private shared, see below for why i changed it***
' Provider that holds the application container. 
Public Shared _containerProvider As IContainerProvider

' Instance property that will be used by Autofac HttpModules
' to resolve And inject dependencies.
Public ReadOnly Property ContainerProvider As IContainerProvider Implements IContainerProviderAccessor.ContainerProvider
        Get
            Return _containerProvider
        End Get
End Property

然后在application_start的global.asax中,我拥有:

then within my global.asax within application_start I have:

***again, note, originally I was using IMyClass when registering type... not sure this or that is correct***
Dim builder As New ContainerBuilder()
builder.RegisterType(Of MyClass)().As(Of MyClass)().InstancePerLifetimeScope()

'... continue registering dependencies...
' Once you're done registering things, set the container
' provider up with your registrations.
_containerProvider = New ContainerProvider(builder.Build())

如您所见,_containerProvider最初只是公开的,但是我必须将它设为共享"才能起作用,这马上就感觉不对!!

As you can see, origianly the _containerProvider was just public, but I have had to make it "Shared" for this to work, this feels wrong right away!

现在,在我的webForm1.aspx.vb中,我有以下内容:

so, now, in my webForm1.aspx.vb I have this:

Public Property MyClass2 As IMyClass
Private _myClass As IMyClass

现在,因为我已经将全局变量调整为"registerType"以使用实际的对象,而不是接口(这也不得不更改,这似乎又是错误的),这意味着现在未设置我的Webform公共属性(但是,因为我周围的工作,无论如何我都不需要)

另外,请注意private _myClass ...这是我的解决方法

因此,现在在我的Webform初始化方法中,我具有以下内容:

WebForm1.aspx.vb *

Now, because I have adjusted the global to "registerType" to use the actual object, not the interface (which, again seems wrong having to change that too), means now my webform public property is not being set (but, because of my work around, i dont need that anyway)

Also, note the private _myClass... this is for my workaround

so, now in my Webform init method, i have the following:

WebForm1.aspx.vb*

_myClass = [Global]._containerProvider.RequestLifetime.Resolve(Of MyClass)(New TypedParameter(GetType(HttpRequest), Request))

现在使用正确注入的参数实例化我的_myClass ...太好了,whopadadeoo

...但是...我认为这是不正确的.

which now instantiates my _myClass with the parameter correctly injected in... this is great, whoopadeedoo

...but... I dont think this is correct.

现在,当我不需要将参数传递给构造函数时,一切都很好,不需要更改autofac指南中的任何代码,它就可以了,在我的webform.aspx页上设置public属性很好,真的很好.

但是,当我开始将参数传递给构造函数时,似乎一切都需要调整,这样才能起作用?这样对吗?

我什至尝试了来自autofac的详尽指南,但是通过在我的webForm.aspx页面中执行此操作也完全不起作用:

Now, when I didnt need to pass in a parameter to the construtor, it all worked nice, didnt need to change any of the code from the autofac guide, it just worked, set the public property on my webform.aspx page fine, was really nice.

But, as soon as I start to work with a paramter being passed into the construtor, it seems everything needs to be tweaked so it will work? is this correct?

I have even tried the deligate guide from autofac, but that also doesnt work for me at all by doing this within my webForm.aspx page:

Dim container As ILifetimeScope = [Global]._containerProvider.RequestLifetime
Dim myClassFactory As MyClass = container.Resolve(Of MyClass.Factory)
Dim myClassholding As MyClass = myClassFactory.Invoke("ABC")

甚至尝试了没有调用"的情况,但是由于没有默认属性,因此无法建立索引"

even tried without the "Invoke", but "cannot be indexed because it has no default property"

以防万一,这是"myClass"

Just incase it helps, here is "myClass"

Private _myID as integer
Public Delegate Sub Factory(myID As integer)

Sub New()
End Sub
Sub New(myID As integer)
    _myID = myID
End Sub
Public Sub DoSomething() Implements IDfCookieManager.DoSomething
    'do something with myID
End Sub


我知道我可以将id作为参数传递给DoSomething,但是我想了解如何将其传递给构造函数 所以,我的问题是:

如果这不是怎么做的(我希望它不正确),我将如何做而又不需要更改所有全局设置?

最好是使用专门的工厂或只是解决问题?

我真的需要将全局容器更改为共享/静态的,以便我可以从代码内访问该容器吗?


I know I can pass the id in as a parameter to DoSomething, but i want to understand how to pass this into the constructor so, my questions:

If this is not how to do this (which I am hoping its not correct), how would I do this without needing to change all the global setup??

Is it best to use a deligate factory or just resolve?

do I really need to change the global container to be shared/static, so that i can access the container from within my code?

推荐答案

因此,有两种方法,但首先,不应该弄清楚Autofac如何建议在global.asax中设置ContainerProvider ...我可以保持设置为非共享(非静态),并访问此值,请执行以下操作:

So, there are two ways, but firstly, shouldnt need to mess around with how Autofac suggests setting up the ContainerProvider in global.asax... i can keep it as non shared (not static), and to access this value I do the following:

   Dim cpa As IContainerProviderAccessor = (CType(HttpContext.Current.ApplicationInstance, IContainerProviderAccessor))
   Dim scope As ILifetimeScope = cpa.ContainerProvider.RequestLifetime

此外,在我们的webform.aspx页面中,当我们需要在解析时将参数传递给构造函数时,不应添加Public Property MyClass As IMyClass(否则它将在我们尝试手动解析之前将其解析!

Also, in our webform.aspx page, Public Property MyClass As IMyClass should not be added when we need to pass in parameters to the constructor when resolving (otherwise it will be resolved before we try to manually resolve it!

1:使用TypedParameter传递

这是我经过调整的代码,可以使用resolve(包括上面的行)传递参数:

Here is my adjusted code to pass in the parameters using resolve (including the lines above):

Dim cpa As IContainerProviderAccessor = (CType(HttpContext.Current.ApplicationInstance, IContainerProviderAccessor))
Dim scope As ILifetimeScope = cpa.ContainerProvider.RequestLifetime
Dim myClass As MyClass = scope.Resolve(Of IMyClass)(New TypedParameter(GetType(Integer), 123))

此外,需要删除WebForm1.aspx顶部的公共属性,因为这将自动解决,这意味着,如果我尝试手动解决"该对象,则它已经由autofac自动解决(这就是为什么我认为我的代码最初不起作用的原因),并且已经用空的构造函数实例化了该对象!

Also, having the public property at the top of my WebForm1.aspx needed to be removed, because that will auto resolve, meaning, if i try to "resolve" the object manually, it has already been automatically resolved by autofac (which is why i thought my code wasnt working initially), and has already instantiated the object with the empty constructor!

2:使用熟食工厂

Public Delegate Sub Factory(myID As integer)行不正确,它应该使用Autofac函数来自动设置!因此应为:Public Delegate Function Factory(myID As integer) as MyClass

the line Public Delegate Sub Factory(myID As integer) isnt correct, it should use a function for Autofac to automaticly set this up! so should be: Public Delegate Function Factory(myID As integer) as MyClass

在Global.asax中,我只需要添加此 builder.RegisterType(Of MyClass)().InstancePerLifetimeScope(),因为我们需要一个参数并使用工厂,因此无法附加.As(Of IMyClass)

In Global.asax, I just need to add this builder.RegisterType(Of MyClass)().InstancePerLifetimeScope(), because we require a parameter and using a factory, we cant append the .As(Of IMyClass)

最后,我们的webform1.aspx.vb仅需要以下内容:

Finally, our webform1.aspx.vb just needs this:

Dim cpa As IContainerProviderAccessor = (CType(HttpContext.Current.ApplicationInstance, IContainerProviderAccessor))
Dim scope As ILifetimeScope = cpa.ContainerProvider.RequestLifetime
Dim myClassFactory As MyClass.Factory = scope.Resolve(Of MyClass.Factory)
_myClass = myClassFactory.Invoke(123)


但是,我对此做了些微调整:


however, I tweaked that slightly to this:

Dim cpa As IContainerProviderAccessor = (CType(HttpContext.Current.ApplicationInstance, IContainerProviderAccessor))
Dim scope As ILifetimeScope = cpa.ContainerProvider.RequestLifetime
Dim myClass As MyClass = scope.Resolve(Of MyClass.Factory).Invoke(123)

这篇关于使用VB.NET将Webforms Autofac参数传递给构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆