从服务返回 json 个对象
returning json object from service
当我这样做时 return Employee
class 工作正常但我只需要几个属性所以我试图让它像这样工作 ERR_CONNECTION_REFUSED
浏览器,但代码隐藏没有错误。
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "employees")]
public object GetEmployees()
{
var c = Employee.GetList().Select(x => new { id = x.Id, title = x.Title, person = x.FullName});
return c;
}
[OperationContract]
object GetEmployees();
WebConfig
<service name="FOO.FOO.FOOService">
<endpoint address="http://localhost:8733/FOOService" binding="webHttpBinding" contract="FOO.FOO.IFOOService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/FOOService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
您不能将匿名类型与默认 WCF 序列化程序一起使用。如果要支持匿名类型,则必须创建自定义消息格式化程序 (https://msdn.microsoft.com/en-us/library/ms733844.aspx)。
对于您的情况,我建议创建 EmployeeDTO
(员工数据传输对象)类型,其中包含您希望从服务中 return 的字段。然后,您可以将此类型用作 GetEmployees
方法的 return 类型。
如果您真的不想创建数据传输对象(这是我的偏好),那么我建议从服务返回 Dictionary<string,string>
对象的列表。虽然有多种方法可以使 WCF 序列化与无类型对象一起使用,但其中 none 是可维护的或优雅的。使用词典应该会给您同样的灵活性,而不会出现这些问题。
您的代码可以重写为:
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "employees")]
public List<Dictionary<string, string>> GetEmployees()
{
var c = Employee.GetList().Select(x => new Dictionary<string, string> {{"id", x.Id.ToString()}, {"title",x.Title}, {"person", "x.FullName"}}).ToList();
return c;
}
不要忘记在客户端将字典的结果转换回您想要的类型。
我还建议您查看此问题的答案:
Passing an instance of anonymous type over WCF 解释为什么通过网络传递匿名类型是个坏主意。
当我这样做时 return Employee
class 工作正常但我只需要几个属性所以我试图让它像这样工作 ERR_CONNECTION_REFUSED
浏览器,但代码隐藏没有错误。
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "employees")]
public object GetEmployees()
{
var c = Employee.GetList().Select(x => new { id = x.Id, title = x.Title, person = x.FullName});
return c;
}
[OperationContract]
object GetEmployees();
WebConfig
<service name="FOO.FOO.FOOService">
<endpoint address="http://localhost:8733/FOOService" binding="webHttpBinding" contract="FOO.FOO.IFOOService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/FOOService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
您不能将匿名类型与默认 WCF 序列化程序一起使用。如果要支持匿名类型,则必须创建自定义消息格式化程序 (https://msdn.microsoft.com/en-us/library/ms733844.aspx)。
对于您的情况,我建议创建 EmployeeDTO
(员工数据传输对象)类型,其中包含您希望从服务中 return 的字段。然后,您可以将此类型用作 GetEmployees
方法的 return 类型。
如果您真的不想创建数据传输对象(这是我的偏好),那么我建议从服务返回 Dictionary<string,string>
对象的列表。虽然有多种方法可以使 WCF 序列化与无类型对象一起使用,但其中 none 是可维护的或优雅的。使用词典应该会给您同样的灵活性,而不会出现这些问题。
您的代码可以重写为:
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "employees")]
public List<Dictionary<string, string>> GetEmployees()
{
var c = Employee.GetList().Select(x => new Dictionary<string, string> {{"id", x.Id.ToString()}, {"title",x.Title}, {"person", "x.FullName"}}).ToList();
return c;
}
不要忘记在客户端将字典的结果转换回您想要的类型。 我还建议您查看此问题的答案: Passing an instance of anonymous type over WCF 解释为什么通过网络传递匿名类型是个坏主意。