[英]How can I send data in MVC4 using Serialization?
I am currently using MVC4 to handle the server side code for a project. 我目前正在使用MVC4处理项目的服务器端代码。 I aim to send a list of objects to a client application using the basic MVC style.
我的目标是使用基本的MVC样式将对象列表发送到客户端应用程序。
Which is to say, I have a "Controller" class with a function that handles basic get requests. 也就是说,我有一个“ Controller”类,该类具有处理基本get请求的功能。
Here is that function: 这是该函数:
/// <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();
}
The client application has a function to send over the user id and get back a list of malicious extensions: 客户端应用程序具有发送用户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);
}
}
///
The data being serialized is a class that will hold data about malicious extensions, right now it only stores the name of malicious extensions. 序列化的数据是一个类,其中包含有关恶意扩展的数据,现在它仅存储恶意扩展的名称。 None-the-less, here is the class being sent.
尽管如此,这是正在发送的课程。 NOTE: I am actually sending a list of the serializable objects, I don't know if that might be causing any problems.
注意:我实际上是在发送可序列化对象的列表,我不知道这是否可能引起任何问题。
///
///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)
{
}
}
So after giving you as much background as I can, the problem is when I go to send/receive the serialized data. 因此,在给了我尽可能多的背景知识之后,问题就出在我去发送/接收序列化数据时。 I call the functions just fine and get responses on both ends, but I get errors on both sides once it comes time to send the serialized data.
我称这些函数很好,并且在两端都得到响应,但是一旦到了发送序列化数据的时候,我都会在双方上出错。
I am open to any technical documents on the subject of sending serialized data through MVC4, as I haven't really found any. 对于通过MVC4发送序列化数据的问题,我持开放态度,因为我还没有发现任何技术文档。 All of this has been put together using snippits from different types of applications.
所有这些都已使用来自不同类型应用程序的代码片段汇总在一起。 Most of the documentation I found on this subject was for use with a TCP connection, which I don't want to set up for this application.
我在此主题上找到的大多数文档都是与TCP连接一起使用的,我不想为此应用程序进行设置。
Any help on the matter would be most appreciated. 对此事的任何帮助将不胜感激。
You are attempting to write into the REQUEST input stream with information you want to send in the RESPONSE . 您正在尝试使用要在RESPONSE中发送的信息写入REQUEST输入流。
Also, your binary view of the data is pretty specific; 同样,您的数据二进制视图非常具体。 I think you might want to consider whether this is the appropriate server API programming model for your domain - WCF seems more applicable to the binary stream format you're pursuing.
我认为您可能要考虑这是否是适合您的域的服务器API编程模型-WCF似乎更适用于您追求的二进制流格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.