locked
How to pass a DataContract to WCF Get Method? RRS feed

  • Question

  • User1666665689 posted

    Hi All,

    Can any one provide me a sample on "How to pass a DataContract to WCF Get Method"? And this Get Method should in return send an Envelope to the Client.

    Plz.. provide me a sample.

    Regards,

    G.V.N.Sandeep

    Monday, June 24, 2013 7:14 AM

Answers

  • User220959680 posted

    WCF service contract is simply an Interface that is exposed to the client through end point. Service Interface is inherited from the service implementation class(shown below) , all the members from the interface are accessible in the service implementation class.

    DataContract is an attribute & used to expose data to the clients in Service Interface, below is the interface from default service being created in Visual Studio where WCF service application template is chosen. All the members in the class that are exposed through service with [DataMember] attribute are accessible from the client.

    Service Interface

        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
        [ServiceContract]
        public interface IService1
        {
    
            [OperationContract]
            string GetData(int value);
    
            [OperationContract]
            CompositeType GetDataUsingDataContract(CompositeType composite);
    
            // TODO: Add your service operations here
        }
    
    
        // Use a data contract as illustrated in the sample below to add composite types to service operations.
        [DataContract]
        public class CompositeType
        {
            bool boolValue = true;
            string stringValue = "Hello ";
    
            [DataMember]
            public bool BoolValue
            {
                get { return boolValue; }
                set { boolValue = value; }
            }
    
            [DataMember]
            public string StringValue
            {
                get { return stringValue; }
                set { stringValue = value; }
            }
        }

    Service implementation 

     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
        public class Service1 : IService1
        {
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value );
    
            }
    
            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                if (composite == null)
                {
                    throw new ArgumentNullException("composite");
                }
                if (composite.BoolValue)
                {
                    composite.StringValue += "Suffix";
                }
                return composite;
            }
        }


    provide me a sample.

    http://msdn.microsoft.com/en-us/library/dd483346.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 24, 2013 8:42 AM