我想在我的C#應用程序中實現REST服務以與Android客戶端進行通信。我與C#部分鬥爭。REST服務返回對象異常終止
我跟着這篇文章:http://msdn.microsoft.com/en-us/library/bb412178
這是我跑我的服務:
WebServiceHost host = new WebServiceHost(typeof(RestService), new Uri("http://localhost:8000/"));
ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IRestService), new WebHttpBinding(), "");
host.Open();
非常喜歡的文章上面我掛有IRestService接口和RestService與方法EchoWithGet(字符串s)和EchoWithPost我不使用,我不會再談論它。我想返回UserClientInfo類,這是我將在稍後描述的問題的原因。
[ServiceContract]
interface IRestService
{
[OperationContract]
[WebGet]
UserClientInfo EchoWithGet(string s);
}
這裏是EchoWithGet(...)的實現。它連接到另一個服務並調用UserLog,該用戶日誌應該返回UserClientInfo。
public UserClientInfo EchoWithGet(string s)
{
service = (IService)Activator.GetObject(typeof(IService), "tcp://127.0.0.1:7878/" + "Service");
service.UserLog("username", "password", out uci);
return uci;
}
UserClientInfo是相當複雜的對象。我不確定我是否可以通過服務返還。它是包含另一個結構的結構,其中包含另一個結構。
[Serializable]
public struct UserClientInfo
{
public ulong ClientId;
public DatabaseManager DBManager;
public string DisplayedName;
public ChISDispositions Dispositions;
public RegistrationInfo RegistrationInfo;
public uint Right1;
public uint Right2;
public uint Right3;
public uint Right4;
public string SessionId;
public string UserId;
}
當我跑我的服務,並指出我的瀏覽器的http://本地主機:8000/EchoWithGet S =的TestString我得到連接所中斷的錯誤。 Firebug顯示狀態在網絡選項卡上中止。服務以某種方式崩潰。我可以調試它,並看到UserLog()重新調用uci對象,服務返回它,但在瀏覽器和服務之間的某處失敗。
我試圖返回完全地新的對象:
UserClientInfo uci = new UserClientInfo();
這一工程,並與普通的對象返回的XML。
有什麼我做錯了嗎?
我應該知道的服務有一些限制嗎?
是否有任何日誌,我可以看到什麼是錯的?
感謝您的任何答案。
檢查以確保您的UserClientInfo數據結構中沒有循環依賴項。如果你這樣做,它會在序列化過程中窒息。這可能是爲什麼一個新的UserClientInfo會通過但實際數據填充一個。 – 2012-04-03 19:09:19
您的問題很可能與以下三種類型之一有關:DatabaseManager,ChISDispositions,RegistrationInfo。 DatabaseManager是一個類嗎?如果是這樣,你有沒有嘗試將其設置爲空? – JamieSee 2012-04-03 22:07:01
正如你所說 - 問題在ChiSDispositions。如果我將它設置爲空,我會得到輸出結果。 ChiSDispositions中有一些方法和常量。我還發現有引用類型的枚舉。這可能是原因嗎?然而,我在那裏發現的一切都是可序列化 – 2012-04-05 12:00:09