从WCF REST通用请求获取类型特定的响应? [英] Getting A Type-Specific Response From A WCF REST Generic Request?
问题描述
我正在设计一个WCF REST服务。对设计的要求是
客户不知道给定请求的细节。例如,
以下请求:
https://www.domain.com/dashboard/group/id/0
返回:
请求:GetGroup(GroupId = 0)
响应:
{
Title =Country,
children =
{
title =USA,Id = 1,type = GROUP},
{title =England,Id = 2,type = GROUP}
}
}
以下请求:
https://www.domain.com/dashboard/group/id/ 3
返回:
请求:GetGroup (groupId = 3)
pre>
回应:
{
Title =Customers,
children =
{
title =通用汽车,编号= 1,type = CUSTOMER},
{title =General Electric,Id = 2,type = CUSTOMER}
}
}
我的问题是我该如何使用通用REST寻求并返回一个类型特定的响应?
在我的项目中,有几个类型将在JSON响应中被序列化。序列化的对象取决于传入的groupId参数。它们是:
GROUP
CUSTOMER
FACILITY
TANK
在一篇相关的文章中,有人建议我创建一个公开GetGroupById
的基类,上面的类应该覆盖基类方法。如果这听起来像是一个很好的
例子来说明如何解决这个问题,那么我会欣赏一个例子。或者,其他建议。
预先感谢。
解决方案您始终可以创建一个返回Stream的服务并使用
JsonSerializer
将对象序列化为MemoryStream
,然后从服务中返回MemoryStream
:public Stream GetSomeObject(int groupId)
{
byte [] bytes;
var serializer = new JavaScriptSerializer();
switch(groupId)
{
case 2:
var groups = GetGroups(); //填充组但是
bytes = Encoding.UTF8.GetBytes(serializer.Serialize(groups));
休息;
案例3:
var customers = GetCustomers();
bytes = Encoding.UTF8.GetBytes(serializer.Serialize(customers));
休息;
}
返回新的MemoryStream(字节);
$ b在这种情况下,你可以简单地将适当的对象加载到内存中参数并通过
Stream
返回相应的强类型对象。 这与我用过的方法相同过去,如果没有类型信息,WCF服务返回Json结果(这种方法是由微软WCF团队成员提出的,所以我认为它相当可靠)。I am designing a WCF REST service. A requirement for the design is that the client is unaware of the particulars of a given request. For example, the following request:
https://www.domain.com/dashboard/group/id/0
Would return:
Request: GetGroup(GroupId = 0) Response: { Title="Country", children = { title="USA", Id=1, type=GROUP}, {title="England", Id=2, type=GROUP} } }
And the following request:
https://www.domain.com/dashboard/group/id/3
Would return:
Request: GetGroup(groupId = 3) Response: { Title="Customers", children = { title="General Motors", Id=1, type=CUSTOMER}, {title="General Electric", Id=2, type=CUSTOMER} } }
MY QUESTION IS how do I take a generic REST request and return a type-specific response?
In my project, there are a few Types that will be serialized in the JSON response. The serialized object depends on the passed-in groupId parameter. They are:
GROUP CUSTOMER FACILITY TANK
In a related post, it was suggested that I create a base class that exposes GetGroupById and the above classes should override the base class method. If this sounds like a good example of how to attack this problem, I'd appreciate an example. Or, alternatively, other suggestions.
Thanks in advance.
解决方案You could always create a service that returns a Stream and use the
JsonSerializer
to serialize your objects into aMemoryStream
, and then return theMemoryStream
from the service:public Stream GetSomeObject(int groupId) { byte[] bytes; var serializer = new JavaScriptSerializer(); switch(groupId) { case 2: var groups = GetGroups(); // fill the groups however bytes = Encoding.UTF8.GetBytes(serializer.Serialize(groups)); break; case 3: var customers = GetCustomers(); bytes = Encoding.UTF8.GetBytes(serializer.Serialize(customers)); break; } return new MemoryStream(bytes); }
In that case, you would simply load the appropriate object into memory based on the parameters and return the appropriate strongly typed object via the
Stream
.This is the same approach I've used in the past to return Json results from a WCF Service without the type information (the approach was suggested by a member Microsoft's WCF team, so I figured it was fairly reliable).
这篇关于从WCF REST通用请求获取类型特定的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!