📄 17.5.txt
字号:
Listing 17.5 Maintaining State Information in Web Services
[WebMethod( Description=”Begins state enabled random number generation”,
EnableSession=true)]
public void BeginNumberList( int Min, int Max, bool AllowDuplicates )
{
Session[“Min”] = Min;
Session[“Max”] = Max;
Session[“Duplicates”] = AllowDuplicates;
Session[“NumberList”] = new ArrayList();
}
[WebMethod( Description=”Gets next number for state enabled number generation”,
EnableSession=true)]
public int NextNumber()
{
if( Session[“Min”] == null || Session[“Max”] == null ||
Session[“Duplicates”] == null || Session[“NumberList”] == null )
return -1;
// generate random number
Random rand = new Random( DateTime.Now.Millisecond );
int newNum = rand.Next( (int) Session[“Min”], (int) Session[“Max”] );
// check if duplicates are allowed
if( ((bool)Session[“Duplicates”]) == false )
{
// get current list and check if any numbers still available
ArrayList list = (ArrayList) Session[“NumberList”];;
if( list.Count >= (int)Session[“Max”]-(int)Session[“Min”] ) return -1;
// generate a non-duplicate random number
while( list.Contains( newNum ) == true ) newNum = rand.Next(
(int)Session[“Min”], Session[“Max”] );
// save in list
list.Add( newNum );
}
return newNum;
}
[WebMethod( Description=”Ends state enabled random number generation”,
EnableSession=true)]
public void EndNumberList()
{
Session.Remove( “Min” );
Session.Remove( “Max” );
Session.Remove( “Duplicates” );
Session.Remove( “NumberList” );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -