我正在尝试使用c#
创建Wcf Rest服务我的想法是处理交易/用户/卡信息,因此我创建了以下结构:
[DataContract]
public class Result : Response
{
public List<IObject> data { get; set; }
}
[DataContract]
public class UserData : IObject
{
[DataMember]
public int id { get; set; }
//...
}
[DataContract]
public class CardData : IObject
{
[DataMember]
public int id { get; set; }
//...
}
[DataContract]
public class Error : Response
{
[DataMember]
public string errorID { get; set; }
[DataMember]
public string errorMessage { get; set; }
}
public abstract class Response
{
}
public abstract class IObject
{
}
public class Status : Response
{ public bool status { get; set; } }
这个想法是卡片/用户是IObjects,我的Result有一个列表。然后结果,状态和错误将出现在Reponse上。 我已经创建了处理get请求和响应的方法
public Dictionary<String, Response> GetUsers()
{
using (var db = new Context())
{
try
{
var query = from result in db.Users
select result;
List<IObject> r = query.Select(state => new UserData
{
id = state.AccountNumber,
//...
} as IObject
).ToList();
Result res = new Result { data = r };
Dictionary<String, Response> dic = new Dictionary<String, Response> { };
dic.Add("Error", new Error
{
errorID = "0",
errorMessage = "success"
});
dic.Add("Status", new Status { status = true });
dic.Add("response", res);
return teste;
}
catch (Exception e)
{
var ErrorMessage = e.Message.ToString();
throw;
}
}
}
虽然看起来有点乱,但我能够编译它。当我调用getUsers的get请求时,我收到以下错误:
&#34;类型&#39; System.Security.VerificationException&#39;的例外情况发生在RestService.dll中但未在用户代码中处理
附加信息:操作可能会破坏运行时的稳定性。&#34;
我想要的回应的意思是:
{"status": true,"error": { "errorID" 0,
"errorMessage": "success"},"result": "GetUsersResult": [ .... ]}
我知道这是非常草率的编码,所以我问你正确的处理方法。