Implementing a Custom Scope Provider
A custom scope provider is a class that implements the Microsoft.ConnectedIndustry.ServiceModel.Discovery.IScopeProvider interface. This interface is defined in the Microsoft.ConnectedIndustry.ServiceModel assembly. For more information about the IScopeProvider interface, see The IScopeProvider Interface.
You implement the GetCurrentScopes method to return a list of scopes in a Microsoft.ConnectedIndustry.ServiceModel.Discovery.Scopes object. For more information about the Scopes class, see The Scopes Class.
The following code example shows the default ScopeProvider class provided with DCS. The GetCurrentScopes method returns a list of scopes based on the current context. The contents of each child property of the Organization property in the context is added as a scope to the Scopes object that the method returns. For example, the current context contains the following property values:
- Context.Organization.Plant property: ContosoBank
- Context.Environment property: HeadOffice
- Context.Organization.Holding property: London
- Context.Organization.BusinessUnit property: Finance
- Context.Organization.OrganizationalUnit property: CashOffice
The Scopes object that the GetCurrentScopes method returns contains the following list in the UriList property:
- ContosoBank
- ContosoBank:HeadOffice
- ContosoBank:HeadOffice:London
- ContosoBank:HeadOffice:London:Finance
- ContosoBank:HeadOffice:London:Finance:CashOffice
If the current context is null, the method returns a Scopes object that has no scopes. The method throws the ArgumentNullException exception if the Organization property of the current context is null or if the Plant child property of the Organization property is empty or null.
public class ScopeProvider : IScopeProvider
{
// Fields
private const string moduleName = "ScopeProvider";
// Methods
public Scopes GetCurrentScopes()
{
Scopes scopes = new Scopes();
Context context = Context.Current;
if (context == null)
{
Tracer.Write(TraceEventType.Warning, "ScopeProvider", Resources.GetCurrentScopesWithoutActiveContext);
scopes.UriList = null;
return scopes;
}
Organization currentOrganization = context.Organization;
if (currentOrganization == null)
{
Tracer.Write(TraceEventType.Error, "ScopeProvider", Resources.GetCurrentScopesWithNullOrganization);
throw new ArgumentNullException("Context.Current.Organization");
}
StringBuilder scopeBuilder = new StringBuilder();
List<string> scopeList = new List<string>();
if (string.IsNullOrEmpty(currentOrganization.Plant))
{
Tracer.Write(TraceEventType.Error, "ScopeProvider", Resources.GetCurrentScopesWithNullOrganizationPlant);
throw new ArgumentNullException("Context.Current.Organization.Plant");
}
scopeBuilder.Append(currentOrganization.Plant);
string currentScope = scopeBuilder.ToString();
Tracer.Write(TraceEventType.Verbose, "ScopeProvider", "Added scope {0}", new object[] { currentScope });
scopeList.Add(currentScope);
string environment = context.Environment;
if (!string.IsNullOrEmpty(environment))
{
scopeBuilder.AppendFormat(":{0}", environment);
currentScope = scopeBuilder.ToString();
Tracer.Write(TraceEventType.Verbose, "ScopeProvider", "Added scope {0}", new object[] { currentScope });
scopeList.Add(currentScope);
scopeBuilder.AppendFormat(":{0}", currentOrganization.Holding);
currentScope = scopeBuilder.ToString();
Tracer.Write(TraceEventType.Verbose, "ScopeProvider", "Added scope {0}", new object[] { currentScope });
scopeList.Add(currentScope);
scopeBuilder.AppendFormat(":{0}", currentOrganization.BusinessUnit);
currentScope = scopeBuilder.ToString();
Tracer.Write(TraceEventType.Verbose, "ScopeProvider", "Added scope {0}", new object[] { currentScope });
scopeList.Add(currentScope);
scopeBuilder.AppendFormat(":{0}", currentOrganization.OrganizationalUnit);
currentScope = scopeBuilder.ToString();
Tracer.Write(TraceEventType.Verbose, "ScopeProvider", "Added scope {0}", new object[] { currentScope });
scopeList.Add(currentScope);
}
string[] text = scopeList.ToArray();
scopes.UriList = text;
return scopes;
}
}
The next code example shows a custom scope provider named MyCustomScopeProvider. This scope provider extends the behavior of the default scope provider. It references an instance of a custom context class called ExContextSample, which adds a property named ExProperty to the current context. For more information about building a custom context class, see Building a Custom Context Class. The GetCurrentScopes method uses the default ScopeProvider class to construct a Scopes object, and then adds a scope that contains a concatenation of the Organization.Plant property and the ExProperty property to the list of scopes.
namespace DCS.Samples.Extensions
{
public class MyCustomScopeProvider : IScopeProvider
{
public Scopes GetCurrentScopes()
{
ScopeProvider scopeProvider = new ScopeProvider();
Scopes scopes = scopeProvider.GetCurrentScopes();
ExContextSample currentContext = Context.Current as ExContextSample;
if (currentContext != null)
{
List<string> uriList = new List<string>(scopes.UriList);
uriList.Add(string.Format("{0}:{1}",
currentContext.Organization.Plant,
currentContext.ExProperty));
scopes.UriList = uriList.ToArray();
}
return scopes;
}
}
}