Skip to main content
 首页 » 编程设计

c#之从 C# WCF 服务返回具有动态维数的锯齿状数组

2024年02月23日31qq78292959

我目前正在尝试创建一个服务,该服务将在 C#/WCF 数据服务中返回 OLAP 多维数据集查询的结果。我这样做是为了获得对如何序列化 OLAP 结果、如何对客户端进行身份验证/授权以及能够直接从网站中的 javascript 查询多维数据集的完全编程控制。

OLAP 查询的结果可能有任意数量的维度(实际上介于 1 和 5 之间)。我遇到的问题是,如果不对我可能使用的每个维度的处理进行硬编码,我无法弄清楚如何首先创建一个动态维度的锯齿状数组。所以第一个问题是:是否有一种优雅的方法可以在 C# 中创建一个动态维数的锯齿状数组?

一旦我有了这个动态维数数组,是否可以使用 DataContractJsonSerializer(或任何其他免费提供的 json 序列化程序)将其序列化为 json。目标是将其序列化为一个对象,对于二维结果看起来像这样:

{ 
  "DimensionMemberCaptions" = [["Dim1 member1", "Dim2 member2"], ["Dim2 member1"], ["Dim2 member2"]], 
  "Data" = [[1, 2], 
            [3, 4]], 
  "FormatedData = [["1$", "2$"], 
                   ["3$", "4$"]] 
} 

其中 DimensionMemberCaptions 包含每个维度的标题(OLAP 成员名称),data/formateddata 是结果表。

我想避免编写自己的序列化函数,但随着时间的推移它看起来更有吸引力——使用数组(多维数组而不是锯齿状数组)并编写我自己的 json 序列化程序,专门用于序列化 OLAP 输出到 WCF REST 方法返回的 Stream。

请您参考如下方法:

我找到了一种方法来解决我的问题,它比我指定的问题更具体,但我认为可以应用相同的技术来解决这个问题而不依赖于 Adomd。我选择使用 Newtonsoft 的 Json 序列化库 ( http://james.newtonking.com/projects/json-net.aspx )。有了这个,我可以创建自己的“JsonConverter”来序列化 Adomd CellSet(基本上是多维 OLAP 查询的结果)。无论维数如何,这都有效。

public class CellSetConverter : JsonConverter 
{ 
 
    public override bool CanRead 
    { 
        get 
        { 
            return false; 
        } 
    } 
 
    public override bool CanConvert(Type objectType) 
    { 
        if (objectType == typeof(CellSet)) 
        { 
            return true; 
        } 
        else 
        { 
            return false; 
        } 
    } 
 
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
        throw new NotImplementedException(); 
    } 
 
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
        CellSet cellSet = (CellSet)value; 
        int cellCount = cellSet.Cells.Count; 
        int[] axisCounts = new int[cellSet.Axes.Count]; 
        int[] currentIndex = new int[cellSet.Axes.Count]; 
        for (int i = 0; i < axisCounts.Length; i++) 
        { 
            axisCounts[i] = cellSet.Axes[i].Positions.Count; 
        } 
 
        for (int i = 0; i < cellSet.Axes.Count; i++) 
        { 
            writer.WriteStartArray(); 
        } 
 
        for (int i = 0; i < cellCount; i++) 
        { 
            serializer.Serialize(writer, cellSet[currentIndex].Value); 
            currentIndex = IncrementIndex(writer, currentIndex, axisCounts); 
        } 
    } 
 
    string[] GetCaptions(CellSet cellSet, int[] index) 
    { 
        string[] captions = new string[index.Length]; 
        for (int i = 0; i < index.Length; i++) 
        { 
            Axis axis = cellSet.Axes[i]; 
            captions[i] = axis.Positions[index[i]].Members[0].Caption; 
        } 
 
        return captions; 
    } 
 
    int[] IncrementIndex(JsonWriter writer, int[] index, int[] maxSizes) 
    { 
        bool incremented = false; 
        int currentAxis = 0; 
        while (!incremented) 
        { 
            if (index[currentAxis] + 1 == maxSizes[currentAxis]) 
            { 
                writer.WriteEndArray(); 
                index[currentAxis] = 0; 
                currentAxis++; 
            } 
            else 
            { 
                for (int i = 0; i < currentAxis; i++) 
                { 
                    writer.WriteStartArray(); 
                } 
 
                index[currentAxis]++; 
                incremented = true; 
            } 
 
            if (currentAxis == index.Length) 
            { 
                return null; 
            } 
        } 
 
        return index; 
    } 
} 

以及与之配套的 WCF Rest 服务:

[ServiceContract] 
public class MdxService 
{ 
    const string JsonMimeType = "application/json"; 
    const string connectionString = "[Data Source String]"; 
    const string mdx = "[MDX query]"; 
 
    [OperationContract] 
    [WebGet(UriTemplate = "OlapResults?session={session}&sequence={sequence}")] 
    public Stream GetResults(string session, string sequence) 
    { 
        CellSet cellSet; 
        using (AdomdConnection connection = new AdomdConnection(connectionString)) 
        { 
            connection.Open(); 
            AdomdCommand command = connection.CreateCommand(); 
            command.CommandText = mdx; 
            cellSet = command.ExecuteCellSet(); 
        } 
 
        string result = JsonConvert.SerializeObject(cellSet, new CellSetConverter()); 
        WebOperationContext.Current.OutgoingResponse.ContentType = JsonMimeType; 
        Encoding encoding = Encoding.UTF8; 
        byte[] bytes = encoding.GetBytes(result); 
        return new MemoryStream(bytes); 
    } 
}