在WCF Rest Service中验证Json对象的结构

时间:2018-09-29 09:58:38

标签: json rest validation wcf post

我写了一个WCF Rest Service,其中在POST方法中Json Object作为StudentDetails接收。现在,我要验证其结构,使其不应该包含比指定字段更多的字段/信息。
以下是我的服务(ServiceContract)

namespace RestService
{

[ServiceContract]
public interface IRestService
{
[OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/SaveStudent", RequestFormat =WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    string SaveStudent(StudentDetails studentDetails);
}

[Serializable]
public class WebServiceDictionary : ISerializable
{
    public Dictionary<string, string> Entries { get; }

    public WebServiceDictionary()
    {
        Entries = new Dictionary<string, string>();
    }
    public WebServiceDictionary(SerializationInfo info, StreamingContext context)
    {
        Entries = new Dictionary<string, string>();
        foreach (var entry in info)
            Entries.Add(entry.Name, entry.Value.ToString());
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        foreach (var entry in Entries)
            info.AddValue(entry.Key, entry.Value);
    }
}

[DataContract]
public class StudentDetails
{

    [DataMember]
    public WebServiceDictionary StudentDetail { get; set; }
}
}

这是Service的实现

namespace RestService
{

public class RestService : IRestService
{

    public string SaveStudent(StudentDetails studentDetails)
    {
        SqlHelper sql = new SqlHelper();
        sql.OpenConnection();

        WebServiceDictionary student = studentDetails.StudentDetail;



        if (student.Entries.Keys.Count != 3)
        {
            WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
            return null;
        }
        sql.SaveJson(student.Entries["Name"], student.Entries["Address"], int.Parse(student.Entries["Grade"]));
        sql.CloseConnection();

        return "Passed";
    }
}
}

因此,Json的结构应为

{
"StudentDetail" : 
{
    "Name" : "ABC", "Address" : "ABC", "Grade":"10"
}
}

我已经检查了是否在缺少某些字段且工作正常的情况下不接受请求。现在,我希望当Json数据包含一个或多个额外信息时,请求应该失败(BadRequest)。例如,如果Json对象是:

{
"StudentDetail" : 
{
    "Name" : "ABC", "Address" : "ABC", "Grade":"10", "Extra" : "Item"
}
}

因为存在Extra项目,所以它应该失败(BadRequest)。 还有

{
"StudentDetail" : 
{
    "Name" : "ABC", "Address" : "ABC", "Grade":"10"
},
"New" : { "key" : "value" }

}

由于引入了额外的“新”项目,因此该项目将失败(BadRequest)。

请帮助我。 谢谢

1 个答案:

答案 0 :(得分:0)

不幸的是,您无法使用wcf做到这一点。在Web api中,您仅可以使用动态对象并验证输入。但是在wcf中,您必须使用这样的对象:

[Serializable]
public class WebServiceDictionary : ISerializable
{
    public Dictionary<string, string> Entries { get; }

    public WebServiceDictionary()
    {
        Entries = new Dictionary<string, string>();
    }
    public WebServiceDictionary(SerializationInfo info, StreamingContext context)
    {
        Entries = new Dictionary<string, string>();
        foreach (var entry in info)
            Entries.Add(entry.Name, entry.Value.ToString());
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        foreach (var entry in Entries)
            info.AddValue(entry.Key, entry.Value);
    }
}

然后您的输入将如下所示:

[DataContract]
public class StudentDetails
{

    [DataMember]
    public WebServiceDictionar StudentDetail { get; set; }
}
}

现在您可以像这样访问StudentDetail

StudentDetail.Entries[Name]

,然后进行验证。 首先,您可以像这样获得字典中的所有键:

var keys=Entries.Keys;

然后您可以像这样验证它们:

if(keys.length!=3)
//its not valid
if(!keys.contain("Address"))
//its not valid
.
.
.