Aadith Ramia
Aadith Ramia

Reputation: 10329

WCF : Making methods in DataContract object accessible to web service clients

Objects of the following class need to be passed as arguments to a WCF web service:

    public class Context
    {
        public static readonly string AUTH_CODE = "AUTH_CODE";

        public static readonly string REQUEST_TAG = "REQUEST_TAG";

        private readonly IDictionary<string, string> _context = new Dictionary<string, string>();

        public void AddProperty(string key, string value)
        {
            _context.Add(key, value);
        }

        public string GetProperty(string name)
        {
            return _context[name];
        }
    }

I tagged the class with [DataContract] and AUTH_CODE, REQUEST_TAG and _context fields with [DataMember]. The class itself is defined along with the web service on the server side.

When I try to instantiate an object of this class so that I can pass it as parameter while calling the web service from the client, I observe the following:

  1. AUTH_CODE and REQUEST_TAG are not visible.
  2. _context is visbile though it is a privte member
  3. AddProperty an GetProperty methods are not visible

Can you please explain the above behavior?

Also, I would need access to AddProperty method to populate the object before calling the web service. How do I achieve this?

Note : This is my first experience with WCF. If I am straying off from any standard practices towards achieving such behavior, please advise.

Upvotes: 1

Views: 1116

Answers (1)

Alex
Alex

Reputation: 8937

  1. The DataMemberAttribute attribute is ignored if it is applied to static members.
  2. Member accessibility levels (internal, private, protected, or public) do not affect the data contract in any way.
  3. Data Contract supports only state, not behaviour. So your methods do not affect on your data contract.

The behaviour of data contacts is described in the following MSDN link: http://msdn.microsoft.com/en-us/library/ms733127.aspx

Upvotes: 2

Related Questions