我在使用ServiceStack.Text(來自Nuget.org)將數據集序列化爲json時遇到了麻煩。我正在使用最新的穩定版本4.0.50和VS 2015。我不斷收到ServiceStack.Text:將DataSet序列化爲json
過程被終止由於StackOverflowException
我的代碼:
using System;
using System.Data;
using System.Data.SqlClient;
using ServiceStack.Text;
namespace TestServiceStackText
{
class Program
{
static void Main(string[] args)
{
string ConnectionString = @"Server=MyServer; Database=NORTHWND; User Id=SomeUser; Password=SomePassword;";
string SqlQuery = @"SELECT TOP 1 * FROM [NORTHWND].[dbo].[Customers]";
// Create new dataset instance
DataSet dataset = new DataSet();
// Fill it with a little data: 1 table, 1 row
using (var conn = new SqlConnection())
{
using (var da = new SqlDataAdapter())
{
using (da.SelectCommand = conn.CreateCommand())
{
da.SelectCommand.CommandText = SqlQuery;
da.SelectCommand.Connection.ConnectionString = ConnectionString;
da.Fill(dataset);
}
}
}
// Serialize to json: exception occurs here
string json = TypeSerializer.SerializeToString<DataSet>(dataset);
Console.WriteLine("Dataset serialized to json:\n {0}", json);
// Deserialize to DataSet
DataSet ds = TypeSerializer.DeserializeFromString<DataSet>(json);
Console.WriteLine("Name: {0}, Nr. of Tables: {1}", ds.DataSetName, ds.Tables.Count);
}
}
}
建議任何人?
我懷疑自己是在'DataSet'循環依賴和servicestack會繼續努力,即使它已經連載之前他們連載所有的屬性。 – sQuir3l