【问题标题】:no response from WCF when returning an Entity Framework object返回实体框架对象时 WCF 没有响应
【发布时间】: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;
             }
         }
     }
}

当我从邮递员那里调用服务时,我无法得到任何响应

Postman screenshot

尽管 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,但是是同样的问题

标签: c# wcf


【解决方案1】:

对于 WCF 中返回的复杂数据类型,请使用数据协定。 WCF在使用前必须知道如何对要传输的数据进行序列化和反序列化。请参考以下示例。
IService1.

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(UriTemplate ="/MyTest",Method ="GET",RequestFormat =WebMessageFormat.Json,ResponseFormat =WebMessageFormat.Json)]
        CompositeType Test();
    }


    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }

}

Service1.cs

   public CompositeType Test()
    {
        return new CompositeType()
        {
            StringValue = "Hello, busy world",
            BoolValue = true
        };
    }

结果。

这是官方文档,希望对你有用。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-data-contracts
如果有什么可以帮助的,请随时告诉我。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多