我正试图首先在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方法,它们工作正常
我已经尝试过的东西:
Configuration.LazyLoadingEnabled = false
中设置HiplotSystemEntities.Context.cs
base.Configuration.ProxyCreationEnabled = false
中设置HiplotSystemEntities.Context.tt
答案 0 :(得分:0)
对于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
随时让我知道是否有什么可以帮助您的。
答案 1 :(得分:0)
实际上,有一个简单的解决方案。您可以使用Stream
代替object
作为输出。这样您可以发回对象。
return new MemoryStream(Encoding.UTF8.GetBytes("your json string"));