Code Reference A collection of code for my reference (and perhaps other people too)

17Jun/100

Add a POST method to the WCF service

NOTE: This continues the series from the post Read the GET request parameters in the AfterReceiveRequest() method

I changed the name of the SchemeTest() method to SimpleTest() because the scheme test will be done on the POST request sending XML, not on a GET request.

Here is the new interface:

namespace WCFServiceWithScheme
{
    [ServiceContract]
    public interface ITestService
    {
        [OperationContract]
        [WebGet(UriTemplate = "SimpleTest?name={name}")]
        string SimpleTest(string name);

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "SchemeTest",
           BodyStyle = WebMessageBodyStyle.Bare,
           RequestFormat = WebMessageFormat.Xml)]
        string SchemeTest(RequestObject theRequest);
    }
}

Here are the new public methods:

namespace WCFServiceWithScheme
{
    public class TestService : ITestService
    {
        public string SimpleTest(string name)
        {
            return "The name is " + name;
        }

        public string SchemeTest(RequestObject theRequest)
        {
            return "nothing interesting";
        }
    }
}

We have to create the simple object that will be POSTed in the request. I called it 'RequestObject'.
Notice we have included a namespace in the DataContract. This will also have to be sent in the request to the service.

namespace WCFServiceWithScheme
{
    [DataContract(Namespace = "http://WCFServiceWithScheme")]
    public class RequestObject
    {
        [DataMember]
        public string FirstName { get; set; }
        [DataMember]
        public string LastName { get; set; }
        [DataMember]
        public short Age
        {
            get { return age; }
            set { age = value; }
        }

        private short age = 0;
    }
}

The XML submitted for this object is here. Notice the namespace is included.

<?xml version="1.0" encoding="utf-8" ?>
<RequestObject xmlns="http://WCFServiceWithScheme">
     <Age>21</Age>
     <FirstName>Howard</FirstName>
     <LastName>Lumpy</LastName>
</RequestObject>
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.