如何序列化来自不同程序集的二进制数据
本文关键字:程序集 二进制 数据 序列化 | 更新日期: 2023-09-27 18:34:39
我正在编写一个将序列化数据发送到客户端应用程序的 MVC4 应用程序。
客户端代码
下面是客户端的代码。
public static void GetMalicousExtensions(string User_ID, string UC, string Implementation_ID)
{
try
{
System.Diagnostics.Debug.WriteLine("GetMalExt Running");
//Call to MVC4 server.
WebRequest request = WebRequest.Create(string.Format("http://localhost:35555/Secure/SendExtensions?User_ID={0}&UC={1}&Implementation_ID={2}"
, User_ID
, UC
, Implementation_ID
));
System.Diagnostics.Debug.WriteLine("Request initialized.");
request.Method = "GET";
System.Diagnostics.Debug.WriteLine("Method set.");
request.Timeout = 10000;//10 seconds for debug, switch to 5 for release.
BinaryFormatter serializer = new BinaryFormatter();
System.Diagnostics.Debug.WriteLine("Serializer initialized.");
System.Diagnostics.Debug.WriteLine("Getting the stream.");
//The object of type BlockExtension is in a shared file.
BlockedExtension bTest = ((BlockedExtension)serializer.Deserialize(request.GetResponse().GetResponseStream(), null));
//request.
System.Diagnostics.Debug.WriteLine("GetMalExt de-serialized");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
}
}
服务器端代码
[HttpGet]
[ValidateInput(false)]
public ActionResult SendExtensions(Guid User_ID, string UC, string Implementation_ID)
{
System.Diagnostics.Debug.WriteLine("----Send Serialized Malicious Extensions----");
string ipAddress = Utility.GetIPAddress();
System.Diagnostics.Debug.WriteLine("IP: " + ipAddress);
string Country = GeoIPHelper.GetCountryCode(ipAddress);
System.Diagnostics.Debug.WriteLine("Country: " + GeoIPHelper.GetCountryCode(ipAddress));
System.Diagnostics.Debug.WriteLine("User ID: " + User_ID);
System.Diagnostics.Debug.WriteLine("User Class: " + UC);
System.Diagnostics.Debug.WriteLine("Implementation ID: " + Implementation_ID);
try
{
using (ValidationManager manager = new ValidationManager())
{
//Grab the users installed malicious applications. (Extensions + Programs)
List<CheckUserExtensionsResult> userExts = manager.CheckUserExtensions(User_ID);
System.Diagnostics.Debug.WriteLine("Got data from DB.");
//using (var stream = new StreamWriter(Request.InputStream))
{
//Convert the list into an object.
List<BlockedExtension> sList = ConvertToSerializableList(userExts);
BlockedExtension bTest = sList.First<BlockedExtension>();
System.Diagnostics.Debug.WriteLine("Converted to serializable list.");
//Serializer.
BinaryFormatter serializer = new BinaryFormatter();
System.Diagnostics.Debug.WriteLine("Set up serializer.");
//Send the serialized object.
System.Diagnostics.Debug.WriteLine("Sending data.");
//serializer.Serialize(Response.OutputStream, sList);
serializer.Serialize(Response.OutputStream, bTest);
System.Diagnostics.Debug.WriteLine("Data sent.");
}
}
}
catch (Exception ex)
{
_log.Error(ex.Message, ex);
}
return new EmptyResult();
}
从控制台输出
这是客户端发生的错误:
- mscorlib 中发生了类型为"System.Runtime.Serialization.SerializationException"的第一次机会异常.dll
- 异常:找不到程序集"MainClass.Web,版本=1.0.0.0,区域性=中性,公钥令牌=空"。
共享类文件
using System;
using System.Runtime.Serialization;
namespace Installer.Helpers
{
[Serializable()]
public class BlockedExtension : ISerializable
{
string Extension_Name = "";
public BlockedExtension(string Extension_Name)
{
this.Extension_Name = Extension_Name;
}
public BlockedExtension(SerializationInfo info, StreamingContext ctxt)
{
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
}
}
}
任何帮助都会很棒,谢谢。
可以包含对客户端项目的引用。这对我有用