【发布时间】:2019-06-05 14:31:02
【问题描述】:
我正在尝试首先在 WCF 服务中返回由实体框架数据库生成的实体框架对象。
这是我的界面
namespace HiplotSystemService.services
{
[ServiceContract]
public interface IServiceUsuario
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "Useru")]
object GetUsuario();
}
}
我的服务等级:
namespace HiplotSystemService.services
{
public class ServiceUsuario : IServiceUsuario
{
public object GetUsuario()
{
using (HiplotSystemEntities datacontext = new HiplotSystemEntities())
{
var response = datacontext.usuario.Where(x => x.id == 6).FirstOrDefault();
return response;
}
}
}
}
当我从邮递员那里调用服务时,我无法得到任何响应
尽管 Visual Studio 说 WCF 回答了 200 代码:
{
"name": "Microsoft.ApplicationInsights.Dev.Request",
"time": "2019-06-03T19:03:34.8763009Z",
"tags": {
"ai.cloud.roleInstance": "DESKTOP-5SD5F4O",
"ai.operation.id": "6b308fdc12cc3748bf7522f1169a3c66",
"ai.operation.name": "GET /services/ServiceUsuario.svc/Useru",
"ai.location.ip": "::1",
"ai.internal.sdkVersion": "web:2.10.0-32157"
},
"data": {
"baseType": "RequestData",
"baseData": {
"ver": 2,
"id": "|6b308fdc12cc3748bf7522f1169a3c66.5b1db8ff_",
"name": "GET /services/ServiceUsuario.svc/Useru",
"duration": "00:00:03.0628593",
"success": true,
"responseCode": "200",
"url": "http://localhost:61768/services/ServiceUsuario.svc/Useru",
"properties": {
"DeveloperMode": "true",
"_MS.ProcessedByMetricExtractors": "(Name:'Requests', Ver:'1.1')"
}
}
}
另外,如果我在返回之前抛出异常,我可以看到 var 响应,看起来很好,它有正确的用户信息。我有一些 POST 方法,它们工作正常
我已经尝试过的东西:
- 使用用户类型代替 var。
- 在
HiplotSystemEntities.Context.cs中设置Configuration.LazyLoadingEnabled = false - 在
HiplotSystemEntities.Context.tt中设置base.Configuration.ProxyCreationEnabled = false - 只返回一个布尔值 - 当我这样做时,我可以在邮递员中得到真或假
- 只选择一些要返回的属性,但仍然没有得到任何响应
- 将对象作为字符串返回,我在响应中得到对象,但显然是字符串
【问题讨论】:
-
结果被转换为
object。当您调试时,您将能够看到该对象是什么,但我认为如果没有更具体的类型,它不会被序列化。该类型代表服务与其消费者之间的合同。如果返回的东西实际上可以是任何对象,它就无法工作。 -
我也试过 usuario response = datacontext.usuario.Where(x => x.id == 6).FirstOrDefault();并且还返回一个List
,但是是同样的问题