将字典序列化为XML C#
我有一个字典,我必须在用户登录时添加一个对象,并且需要在用户注销windows时删除该对象。我还将字典序列化为xml。因为我对C#和windows服务都是新手,所以我有些怀疑 这是我的密码将字典序列化为XML C#,c#,xml,windows,usersession,C#,Xml,Windows,Usersession,我有一个字典,我必须在用户登录时添加一个对象,并且需要在用户注销windows时删除该对象。我还将字典序列化为xml。因为我对C#和windows服务都是新手,所以我有些怀疑 这是我的密码 public class UserSessionCapturePlugin : IInformServiceHandler { public Dictionary<int, UserSessionInfo> UserSessionLookupTable = new Di
public class UserSessionCapturePlugin : IInformServiceHandler
{
public Dictionary<int, UserSessionInfo> UserSessionLookupTable = new Dictionary<int, UserSessionInfo>();
public void OnSessionChange(SessionChangeDescription changeDescription)
{
switch (changeDescription.Reason)
{
//Case of Logon
case SessionChangeReason.SessionLogon:
//CreateRunningProcessesLog("UserSession-SessionLogon");
UserSession userSessionLogin = new UserSession()
{
UserName = MachineHelper.GetUsername(),
UserGuid = MachineHelper.GetUserGuid(),
MachineGuid = MachineHelper.GetMachineGUID(),
LoginTime = DateTime.Now.ToUniversalTime(),
SessionGuid = Guid.NewGuid(), //New Guid generated for tracking the UserSession, this will be created on on logon
IsReadable = false,
SessionId = changeDescription.SessionId,
};
UserSessionInfo userSessionInfoLogin = new UserSessionInfo()
{
UserName = MachineHelper.GetUsername(),
SessionGuid = userSessionLogin.SessionGuid,
IsActiveUser = true,
SessionId = changeDescription.SessionId,
LoginTime = userSessionLogin.LoginTime,
State = RowState.Added,
};
UserSessionLookupTable.Add(userSessionInfoLogin.SessionId, userSessionInfoLogin);
XmlSerializer serializer = new XmlSerializer(typeof(Dictionary<Guid, UserSessionInfo>));
TextWriter textWriter = new StreamWriter(@"UserSessionLookupDictionarySerialized.xml");
serializer.Serialize(textWriter, UserSessionLookupTable);
textWriter.Close();
//Case of Logoff
case SessionChangeReason.SessionLogoff:
UserSession userSessionLogoff = new UserSession()
{
UserName = MachineHelper.GetUsername(),
UserGuid = MachineHelper.GetUserGuid(),
MachineGuid = MachineHelper.GetMachineGUID(),
LogOffTime = DateTime.Now.ToUniversalTime(),
IsReadable = true,
SessionId = changeDescription.SessionId,
};
UserSessionLookupTable.Remove(userSessionLogoff.SessionId);
XmlSerializer serializer = new XmlSerializer(typeof(Dictionary<Guid, UserSessionInfo>));
TextWriter textWriter = new StreamWriter(@"UserSessionLookupDictionarySerialized.xml");
serializer.Serialize(textWriter, UserSessionLookupTable);
textWriter.Close();
break;
}
}
}
公共类UserSessionCapturePlugin:IInformServiceHandler
{
公共字典UserSessionLookupTable=新字典();
public void OnSessionChange(sessionchangescription changescription)
{
开关(changeDescription.Reason)
{
//登录案例
案例SessionChangeReason.SessionLogon:
//CreateRunningProcessLog(“用户会话会话登录”);
UserSession userSessionLogin=new UserSession()
{
UserName=MachineHelper.GetUsername(),
UserGuid=MachineHelper.GetUserGuid(),
MachineGuid=MachineHelper.GetMachineGUID(),
LoginTime=DateTime.Now.ToUniversalTime(),
SessionGuid=Guid.NewGuid(),//为跟踪用户会话而生成的新Guid,将在登录时创建
IsReadable=false,
SessionId=changescription.SessionId,
};
UserSessionInfo UserSessionInfo=new UserSessionInfo()
{
UserName=MachineHelper.GetUsername(),
SessionGuid=userSessionLogin.SessionGuid,
IsActiveUser=true,
SessionId=changescription.SessionId,
LoginTime=userSessionLogin.LoginTime,
State=RowState.Added,
};
添加(usersessioninfo.SessionId,usersessioninfo);
XmlSerializer serializer=新的XmlSerializer(类型(字典));
TextWriter TextWriter=newstreamWriter(@“UserSessionLookupDictionarySerialized.xml”);
serializer.Serialize(textWriter,UserSessionLookupTable);
textWriter.Close();
//注销案件
案例SessionChangeReason.SessionLogoff:
UserSession userSessionLogoff=新的UserSession()
{
UserName=MachineHelper.GetUsername(),
UserGuid=MachineHelper.GetUserGuid(),
MachineGuid=MachineHelper.GetMachineGUID(),
LogOffTime=DateTime.Now.ToUniversalTime(),
IsReadable=true,
SessionId=changescription.SessionId,
};
移除(userSessionLogoff.SessionId);
XmlSerializer serializer=新的XmlSerializer(类型(字典));
TextWriter TextWriter=newstreamWriter(@“UserSessionLookupDictionarySerialized.xml”);
serializer.Serialize(textWriter,UserSessionLookupTable);
textWriter.Close();
打破
}
}
}
但我有以下疑问
我目前无法调试或运行代码,这就是我将其发布到此处的原因。因为
UserSessionLookupTable
是非静态对象,因此其寿命与父对象的寿命相同。只要您对所有用户使用相同的UserSessionCapturePlugin
实例,就会保存所有用户的记录
如果您正在为每个请求创建不同的UserSessionLookupTable
实例,该实例将只保存最后一个用户的记录
另外,XmlSerializer
也不能
为了纠正行为并记录所有用户会话,我建议修改保存会话信息的方式
- 在保存新用户会话信息之前,首先加载并反序列化现有xml,在其中添加新记录,再次序列化并保存到文件。(若这是第一次,您需要确保该文件不存在,以便处理该文件)
- 在删除用户会话信息之前,首先加载并反序列化现有xml,删除要删除的记录,再次序列化并将其保存回文件
Dictionary<Guid, UserSessionInfo> LoadUserSessionData()
{
try
{
var serializer = new XmlSerializer(typeof(KeyValuePair<Guid, UserSessionInfo>[]));
using (var stream = new FileStream(@"UserSessionLookupDictionarySerialized.xml", FileMode.Open))
{
var sessionData = (KeyValuePair<Guid, UserSessionInfo>[])serializer.Deserialize(stream)
return sessionData.ToDictionary(i => i.Key, i => i.Value);
}
}
catch (FileNotFoundException)
{
return new Dictionary<int, UserSessionInfo>();
}
}
void SaveUserSessionData(Dictionary<Guid, UserSessionInfo> sessionData)
{
var serializer = new XmlSerializer(typeof(KeyValuePair<Guid, UserSessionInfo>[]));
using (var stream = new FileStream(@"UserSessionLookupDictionarySerialized.xml", FileMode. OpenOrCreate))
{
serializer.Serialize(stream, sessionData.ToArray());
}
}
由于
UserSessionLookupTable
是非静态对象,因此其寿命与父对象的寿命相同。只要您对所有用户使用相同的UserSessionCapturePlugin
实例,就会保存所有用户的记录
如果您正在为每个请求创建不同的UserSessionLookupTable
实例,该实例将只保存最后一个用户的记录
另外,XmlSerializer
也不能
为了纠正行为并记录所有用户会话,我建议修改保存会话信息的方式
- 在保存新用户会话信息之前,首先加载并反序列化现有xml,在其中添加新记录,再次序列化并保存到文件。(若这是第一次,您需要确保该文件不存在,以便处理该文件)
- 在删除用户会话信息之前,首先加载并反序列化现有xml,删除要删除的记录,再次序列化并将其保存回文件
Dictionary<Guid, UserSessionInfo> LoadUserSessionData()
{
try
{
var serializer = new XmlSerializer(typeof(KeyValuePair<Guid, UserSessionInfo>[]));
using (var stream = new FileStream(@"UserSessionLookupDictionarySerialized.xml", FileMode.Open))
{
var sessionData = (KeyValuePair<Guid, UserSessionInfo>[])serializer.Deserialize(stream)
return sessionData.ToDictionary(i => i.Key, i => i.Value);
}
}
catch (FileNotFoundException)
{
return new Dictionary<int, UserSessionInfo>();
}
}
void SaveUserSessionData(Dictionary<Guid, UserSessionInfo> sessionData)
{
var serializer = new XmlSerializer(typeof(KeyValuePair<Guid, UserSessionInfo>[]));
using (var stream = new FileStream(@"UserSessionLookupDictionarySerialized.xml", FileMode. OpenOrCreate))
{
serializer.Serialize(stream, sessionData.ToArray());
}
}
您在哪里初始化了
用户会话lookuptable
?很抱歉。。。我已在类中启动,但未包含在代码中(我仅在此处粘贴了方法)我的关注点