none
.net framework下序列化dataset得到的二进制拿到.net core下不能被反序列化 RRS feed

  • 问题

  • 我在.net framework框架下序列化dataset,将得到的二进制流通过http 传给.net core webapi,webapi函数接受到的二进制流是正确的,但是不能被反序列化?

    .net framework下序列化代码如下:

     //把数据集序列化成二进制流
            public static byte[] DataSetToBytes(DataSet ds, ref string errorinfo)
            {
                try
                {
                    BinaryFormatter ser = new BinaryFormatter();
                    MemoryStream unCompressMS = new MemoryStream();
                    ds.RemotingFormat = SerializationFormat.Binary;
                    ser.Serialize(unCompressMS, ds);
                    byte[] bytes = unCompressMS.ToArray();
                    unCompressMS.Close();
                    return bytes;
                }
                catch (Exception ex)
                {
                    errorinfo = ex.ToString();
                    return null;
                }
            }

    .net core反序列化代码如下:

     //将二进制转换为DataSet
            private DataSet LocBytesToDataSet(byte[] datas, ref string errorinfo)
            {
                try
                {
                    MemoryStream ms = new MemoryStream(datas);
                    BinaryFormatter bf = new BinaryFormatter();
                    object obj = bf.Deserialize(ms);
                    DataSet ds = (DataSet)obj;
                    ms.Close();
                    return ds;
                }
                catch (Exception ex)
                {
                    errorinfo = ex.Message.ToString();
                    return null;
                }
            }

    以上代码在各自框架下都能正常序列化和反序列化

    我也试着将序列化代码写在.net standard库下,但结果还是一样

    请高手指点,谢谢。

    2018年11月11日 14:48

全部回复

  • 你好,

    能贴一下的你的发送和WebApi 接收的代码吗?

    或者直接传个dataset过去:

    #Client:

    DataSet ds = yourdsresulthere. 
    var client = new HttpClient();
    client.BaseAddress = new Uri("http://localhost:63879/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    var content = new StringContent(JsonConvert.SerializeObject(ds), Encoding.UTF8, "application/json");
    var response = client.PostAsync("/api/Values", content).Result;
    string s = await response.Content.ReadAsStringAsync();

    #WebAPI

     // POST api/values
     [HttpPost]
    public ActionResult<string> Post([FromBody] DataSet value)
    {
          return "OK";
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2018年11月12日 6:53
    版主
  • 你转换成json字符串传送了,我是直接传byte数组,在服务端接受到的内容和我发送的内容一样

    ,我逐个字节对比的,我客户端传送代码如下:

     HttpClient myHttpClient = new HttpClient();
                myHttpClient.DefaultRequestHeaders.Add("Method", "POST");
                myHttpClient.DefaultRequestHeaders.Add("ContentType", "application/octet-stream");
                //提交当前地址的webapi
                string url = "http://localhost:1851";
                myHttpClient.BaseAddress = new Uri(url);
                DataSet tempds = new DataSet();
                tempds.Merge(CurrDt);
                tempds.Tables[0].TableName = "temp";
                tempds.AcceptChanges();
                string errorinfo = "";
                byte[] datas = LocalDataSetToBytes(tempds, ref errorinfo);
                HttpContent content = new ByteArrayContent(datas);
                HttpResponseMessage response = myHttpClient.PostAsync("mes/api/mes/ReadData03", content).Result;

    2018年11月16日 2:54
  • 你好,

    我建议你可以改为Json 来做,我这边使用Json,是可以直接转为Dataset的。

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年11月27日 6:06
    版主