Home > Net >  Lost ashx write file server Session for help
Lost ashx write file server Session for help

Time:09-16

Ashx using context. The Session [newToken]=loginName; Save the token, if a program needs to be part of external files written to the server, visit this ashx, again the context. The Session newToken for null values, if do not write an external file or read only external files, the context. The Session [newToken] is not null, the example, the writeFile (newToken, loginName); Code can't perform, if execution context. The Session [newToken] disappear

Increased in the web.config & lt; The sessionState mode="InProc" timeout="1800" & gt; & lt;/sessionState> Not

Secondly I also found that this kind of situation. If use the HttpRuntime Cache or static also found this problem,

 
using System;
Using System. Collections. Generic;
Using System. IO;
Using System. Linq;
Using System. The Web;
Using System. Web. SessionState;
Using System. Xml;

The namespace ECInterSys
{
///& lt; summary>
///testSession summary description
///& lt;/summary>
Public class testSession: IHttpHandler, IRequiresSessionState
{

Public void the ProcessRequest (HttpContext context)
{
The context. The Response. ContentType="text/plain";
String login=context. The Request (" login ");
If (login!=null& & The login. The Equals (" yes "))
{
String loginName=context. Request [" loginName "];
//string PWD=context. The Request (" PWD ");
//... Validating a user's password
String newToken=Guid. NewGuid (). The ToString ();
The context. The Session [newToken]=loginName;
//writeFile (newToken, loginName);//the demo of external file is written to the server, if carry out this sentence, then again when access to the context. The Session (token)==null
The context. The Response. The Write (newToken);
return;
}
String token=context. The Request (" token ");
if (! String. IsNullOrEmpty (token))
{
//verification token
If (context. The Session!=null& & The context. The Session [token]!=null)
{
Context. The Response. The Write (context. The Session/token);
return;
}
}
The context. The Response. Write (" hello!" );
}
//test Session storage if write to local file Session message
//example written to a local file content is for reference only, do not write in practical use login information
//sample just said need to write an external file
Private void writeFile (string token, string loginName)
{
The string fileName=the string. Format (@ "{0} bin \ outerFile", AppDomain. CurrentDomain. BaseDirectory. ToString ());

Try
{
XmlDocument XML=new XmlDocument ();
if (! File. The Exists (fileName))
{
XmlDeclaration declaration=XML. CreateXmlDeclaration (" 1.0 ", "GB2312", "");//statement part of the XML document
XML. The AppendChild (declaration);
XmlElement xeRoot=XML. The CreateElement method (" root ");//create a & lt; Thumb> Node
XML. The AppendChild (xeRoot);
}
The else
XML. The Load (fileName);
XmlNode xnRoot=XML. SelectSingleNode ("//root ");
XmlElement xeSub=XML. The CreateElement method (" item ");
XeSub. SetAttribute (" ID ", token);
XeSub. SetAttribute (loginName, loginName);
XML. The Save (fileName);
}
The catch (Exception ex)
{
throw ex;
}
}

Public bool IsReusable
{
The get
{
return false;
}
}
}
}

CodePudding user response:

Firstly, we keep the session to the service rather than a process.

Mode="StateSetver" and open asp.net _state service.

The second.. Even if you use InProc also won't appear the session immediately lost.

So you are the time request is a session?

CodePudding user response:

According to the big though moral emperor supreme instructions, we cannot make any speculative Suggestions to you ask, because it certainly doubt you for weak-minded, belong to bully, teach new typical reply

So we can't tell you, whether should first check whether the file directory have write access,
We also can't tell you
The string fileName=the string. Format (@ "{0} bin \ outerFile", AppDomain. CurrentDomain. BaseDirectory. ToString ());
This code actually may have a problem, because it is suspected that you graduated from elementary school didn't, don't know the web site project actual operation for the system tray asp.net temp directory, directly to the physical path might also is beyond the scope of the web server permissions management

CodePudding user response:

According to the big though moral emperor supreme instructions
1. We can't tell you, path problem
The
reference
private void writeFile (HttpContext context, string token, string loginName)
{
The FileInfo file=new the FileInfo (context. Request. MapPath (" ~/App_Data/temp. TXT "));

Using (FileStream fs=file. Open (FileMode. Append, FileAccess. Write, FileShare ReadWrite))
{
Byte [] buffer.=Encoding UTF8. GetBytes (DateTime. Now. The ToString ());
Fs. Write (buffer, 0, buffer Length);
}

}

2. We can't tell you
According to the debugging, the if (context. The Session!=null& & context. The Session [token]!=null ) here has a problem, the actual session has value, this is certainly doubt the couple will under no breakpoint debugging

3. We can't tell you, do not recommend the use of text, because text concurrent read and write and good operation, please change to save the session with MSSQL database or redis, this is certainly doubt the couple don't have ability to deal with text concurrency issues

CodePudding user response:

According to the big though moral emperor supreme instructions
1. We can't tell you, according to the test, the session has a value, save is null, because you set the loginname, but the above context. The Request (" loginname "); Does not ensure that he is not null (according to the large though moral ethical standards, for serious despise the couple don't test here, don't write assertions)

CodePudding user response:

The context. The Session!=null& & The context. The Session [token]!=null dizzy,

The file you wrote in the bin directory? Dizzy, hurry up and change, wrote the site directory outside,

CodePudding user response:

This kind of situation. If use the HttpRuntime Cache or static also found this problem, you have found that such result, self analysis, what circumstances the Cache, the static loss value? The first thing you think of is application restart, then the application will be restart? In addition to manually restart the iis, etc., and what? .

CodePudding user response:

& lt; The sessionState mode="InProc" timeout="1800" & gt; & lt;/sessionState> Or if you modify the configuration, deposit somewhere else to
3. The InProc mode (default)
& lt; SessionState mode="InProc cookieless"="false" timeout="20" & gt; & lt;/sessionState>
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related