Implementing a Custom Context Class

The default Context class, Microsoft.ConnectedIndustry.ServiceModel.Common.Context in the Microsoft.ConnectedIndustry.ServiceModel.Common assembly provides the Organization and Environment properties that enable a client application to specify a hierarchy of scopes. For more information about the Context class, see The Context Class. For information about how a client application uses the default Context class to define scopes for an operation request, see DCS Context, DCS Scopes, and How a DCS Client Invokes an Operation.

The following figure shows the public structure of the default Context class.

Dd632292.1c9798a7-522a-4bfc-a513-01401f423b66(en-us,MSDN.10).png

The public members of the Context class

To create a custom Context class, you can extend the default Context class and add your own properties.

You can define an additional property in two ways:

  • You can add a custom property to the class. A service that accesses this custom property must be configured to use the same custom Context class as the client application.
  • You can add a custom property to the Properties dictionary inherited from the default Context class. Properties that are defined in this manner are available to a service even if the service is not configured to use the custom Context class. Be aware that properties defined in the Properties dictionary are more cumbersome to serialize than custom properties that are added to the class and that they can result in larger messages passing between a client application and a service.

The following example shows a custom Context class named ExContextSample. This class illustrates both techniques for adding a property. The ExUsingPropBag property uses the Properties dictionary and ExProperty is a custom string property. Notice that a custom property must be serializable (that is, both of these properties are tagged with the DataMember attribute.)

Additionally, you should override the static Current property that is inherited from the Context class. The Current property returns the current context object as a Context object. You should verify that the object returned by the Current property is of the correct type.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ConnectedIndustry.ServiceModel.Common;
using System.Runtime.Serialization;

namespace DCS.Samples
{
    [Serializable]
    [DataContract(Namespace = "http://DCS.Samples")]
    public class ExContextSample : Context
    {
        private string exProperty = String.Empty;
        public const string PropBagProperty = "CustomProp";

        #region Public Properties
        // Value for this property will be accessible to service even 
        // if service is not configured to use Custom context
        [DataMember]
        public string ExUsingPropBag
        {
            get
            {
                if (Properties.ContainsKey(PropBagProperty))
                {
                    return Properties[PropBagProperty] as string;
                }
                return string.Empty;
            }
            set
            {
                Properties[PropBagProperty] = value;
            }
        }

        // Value for this property will be passed to service only 
        // when service is configured to use Custom context
        [DataMember]
        public string ExProperty
        {
            get
            {
                return exProperty;
            }
            set
            {
                exProperty = value;
            }
        }
        #endregion

        #region Overridden Methods
        /// <summary>
        /// Gets the current ExtendedContext.
        /// </summary>
        public static new ExContextSample Current
        {
            get
            {
                ExContextSample exContext = Context.Current as ExContextSample;
                if (exContext == null)
                {
                    throw new InvalidCastException("Invalid Cast Error");
                }
                return exContext;
            }
        }
        #endregion
    }
}

See Also

The Context Class

DCS Context

DCS Scopes

How a DCS Client Invokes an Operation

Configuring a DCS Service to Use a Custom Context Class

Configuring a DCS Client Application to Use a Custom Context Class