简体   繁体   English

如何使用序列化在MVC4中发送数据?

[英]How can I send data in MVC4 using Serialization?

MVC4 Serialization Problems MVC4序列化问题

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样式将对象列表发送到客户端应用程序。

The Server 服务器

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 客户端

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 Serializable Object 可序列化的对象

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)
    {

    }
}

Finally! 最后! The Question: 问题:

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. 我称这些函数很好,并且在两端都得到响应,但是一旦到了发送序列化数据的时候,我都会在双方上出错。

The Clients Error : This is from the debug console 客户端错误:这是从调试控制台

  • Getting the stream. 获取流。
  • A first chance exception of type 'System.Runtime.Serialization.SerializationException' - - occurred in mscorlib.dll mscorlib.dll中发生类型'System.Runtime.Serialization.SerializationException'的第一次机会异常-
  • Exception: End of Stream encountered before parsing was completed. 例外:在解析完成之前遇到流的结尾。

The Servers Error : This is from the debug console 服务器错误:这是来自调试控制台

  • ----Send Serialized Malicious Extensions---- ----发送序列化的恶意扩展程序----
  • IP: ::1 IP::: 1
  • Country: -- 国家/地区:-
  • User ID: d0ba65e1-b840-49cb-bbbb-002077050cd2 用户ID:d0ba65e1-b840-49cb-bbbb-002077050cd2
  • User Class: 567 用户等级:567
  • Implementation ID: test 实施编号:测试
  • Getting data from DB. 从数据库获取数据。
  • A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll mscorlib.dll中发生类型'System.ArgumentException'的第一次机会异常

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.

相关问题 我如何无法在MVC4中发送响应? - How can i not send a response in mvc4? 如何在不使用密码的情况下将电子邮件从MVC4发送到Gmail? - How Can i send the email from the MVC4 to my gmail without using my password? 如何使用MVC4在Controller中获得主机 - How can I get host in a Controller using MVC4 如何通过curl将数据发送到mvc4 Web服务 - How do I send a data via curl into an mvc4 web service 如何获取文本框或下拉列表的值并发送到ASP.NET MVC4中的数据库 - How can I get value of textbox or dropdownlist and send to database in ASP.NET MVC4 如何使用带有剃刀视图的实体框架(.edmx模型)为MVC4或MVC 5创建局部视图? - How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views? 在MVC4中使用WebApi,如何将动态json反序列化为具体类型? - Using WebApi in MVC4, how can I deserialize dynamic json to a concrete type? 如何在linq中将多重表包含在渴望使用mvc4 C#加载的实体中 - How can I Include Multiples Tables in my linq to entities eager loading using mvc4 C# 我如何使用 datapost 将参数从 jqgrid 传递到 controller(使用 MVC4 和 asp.net) - how can i pass parameter from jqgrid to controller with datapost (using MVC4 and asp.net ) 如何在MVC4中将部分视图发送到Ajax - How to send partial view to ajax in mvc4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM