首页 > 解决方案 > 返回实体框架对象时 WCF 没有响应

问题描述

我正在尝试首先在 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 方法,它们工作正常

我已经尝试过的东西:

标签: c#wcf

解决方案


实际上有一个简单的解决方案。您可以Stream改为使用object您的输出。这样你就可以发回对象。

return new MemoryStream(Encoding.UTF8.GetBytes("your json string"));

推荐阅读