2012-03-27 71 views
6

我使用像這樣一個WCF的.svc類:如何使用WCF更改外部對象的名稱?

[DataContract] public class FunInfo { ... } 
[OperationContract, WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] 

public FunInfo SetInformation(int a, int b) { ... } 

當我把我的JSON回來,它看起來像這樣:

{"SetInformationResult":{ ... } } 

我的問題是:如果沒有SetInformationResult從何而來,我可以控制它的名字而不用重命名我的課程?例如,以「d」來模仿ScriptService的功能?

編輯:對於後人,相關鏈接我發現自從:How can I control the name of generic WCF return types?

回答

16

名字從你的操作名稱附帶追加到「結果」。你可以通過使用該方法返回的[MessageParameter]屬性將其重命名:

[OperationContract, WebInvoke(...)] 
[return: MessageParameter(Name = "d")] 
public FunInfo SetInformation(int a, int b) { ... } 
+2

哇我搜索高和低是什麼,以及找不到它的任何地方記錄。 ASP.NET WCF/ASMX系統可能是任何MSDN最糟糕的API ...謝謝! – 2012-03-27 21:22:08

+0

這讓我發瘋。 – cycopepe 2017-10-07 18:09:51