Click here to Skip to main content
15,562,561 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!
I Created a REST service used by WCF,Like This:
Service1.cs
C#
namespace WcfRestService1
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class Service1
    {
        [WebGet(UriTemplate = "")]
        public List<SampleItem> GetCollection()
        {
            return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
        }
        [WebInvoke(UriTemplate = "", Method = "POST")]
        public SampleItem Test(SampleItem instance)
        {
            return new SampleItem() { Id = 123, StringValue = instance.StringValue };
        }
    }
}

SampleItem.cs
C#
namespace WcfRestService1
{
    public class SampleItem
    {
        public int Id { get; set; }
        public string StringValue { get; set; }
    }
}

When I use the POST method ,it has some error :
Request Error
The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service

My english is poor,so sorry i can`t say more!
Best wishes!
You can download my code from here
Posted
Updated 7-Jul-10 0:35am
v3

I haven't much experience in WCF but first thing i found wrong here is SampleItem class.

You can only send basic types throgh services so your class cannot be returned in service method. To do this try something like this:


C#
namespace WcfRestService1
{
    [DataContract]
    public class SampleItem
    {
       [DataMember]
        public int Id { get; set; }
       [DataMember]
        public string StringValue { get; set; }
    }
}


Where [DataContract] defines any object sending throgh service, and [DataMember] for each basic type field of certain object.

I'm not sure if this everything to make it work but you need things i mentioned for sure.
 
Share this answer
 
Comments
艾绍荣 9-Jul-10 9:35am    
Reason for my vote of 5
Thank You!
First! Thank helianthus87 !
But my Problems still exist,I test the code like here:
Service1.cs
C#
...
[WebInvoke(UriTemplate = "", Method = "POST")]
public string Test(string instance)
{
    return instance;
}
...

When I POST the URL http://localhost/WcfRestService1 and use <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">String content</string> for request_body.
It is not difficult ,but the error is still exist!
VS2010 &.net 4 & win 7
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900