当我使用虚拟属性时,WCF Rest GET不返回任何内容
本文关键字:GET Rest 返回 任何内 WCF 虚拟 属性 | 更新日期: 2023-09-27 18:25:52
这是我第一次开发Rest WCF服务。
这就是我所做的让数据库中的所有用户:
namespace MyCompanyWCFService
{
[ServiceContract]
public interface IMyCompanyService
{
[WebGet(UriTemplate = "/users",
BodyStyle=WebMessageBodyStyle.Bare,
ResponseFormat=WebMessageFormat.Json)]
[OperationContract]
List<User> GetAllUsers();
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
MyClass DoWork();
}
}
实施:
namespace MyCompanyWCFService
{
public class MyCompanyService : IMyCompanyService
{
public List<User> GetAllUsers()
{
List<User> usersList = null;
using (var context = new MyCompanyContext())
{
usersList = context.Users.ToList();
}
return usersList;
}
public MyClass DoWork()
{
var returnObject = new MyClass
{
Id = 1,
IHaveNoDataMemberAttribute = "Meaningless",
Name = "Satchmo",
NoPublicGet = "Hello world!",
InternalProperty = 155
};
return returnObject;
}
}
}
User
型号类别:
namespace MyCompanyModel
{
[DataContract]
public class User
{
[DataMember]
public int UserId { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public string City { get; set; }
[DataMember]
public string Country { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public string InterestIn { get; set; }
[DataMember]
public virtual ICollection<User> Friends { get; set; }
[DataMember]
public virtual ICollection<User> FromWhomIsFriend { get; set; }
[DataMember]
public virtual ICollection<Activity> WantsToDo { get; set; }
[DataMember]
public virtual ICollection<Message> MessagesSent { get; set; }
[DataMember]
public virtual ICollection<Message> MessagesReceived { get; set; }
public override string ToString()
{
string output = string.Empty;
output = String.Format("UserId: {0}, Name: {1}, Age: {2}, City: {3}, " +
"Country: {4}, Email: {5}, InterestIn: {6}",
UserId, Name, Age, City, Country, Email, InterestIn);
return output;
}
}
}
MyClass
:
namespace MyCompanyWCFService
{
[DataContract]
public class MyClass
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
public string IHaveNoDataMemberAttribute { get; set; }
[DataMember]
public string NoPublicGet { private get; set; }
[DataMember]
internal ushort InternalProperty { get; set; }
}
}
和web.config
:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="MyCompanyWCFService.MyCompanyService">
<endpoint address=""
contract="MyCompanyWCFService.IMyCompanyService"
behaviorConfiguration="restfulBehavior"
bindingConfiguration=""
binding="webHttpBinding" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="restfulBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
<add name="MyCompanyContext"
providerName="System.Data.SqlClient"
connectionString="Server=.'SQLEXPRESS;Database=MyCompany;Integrated Security=True;MultipleActiveResultSets=True"/>
</connectionStrings>
</configuration>
但是,当我在网络浏览器上设置这个URI http://localhost:7342/MyCompanyService.svc/users
时,我什么也得不到。我还用PostMan Chrome扩展进行了测试,但一无所获。
调试代码context.Users.ToList();
返回三个用户。
但是,如果我这样做,http://localhost:7342/MyCompanyService.svc/DoWork
,它运行得很好:
{"Id":1,"InternalProperty":155,"Name":"Satchmo","NoPublicGet":"Hello world!"}
问题出在public virtual
属性上。如果我删除virtual
,它可以工作,但我需要virtual
来延迟加载这些导航属性。
如何解决此问题
我在这里修复了执行context.Configuration.ProxyCreationEnabled = false;
的错误:
public List<User> GetAllUsers()
{
List<User> usersList = null;
using (var context = new AdnLineContext())
{
context.Configuration.ProxyCreationEnabled = false;
usersList = context.Users.ToList();
}
return usersList;
}