WCF 数据服务入门
这是我在空白 Web 应用程序 (.Net 4) 中拥有的唯一代码:
public class Spork
{
public string Name { get; set; }
public DateTime BirthDate { get; set; }
}
public class WcfDataService1 : DataService<Spork>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.SetEntitySetPageSize("*", 26);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
[WebGet]
public IQueryable<Spork> Get()
{
List<Spork> retval = new List<Spork>();
retval.Add(new Spork() { BirthDate = DateTime.Now, Name = "jason" });
return retval.AsQueryable<Spork>();
}
}
如果我转到 http://localhost:1285/WcfDataService1.svc/,我会得到以下响应:
<service xml:base="http://localhost:1285/WcfDataService1.svc/">
<workspace>
<atom:title>Default</atom:title>
</workspace>
</service>
到目前为止太好了,我想。现在,我想通过访问 http://localhost:1285/WcfDataService1.svc/Get
来获取我的 spork。但我收到一条“未找到‘获取’段的资源”。错误。我有什么误解吗?
This is the only code I have in an otherwise blank web application (.Net 4):
public class Spork
{
public string Name { get; set; }
public DateTime BirthDate { get; set; }
}
public class WcfDataService1 : DataService<Spork>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.SetEntitySetPageSize("*", 26);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
[WebGet]
public IQueryable<Spork> Get()
{
List<Spork> retval = new List<Spork>();
retval.Add(new Spork() { BirthDate = DateTime.Now, Name = "jason" });
return retval.AsQueryable<Spork>();
}
}
If I go to http://localhost:1285/WcfDataService1.svc/
, I get this response:
<service xml:base="http://localhost:1285/WcfDataService1.svc/">
<workspace>
<atom:title>Default</atom:title>
</workspace>
</service>
So far so good, I guess. Now, I want to get my spork by going to http://localhost:1285/WcfDataService1.svc/Get
. But I get a "Resource not found for the segment 'Get'." error. What am I misunderstanding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用 DataService 但 Spork 不是数据源(Context),它是一个实体类。
尝试在数据上下文中定义 Spork,例如使用实体框架模型或 Linq To Sql 模型。
You're using DataService but Spork is not a data source (Context), it is an entity class.
Try defining your Spork in a data context, for instance using an Entity Framework model or a Linq To Sql model.
您似乎正在尝试将 REST 与 WCF 结合使用。可以这样做(请参阅:http://msdn.microsoft.com/ en-us/magazine/dd315413.aspx),但默认情况下,WCF 是基于 SOAP 的。如果您想使用 URL + 动词,则必须在 web.config 中进行设置。
祝你好运!
It appears you are trying to use REST with WCF. It is possible to do so (see: http://msdn.microsoft.com/en-us/magazine/dd315413.aspx) but by default, WCF is SOAP based. If you want to use URL + verb, you will have to set it up in your web.config.
Good luck!