c# - 返回实体框架对象时 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 方法,它们工作正常
我已经尝试过的东西:
- 使用用户类型而不是 var。
- 设置
Configuration.LazyLoadingEnabled = false
_HiplotSystemEntities.Context.cs
- 设置
base.Configuration.ProxyCreationEnabled = false
_HiplotSystemEntities.Context.tt
- 只返回一个布尔值 - 当我这样做时,我可以在邮递员中得到真或假
- 只选择一些要返回的属性,但仍然没有得到任何响应
- 将对象作为字符串返回,我在响应中得到对象但作为字符串,显然
解决方案
实际上有一个简单的解决方案。您可以Stream
改为使用object
您的输出。这样你就可以发回对象。
return new MemoryStream(Encoding.UTF8.GetBytes("your json string"));
推荐阅读
- python - 使用 Python 从网页中抓取单个 li
- machine-learning - fastai 机器学习课程:版本问题
- c++ - 与头文件一起使用时,使用个人 C++ 库编译代码会中断
- python - 如何在 Python Django 中将多个键值对作为查询参数传递?
- c# - 缩放子游戏对象
- wso2 - 在 Ballerina 中访问表数据结构时出现问题。错误无效操作...不支持非必填字段的字段访问
- ios - 无法在 Iphone 上模拟应用程序
- go - VSCode golang 模块只能在调试模式下工作 / 模块目录问题?
- python - 如何使变量等效于不同的变量
- php - 需要调整一个废弃的插件供我使用