这是我第一次开发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
来延迟加载这些导航属性。
如何解决此问题?
答案 0 :(得分:4)
我已在此处修复了此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;
}