c# - 从 WCF 服务返回不同的对象(列表或错误类)

标签 c# wcf datacontract

<分区>

我正在尝试构建具有单一方法的 WCF 服务 getPersonList返回这样的人列表

[
  {"personId":121, "personName":"John Smith"},
  {"personId":953, "personName":"Maggie Johnson"}
]

如果出现错误,我想从相同的方法返回这样的错误响应。

{"errorCode":4002,"errorMessage":"invalid request token"}

现在我的服务契约(Contract)如下:

    [ServiceContract()]
    public interface IPersonContract
    {
        [OperationContract()]
        Object GetPersonList(int requestParam);
    }

和我的样本 GetPersonList方法

Object GetPersonList(int requestParam)
{
  if (requestParam == 1)
  {
    ErrorResponse error = new ErrorResponse();
    error.ErrorCode = 4002;
    error.ErrorMessage = "invalid request token";

    return error;
  }else
  {
    List<Person> returnList = new List<Person>();
    // add Person to returnList
    return returnList;
  }

}

人类

[DataContract()]
public class Person
{

    [DataMember(Name = "personName")]
    String PersonName{   get;   set;  }

    [DataMember(Name = "personId")]
    String PersonID {   get;   set;  }

}

错误类

[DataContract()]
public class ErrorResponse
{

    [DataMember(Name = "errorCode")]
    int ErrorCode{   get;   set;  }

    [DataMember(Name = "errorMessage")]
    String ErrorMessage{   get;   set;  }

}

我查了一下KnownTypes对于 DataContract 类,但如何在 Object 上应用它.

如果我从 ErrorReponse 添加字段并添加 List<Person>在一个类中并返回该对象,我在成功案例中得到这样的响应,这不是我想要的。

{
"Person":[{"personId":121, "personName":"John Smith"},
      {"personId":953, "personName":"Maggie Johnson"}]
}

最佳答案

更改您的服务契约(Contract)定义,例如 -

    [OperationContract]
    [WebInvoke(Method = "GET",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "/get/{id}")]
    [ServiceKnownType(typeof(RootObject1))]
    [ServiceKnownType(typeof(RootObject2))]
    object GetOrder(string id);

服务实现-

    public object GetOrder(string id)
    {
        if (id.Length == 1)
        {
            return new RootObject1 { errorCode = 4002, errorMessage = "invalid request token" };
        }
        return new RootObject2 { personId = 121, personName = "John Smith" };
    }

enter image description here

enter image description here


列表类型更新于 12/27/2016

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, Method = "GET", UriTemplate = "GetOrdersJSON?ClientID={ClientID}&SenderEmail={SenderEmail}&VersionNumber={VersionNumber}")]
    [ServiceKnownType(typeof(List<MyCustomErrorDetail>))]
    [ServiceKnownType(typeof(List<ShipToDetails>))]
    object GetOrdersJSON(int ClientID, string SenderEmail, string VersionNumber);


[DataContract]
public class MyCustomErrorDetail
{
    public MyCustomErrorDetail(string errorInfo, string errorDetails)
    {
        ErrorInfo = errorInfo;
        ErrorDetails = errorDetails;
    }

    [DataMember]
    public string ErrorInfo { get; private set; }

    [DataMember]
    public string ErrorDetails { get; private set; }
}

根据您的需要,当没有记录或发生任何其他错误时,从 GetOrdersJSON 返回以下对象!

                MyCustomErrorDetail myCustomErrorObject = new MyCustomErrorDetail("There are no records available", string.Format("There are no records available for user {0}", fstr_UserName));
                List<MyCustomErrorDetail> myCustomErrorList = new List<MyCustomErrorDetail>();
                myCustomErrorList.Add(myCustomErrorObject);
                return myCustomErrorList;

关于c# - 从 WCF 服务返回不同的对象(列表或错误类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36771247/

相关文章:

.net - 与 SerializableAttribute 相比,使用 DataContractAttribute 有什么优势?

wcf - 为什么不推荐使用 [DataMember(EmitDefaultValue = false)]?

C# Json.Net 序列化具有虚拟属性的实体

wcf - Windows Azure 上的 Web 服务?

c# - 计算条目数?

c# - 系统.InvalidOperationException : 'There is an error in XML document (0, 0).'

c# - 是否有一个 C# 类用于跟踪服务方法调用,如堆栈跟踪

WCF - 在 DataContract 构造函数中实例化一个对象

c# - 如何在 C# 中使用 jquery ajax?

c# - 将 Json 文档反序列化为 C# 对象