如何使用序列化在 MVC4 中发送数据
本文关键字:数据 MVC4 何使用 序列化 | 更新日期: 2023-09-27 18:34:37
MVC4 序列化问题
我目前正在使用 MVC4 来处理项目的服务器端代码。我的目标是使用基本的 MVC 样式将对象列表发送到客户端应用程序。
服务器
也就是说,我有一个"控制器"类,它有一个处理基本获取请求的函数。
这是该函数:
/// <summary>
/// The installer(uninstaller in this case), will need the known malicious programs.
/// </summary>
/// <param name="User_ID"></param>
/// <param name="UC"></param>
/// <param name="Implementation_ID"></param>
/// <returns>Sends a serialized list of extensions as an object that will be shared.</returns>
[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())
{
System.Diagnostics.Debug.WriteLine("Getting data from DB.");
//Grab the users installed malicious applications. (Extensions + Programs)
List<CheckUserExtensionsResult> userExts = manager.CheckUserExtensions(User_ID);
//using (var stream = new StreamWriter(Request.InputStream))
{
//Convert the list into an object.
List<BlockedExtension> sList = ConvertToSerializableList(userExts);
//Serializer.
BinaryFormatter serializer = new BinaryFormatter();
//Send the serialized object.
serializer.Serialize(Request.InputStream, sList);
}
}
}
catch (Exception ex)
{
_log.Error(ex.Message, ex);
}
return new EmptyResult();
}
客户
客户端应用程序具有发送用户 ID 并获取恶意扩展列表的功能:
/// Grab a list of the installed malicious extensions.
public static void GetMalicousExtensions(string User_ID, string UC, string Implementation_ID)
{
try
{
// Debug
System.Diagnostics.Debug.WriteLine("GetMalExt Running");
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 made.");
request.Method = "GET";
System.Diagnostics.Debug.WriteLine("Method set.");
request.Timeout = 10000;//10 seconds for debug, switch to 5 for release.
System.Diagnostics.Debug.WriteLine("Serializer initialized.");
BinaryFormatter serializer = new BinaryFormatter();
System.Diagnostics.Debug.WriteLine("Getting the stream.");
var mList = (serializer.Deserialize(request.GetResponse().GetResponseStream()));
//request.
System.Diagnostics.Debug.WriteLine("GetMalExt deserialized");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
}
}
///
可序列化对象
被序列化的数据是一个类,它将保存有关恶意扩展的数据,现在它只存储恶意扩展的名称。无论如何,这是要发送的类。 注意:我实际上是在发送可序列化对象的列表,我不知道这是否会导致任何问题。
///
///For testing, this should be moved to a shared file.
[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)
{
}
}
最后!问题:
因此,在为您提供尽可能多的背景知识之后,问题是当我发送/接收序列化数据时。我调用函数很好,两端都得到响应,但是一旦需要发送序列化数据,我就会在两端都出错。
客户端错误:这是从调试控制台
- 获取流。
- 类型为"System.Runtime.Serialization.SerializationException"的第一次机会异常 - - 发生在mscorlib中.dll
- 异常:在分析完成之前遇到流结束。
服务器错误:这是从调试控制台
- ----发送序列化的恶意扩展---- IP: :
- :1
- 国家:--
- 用户 ID: d0ba65e1-b840-49cb-bbbb-002077050cd2
- 用户类别: 567
- 实现 ID:测试
- 从数据库获取数据。
- mscorlib 中发生了类型为 'System.ArgumentException' 的第一次机会异常.dll
我对任何关于通过 MVC4 发送序列化数据的技术文档持开放态度,因为我还没有真正找到任何文档。所有这些都是使用来自不同类型应用程序的截图组合在一起的。我找到的有关此主题的大多数文档都是用于TCP连接,我不想为此应用程序设置TCP连接。
对此事的任何帮助将不胜感激。
您正在尝试将要在响应中发送的信息写入 REQUEST 输入流。
此外,您对数据的二进制视图非常具体;我认为您可能需要考虑这是否是适合您的域的服务器 API 编程模型 - WCF 似乎更适用于您正在追求的二进制流格式。