简体   繁体   中英

Getting started with WCF Data Services

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?

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM