Vb.net - 使用设置为对象的会话变量 [英] Vb.net - using session variables set to objects

查看:80
本文介绍了Vb.net - 使用设置为对象的会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试设置和检索会话变量。这些实际上是一个内存流和流编写器。



我将它们设置在global.aspx中 -



  Sub  Application_Start( ByVal  sender 作为 对象 ByVal  e 正如 EventArgs)
Dim memoryStream As System.IO.MemoryStream()
Dim textWriter 作为系统.IO.TextWriter = System.IO.StreamWriter(memoryStream)
Dim bytesInStream 作为 字节()
结束 < span class =code-keyword> Sub





这似乎有效。无论如何都没有错误。



我尝试了什么:



在aspx.vb页面的函数中,我调用那些会话变量 -



Session(textWriter)。WriteLine(test)



预览时没有错误,但是当我运行它时,它会抛出异常 - 对象变量或没有设置块变量。



它不在循环内或有块,所以我猜它没有看到会话变量????但只有当你运行代码?正如我所说,它并没有在代码本身中显示为错误。



真的卡住了,有什么想法吗?或者就此而言,即使在另一个.aspx page_load子上也可以设置这些对象,并且能够在不同的.aspx页面中的函数中看到它们吗?



我无法在函数所在的页面中设置它们,因为当客户端AJAX调用它时,aspx.vb页面加载很多次并且它必须继续附加到内存流。

解决方案

您已在 Application_Start 方法中创建了三个局部变量,但尚未将它们存储在任何位置。您当然没有将它们添加到会话中。



要将变量存储在会话中,您需要将其添加到会话中。为此,您需要使用 Session_OnStart 事件:

 Sub Session_OnStart(ByVal sender As  Object ,ByVal e As EventArgs)
Dim memoryStream As New System.IO.MemoryStream()
HttpContext.Current.Session.Add( memoryStream,memoryStream)

Dim textWriter As New System.IO.StreamWriter(memoryStream)
HttpContext.Current.Session.Add( textWriter,textWriter)
End Sub



您不能使用 Application_Start ,因为只有在应用程序启动时才会触发一次。 Session_OnStart 事件 [ ^ ]在创建新会话时触发在应用程序运行时多次发生,因为不同的用户连接到它。



ASP.NET应用程序生命周期概述 [ ^ ]


在Global.asax中,我知道您可以设置属性,就像右键单击项目时的属性,然后单击在资源选项卡上,您可以在其中设置字符串资源。但它不是Session对象。这是一个反射对象。



我也知道你可以写资源或创建新资源,但我的VS2015此刻出现故障,我可以举例说明。





 protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}

公共静态字符串GetWebsiteName()
{
string pValue = Properties.Settings.Default.WebsiteName;
返回pValue;
}





要访问字符串,MvcApplication是项目的类,不知道你的是什么。

 string websiteName = MvcApplication.GetWebsiteName(); 


Hi all,

I am trying to set and retrieve session variables. These are actually a memorystream and stream writer.

I set them in global.aspx thus -

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Dim memoryStream As New System.IO.MemoryStream()
    Dim textWriter As System.IO.TextWriter = New System.IO.StreamWriter(memoryStream)
    Dim bytesInStream As Byte()
End Sub



this seems to work. No errors anyway.

What I have tried:

In an in a function in an aspx.vb page I call on those session variables -

Session("textWriter").WriteLine("test")

No errors on preview, but when I run it, it throws the exception - "Object variable or With block variable not set."

It's not within a loop or with block, so I guess it's not seeing the Session Variable ???? But only when you run the code? As I said, it's not showing up as an error in the code itself.

Really stuck, any ideas? Or for that matter, how I could set these objects once even on another .aspx page_load sub and be able to see them in a function in a different .aspx page?

I can't set them in the page that the function resides because the aspx.vb page loads many times when client AJAX calls it and it has to keep appending to the memorystream.

解决方案

You have created three local variables in the Application_Start method, but you haven't stored them anywhere. You certainly haven't added them to the session.

To store the variable in the session, you need to add it to the session. To do that, you would need to use the Session_OnStart event:

Sub Session_OnStart(ByVal sender As Object, ByVal e As EventArgs)
    Dim memoryStream As New System.IO.MemoryStream()
    HttpContext.Current.Session.Add("memoryStream", memoryStream)
    
    Dim textWriter As New System.IO.StreamWriter(memoryStream)
    HttpContext.Current.Session.Add("textWriter", textWriter)
End Sub


You can't use Application_Start, because that only fires once when your application starts up. The Session_OnStart event[^] fires when a new session is created, which will happen multiple times whilst your application is running, as different users connect to it.

ASP.NET Application Life Cycle Overview[^]


In Global.asax, I know you can set a property, like the one when you right click the project, and then click on the resource tab, in which you can set a string resource. But it's not a Session object. It's a reflection Object.

I also know that you can write to the resource or create new ones as well, but my VS2015 is malfunctioning at the moment, and I can't show an example of it.


protected void Application_Start()
{
     AreaRegistration.RegisterAllAreas();
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
     RouteConfig.RegisterRoutes(RouteTable.Routes);                     
}

public static string GetWebsiteName()
{
    string pValue = Properties.Settings.Default.WebsiteName;
    return pValue;
}



To access the string, MvcApplication is the class of the Project, not sure what yours will be.

string websiteName = MvcApplication.GetWebsiteName();


这篇关于Vb.net - 使用设置为对象的会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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