Configuring a DCS Client Application to Use a Custom Context Class
You configure a DCS client application to use a custom Context class by using the custom <context> section of the application configuration file. You must include the Microsoft.ConnectedIndustry.ServiceModel.Common.ContextSection configuration class to enable the <context> section.
You must also specify how to serialize instances of this class. Add the <dataContractSerializer> element to the <system.runtime.serialization> section of the application configuration file and specify that the class should be serialized by using the data contract for the Microsoft.ConnectedIndustry.ServiceModel.Common.Context object. For more information about configuring serialization, see <system.runtime.serialization>.
The following code shows the elements of an application configuration file that configures the application to use a custom Context class named DCS.Samples.ExContextSample. For more information about the ExContextSample custom Context class, see Implementing a Custom Context Class.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="context" type="Microsoft.ConnectedIndustry.ServiceModel.Common.ContextSection,Microsoft.ConnectedIndustry.ServiceModel.Common,Version=1.0.0.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
...
</configSections>
...
<context>
<extension type="DCS.Samples.ExContextSample, DCSExtensionMessages"/>
</context>
...
<system.runtime.serialization>
<dataContractSerializer>
<declaredTypes>
<add type="Microsoft.ConnectedIndustry.ServiceModel.Common.Context, Microsoft.ConnectedIndustry.ServiceModel.Common">
<knownType type="DCS.Samples.ExContextSample, DCSExtensionMessages"/>
</add>
</declaredTypes>
</dataContractSerializer>
</system.runtime.serialization>
</configuration>
A client application can populate the properties that a custom context object implements. A custom scope provider can include the values of these properties when it constructs a list of scopes for the Discovery Service. For more information, see Building a Custom Scope Provider and Ranker. A DCS service can include a custom property value in the scopes that it responds to.
The following code example shows how to create an instance of the ExSampleContext custom Context class and specify a value for the ExProperty property. The example code passes the custom context object to a service named ExtensionsSampleService that implements an operation named GetInfo.
ExContextSample context = new ExContextSample();
context.Environment = "DEMO";
context.Organization = new Organization();
context.Organization.Plant = "DEMO";
context.ExProperty = "DemoExPropertyValue";
ExtensionsSampleServiceProxy target = new ExtensionsSampleServiceProxy();
RequestMessage request = new RequestMessage(...);
ResponseMessage response = target.GetInfo(request, context);