我正在尝试使用命名管道创建跨应用程序的asp.net缓存服务(类似于memcached或AppFabric / Velocity)。基本前提是您可以获取或设置在连接到命名管道的任何其他应用程序之间共享的缓存项目。
部分原因是要将发送到缓存的对象序列化为byte []以方便PipeStream.Read()和PipeStream.Write()方法。由于缓存将保存任意数量的任意对象类型/类,我不希望在每个对象类型/类上设置[Serializable]属性,因此我选择创建一个包装类,它将是[Serializable]并且具有我可以用来传输缓存项的通用对象类型字段,类似于此处使用的方法:http://www.eggheadcafe.com/articles/20060404.asp
一切似乎最初都适用于内置类型,但现在我正在尝试发送一个List<>自定义类型(一个类)的对象,我得到一个异常,我的自定义类需要[Serializable] ...这是我想避免的。
以下是代码:
服务器 -
class Server
{
static Dictionary<string, object> Cache = new Dictionary<string, object>();
static Dictionary<string, DateTime> CacheExpireTime = new Dictionary<string, DateTime>();
static void Main(string[] args)
{
new Thread(HandleGets).Start();
new Thread(HandleSets).Start();
}
static protected void HandleSets()
{
PipeSecurity ps = new PipeSecurity();
PipeAccessRule par = new PipeAccessRule("Everyone", PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow);
ps.AddAccessRule(par);
while (true)
{
using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("MemCacheSet", PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.None, 0, 0, ps))
{
pipeServer.WaitForConnection();
CacheAction ca = CacheAction.FromBytes(pipeServer.ReadAll());
Cache[ca.DictionaryKey] = ca.DictionaryValue;
CacheExpireTime[ca.DictionaryKey] = ca.TimeOfExpire;
}
}
}
static protected void HandleGets()
{
PipeSecurity ps = new PipeSecurity();
PipeAccessRule par = new PipeAccessRule("Everyone", PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow);
ps.AddAccessRule(par);
while (true)
{
using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("MemCacheGet", PipeDirection.InOut,1,PipeTransmissionMode.Byte,PipeOptions.None,0,0,ps))
{
pipeServer.WaitForConnection();
CacheAction ca = CacheAction.FromBytes(pipeServer.ReadAll());
CacheAction resp = new CacheAction();
resp.DictionaryKey = ca.DictionaryKey;
if (Cache.ContainsKey(ca.DictionaryKey) && CacheExpireTime[ca.DictionaryKey]>=DateTime.Now)
resp.DictionaryValue = Cache[ca.DictionaryKey];
pipeServer.WriteAll(resp.ToBytes());
}
}
}
}
客户端方法(在静态类中) -
static object GetItem(string inKey)
{
object rVal;
using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", "MemCacheGet", PipeDirection.InOut))
{
pipeStream.Connect();
CacheAction ca = new CacheAction();
ca.DictionaryKey = inKey;
pipeStream.WriteAll(ca.ToBytes());
ca = CacheAction.FromBytes(pipeStream.ReadAll());
rVal = ca.DictionaryValue;
}
return rVal;
}
static void SetItem(string inName, object inItem, TimeSpan? expireTime = null)
{
if (!expireTime.HasValue)
expireTime = new TimeSpan(0, 10, 0);
using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", "MemCacheSet", PipeDirection.Out))
{
pipeStream.Connect();
CacheAction ca = new CacheAction();
ca.DictionaryKey = inName;
ca.DictionaryValue = inItem;
ca.TimeOfExpire = DateTime.Now + expireTime.Value;
pipeStream.WriteAll(ca.ToBytes());
}
}
共享代码:
[Serializable]
public class CacheAction
{
public string DictionaryKey;
public object DictionaryValue;
public DateTime TimeOfExpire;
public static CacheAction FromBytes(byte[] inBytes)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(inBytes);
CacheAction p = (CacheAction)bf.Deserialize(ms);
return p;
}
public byte[] ToBytes()
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, this);
return ms.ToArray();
}
}
public static class MyExtensions
{
public static byte[] ReadAll(this NamedPipeClientStream np)
{
byte[] size = new byte[4];
np.Read(size, 0, 4);
int iSize = BitConverter.ToInt32(size, 0);
byte[] rVal = new byte[iSize];
np.Read(rVal, 0, iSize);
return rVal;
}
public static byte[] ReadAll(this NamedPipeServerStream np)
{
byte[] size = new byte[4];
np.Read(size, 0, 4);
int iSize = BitConverter.ToInt32(size, 0);
byte[] rVal = new byte[iSize];
np.Read(rVal, 0, iSize);
return rVal;
}
public static void WriteAll(this NamedPipeClientStream np, byte[] toWrite)
{
byte[] size = BitConverter.GetBytes(toWrite.Length);
np.Write(size, 0, size.Length);
np.Write(toWrite, 0, toWrite.Length);
}
public static void WriteAll(this NamedPipeServerStream np, byte[] toWrite)
{
byte[] size = BitConverter.GetBytes(toWrite.Length);
np.Write(size, 0, size.Length);
np.Write(toWrite, 0, toWrite.Length);
}
}
最后导致问题的特定用例:
class MemCachedSession
{
public string SessionId { get; set; }
public DateTime Created { get; set; }
public DateTime Expires { get; set; }
public DateTime LockDate { get; set; }
public int LockId { get; set; }
public int Timeout { get; set; }
public bool Locked { get; set; }
public string SessionItems { get; set; }
public int Flags { get; set; }
}
像这样使用:
SetItem("MemCacheSessionStateProvider", new List<MemCachedSession>(), new TimeSpan(7, 0, 0, 0, 0))
抛出的异常是在调用bf.Serialize(ms,this)时的ToBytes()方法中,它说: “在Assembly'API中键入'MemCachedSession',Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'未标记为可序列化。'”
(编辑)为什么这样,和/或我是否在正确的轨道上做我想做的事情?(编辑)
整个想法是封装类(CacheAction)是可序列化的,以防止这种情况,这在某种程度上起作用,但不适用于此自定义类(MemCachedSession)。
很抱歉这个大问题,也许答案也不简单(即整个方法都错了),但如果有人花时间提供一些见解,我们将不胜感激! 谢谢,
答案 0 :(得分:1)
默认情况下,序列化程序只能序列化基本类型。任何其他类型必须使用属性Serializable
进行标记才能符合序列化条件。
举个例子:
[Serializable] class AClass {
int myInt;
long myLong;
string myString;
}
[Serializable] class BClass {
int myInt;
AClass myAClass;
}
[Serializable] class CClass {
int myInt;
DClass otherClass;
}
class DClass {
int myInt;
long myLong;
string myString;
}
AClass
可以序列化,因为它只包含基元并被装饰为Serializable
。
BClass
可以序列化,因为它的整个对象图是原始的或Serializable
。
CClass
被装饰为Serializable
,但在尝试序列化时会抛出错误,因为它包含的成员不是。这可以通过将otherClass
声明替换为[NonSerialized] DClass otherClass;
来防止,但这对您的情况没有帮助,因为该成员不会像属性所暗示的那样使用对象图的其余部分进行序列化。
DClass
无法序列化,即使它不包含任何非基本字段,因为它未被装饰为Serializable
。
可以在MSDN网站上找到更多信息:http://msdn.microsoft.com/en-us/library/vstudio/ms233843.aspx
TLDR;整个方法都是错误的 - 所有缓存的对象都必须标记为Serializable。