Restsharp不会反序列化枚举

时间:2018-02-16 01:05:03

标签: c# api generics deserialization restsharp

我试图在我的应用程序中调用外部RESTful API,并且我一直在使用restsharp,它迄今为止一直很好,但我认为我遇到了问题。< / p>

我正在调用一个通常会返回XML文档的API:

<res>
    <resultCode>100</resultCode>
    <resultText>OK</resultText>
    <messageId>52788198</messageId>
    <sessionId>TEST</sessionId>
    <operatorCode>22802</operatorCode>
</res>

这是我的代码:

internal class SubmitMessageResponse
{
    public ResultCode resultCode { get; set; }
    public string resultText { get; set; }
    public string messageId; { get; set; }
    public string sessionId; { get; set; }
    public string operatorCode; { get; set; }
}

public SubmitMessageResponse SendWelcomeSMS(string subscriptionId, string msisdn)
{
    var request = new RestRequest(Method.GET);
    request.AddQueryParameter("command", "submitMessage")
            .AddQueryParameter("username", _Config.User)
            .AddQueryParameter("password", _Config.Password)
            .AddQueryParameter("msisdn", "00" + _Club.CountryCode + msisdn)
            .AddQueryParameter("businessNumber", _Club.SubscribeShortCode)
            .AddQueryParameter("content", _Club.WelcomeMessage)
            .AddQueryParameter("price", "0")
            .AddQueryParameter("sessionId", subscriptionId)
            .AddQueryParameter("label", "W")
            .AddQueryParameter("keyword", _Club.SubscribeKeyword)
            .AddQueryParameter("operatorCode", "test");

    return SendGetRequest<SubmitMessageResponse>(request);
}

private T SendGetRequest<T>(RestRequest request) where T : new() 
{
    try
    {
        var client = new RestClient(this._BaseUrl);
        client.Timeout = 10000;
        _Logger.DebugFormat("{0} Request: {1}", "submitMessage", client.BuildUri(request));
        var data = client.Execute<T>(request);
        _Logger.DebugFormat("{0} Response: {1}", "submitMessage", Newtonsoft.Json.JsonConvert.SerializeObject(data.Content));
        return data.Data;
    } catch(Exception e)
    {
        _Logger.Error(e);
    }
    return default(T);
}

其中ResultCode是一个枚举,包括此API返回的所有已知值:

internal enum ResultCode
{
    Ok = 100,
    AuthorizationInProgress = 150,
    PaymentAuthInProcess = 150,
    InvalidOperatorCode = 201,
...
}

问题是通常这会正常工作,但我无法从XML反序列化枚举:

var response = apiHelper.SendWelcomeSMS(client.ExternalData, client.Number);            
Logger.Debug(Newtonsoft.Json.JsonConvert.SerializeObject(response));

我的日志:

{"messageId":"52788292","sessionId":"TEST","operatorCode":"22802","**resultCode**":0,"resultText":"OK"}

但我也记录了响应,它运行正常:

submitMessage Response: "<res>\r\n\t<**resultCode**>**100**</**resultCode**>\r\n\t<resultText>OK</resultText>\r\n\t<messageId>52788292</messageId>\r\n\t<sessionId>TEST</sessionId>\r\n\t<operatorCode>22802</operatorCode>\r\n</res>\r\n"

感谢任何建议。

0 个答案:

没有答案