WCF Rest服务使用json传递object []

时间:2018-12-04 16:39:33

标签: wcf-rest-starter-kit

我有一个WCF休息服务,具有以下对象作为输入。

[DataContract]
public class ODMServiceParameters
{
    [DataMember]
    public string name { get; set; }

    [DataMember]
    public string Op { get; set; }

    [DataMember]
    public object[] param { get; set; }

    [DataMember]
    public string logname { get; set; }
}

客户端,我编写了以下代码以访问该服务。

ODMServiceParameters odmParameters = new ODMServiceParameters();
        odmParameters.name = name;
        odmParameters.Op = op;
        odmParameters.param = args;
        odmParameters.logname = _logName;
        HttpClient client = new HttpClient();
        //string requestURL = ConfigurationManager.AppSettings["ODMServiceURL"];
        string requestURL = "http://localhost:49634/ODMWCFRestService.svc/Call";
        string jsonString = JsonConvert.SerializeObject(odmParameters);
        //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        object obj = null;

        try
        {
            var request = new StringContent(jsonString, Encoding.UTF8, "application/json");
            var responseObject = client.PostAsync(requestURL, request).Result;
            if (responseObject.IsSuccessStatusCode)
            {
                var strinContent = responseObject.Content.ReadAsStringAsync().Result;
                byte[] bytes = JsonConvert.DeserializeObject<byte[]>(strinContent);

                obj = SerializeUtils.Deserialize(bytes);
            }
            return obj;

        }
        catch (Exception oex)
        {
            string s = oex.ToString();
            throw;
        }

在上面的odmParameters.param代码中,它是一种object []类型,如果我传递字符串或int表示它工作正常。但是对于此参数,我想分配一个对象,如下所示:

public class SyncLog
{
    public SyncLog()
    {

    }
    public SyncLog(Int32 syncRequestID, string _log, SyncLogStatusType _logType = SyncLogStatusType.Fail)
    {
        Log = _log;
        LogType = _logType;
        LoggedToDB = false;
        CreatedDateTime = DateTime.Now;
        SyncRequestID = syncRequestID;
    }
    private DateTime _createdDateTime;
    public DateTime CreatedDateTime
    {
        get
        {
            return _createdDateTime;
        }
        set
        {
            _createdDateTime = value;
            CreatedDateTimeStamp = value.ToString("MM/dd/yyyy HH:mm:ss");
        }
    }
    public string Log { get; set; }
    public string Status { get; set; }
    public SyncLogStatusType StatusType { get; set; }
    public string CreatedDateTimeStamp { get; set; }
    public bool LoggedToDB { get; set; }
    public Int32 SyncRequestID
    {
        get;
        set;
    }
    public SyncLogStatusType LogType { get; set; }
}

}

我正在将此Synclog对象传递给object [],这很好。但是,当我在服务器端发布输入时,对象不正确。我由于对象引用错误而失败,因为它无法获取synclog对象作为输入。有人可以帮忙吗?

0 个答案:

没有答案