验证 WCF 休息服务中 Json 对象的结构



我编写了一个WCF Rest Service,在POST方法中,Json对象作为StudentDetails接收。现在我想验证它的结构,它不应该包含比指定字段更多的字段/信息。
以下是我的服务(服务合同(

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; }
}
}

这是服务的实现

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"
}
}

由于存在额外项目,因此它应该失败(错误请求(。 而且

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

由于引入了额外的"新"项,因此它应该失败(错误请求(。

请帮助我。 谢谢

很遗憾,您无法使用 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
.
.
.

最新更新