2011-09-30 70 views
2

我正在开发一个RESTful WCF删除内部“__type”的标签,而我目前遇到了麻烦漂亮,干净的JSON序列化的返回值。在ASP.NET宁静的WCF

A little while back, I got rid of "Key" and "Value" tags围绕我的每个键和值(通过序列化一个对象Dictionary<string, object>)通过使用包装。

但是,当我这样做,“__type”标签开始出现。我设法通过用我的svc文件的ServiceHost标记中的WebServiceHostFactory替换WebScriptServiceHostFactory来消除“d”标记以及第一个“__type”标记。

所以我的结果是这样的:

{"Status":"Success","Data":{"__type":"SerializableDictionaryOfstringanyType:#MyNamespace.MyFolder","Key1":"Value1","Key2":"Value2","Key3":"Value3"}} 

但我希望它看起来像这样:

{"Status":"Success","Data":{"Key1":"Value1","Key2":"Value2","Key3":"Value3"}} 

我的测试Web服务代码如下所示:

[OperationContract] 
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
public SerializableDictionary<string, object> Test1(String Token, String Id) 
{ 
    DataTable testTable = new DataTable(); 
    testTable.Columns.Add("Key1", typeof(System.String)); 
    testTable.Columns.Add("Key2", typeof(System.String)); 
    testTable.Columns.Add("Key3", typeof(System.String)); 

    DataRow testRow = testTable.NewRow(); 
    testRow["Key1"] = "Value1"; 
    testRow["Key2"] = "Value2"; 
    testRow["Key3"] = "Value3"; 

    testTable.Rows.Add(testRow); 

    return SuccessfulResult(testTable); 
} 

编辑:而SuccessfulResult功能看起来像这样(对不起忘记吧):

private SerializableDictionary<string, object> SuccessfulResult(DataTable dt = null) 
{ 
    SerializableDictionary<string, object> result = new SerializableDictionary<string, object>(); 
    result.Add("Status", "Success"); 

    if (dt == null || dt.Rows.Count != 1) 
     return result; 

    SerializableDictionary<string, object> dct = new SerializableDictionary<string, object>(); 
    foreach (DataColumn currCol in dt.Rows[0].Table.Columns) 
     dct.Add(currCol.ColumnName, dt.Rows[0][currCol.ColumnName].ToString()); 

    result.Add("Data", dct); 

    return result; 
} 

如果有人对我怎么可能摆脱过去的小“__type”标签的任何想法,我很愿意听到他们的声音!非常感谢,并让我知道是否还有其他我可以发布的内容可能会有所帮助。

回答

1

好的,把它修好了 - 但最终做的事情有点不同。 WCF似乎坚持在序列化字典时加入这些内部的“__type”标签,但出于某种原因,它不会对Streams做同样的事情。

这里是新的Web服务方法代码(只是返回类型发生了变化):

[OperationContract] 
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
public Stream Test1(String Token, String StreamId) 
{ 
    DataTable testTable = new DataTable(); 
    testTable.Columns.Add("Key1", typeof(System.String)); 
    testTable.Columns.Add("Key2", typeof(System.String)); 
    testTable.Columns.Add("Key3", typeof(System.String)); 

    DataRow testRow = testTable.NewRow(); 
    testRow["Key1"] = "Value1"; 
    testRow["Key2"] = "Value2"; 
    testRow["Key3"] = "Value3"; 

    testTable.Rows.Add(testRow); 

    return SuccessfulResult(testTable); 
} 

而这里的新SuccessfulResult功能(这是什么做的差):

private Stream SuccessfulResult(DataTable dt = null) 
{ 
    Dictionary<string, object> returnDict = new Dictionary<string, object>(); 
    returnDict.Add("Status", "Success"); 

    Dictionary<string,object> dct = new Dictionary<string,object>(); 
    foreach (DataColumn currCol in dt.Rows[0].Table.Columns) 
     dct.Add(currCol.ColumnName, dt.Rows[0][currCol.ColumnName].ToString()); 

    returnDict.Add("Data", dct); 

    string sResponse = json.Serialize(returnDict); 
    byte[] byResponse = Encoding.UTF8.GetBytes(sResponse); 

    return new MemoryStream(byResponse); 
} 

现在输出看起来正是我希望它看起来:

{"Status":"Success","Data":{"Key1":"Value1","Key2":"Value2","Key3":"Value3"}} 

无论如何,我希望这个例子可以帮助别人:)

+1

使用<webHttp/>这是一个不错的黑客,但一定是这样做的更多的“内置”的方式。 : -/ – BrainSlugs83

0

在某些情况下,浏览器将把响应流为二进制,所以,它会显示下载文件弹出。 那么你应该使用:

string result = "Hello world"; 
byte[] resultBytes = Encoding.UTF8.GetBytes(result); 
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"; 
return new MemoryStream(resultBytes); 
0

我知道,这是很久以前的事,但我发现了一个非常简单的方法来做到这一点。

在Web.config文件中,你应该换行。而不是<enableWebScript/>

<endpointBehaviors> 
     <behavior> 
      <webHttp /> 
     </behavior> 
+0

正好相反,你应该使用。 –