|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
ContentsFeatures
Introduction and ConceptThe wsdl 1.1 and xml schema include an option, where each element (component) can be annotated. The annotation is not a part of the service metadata. It is data about the data, to inform the service consumer in the human readable style about the metadata. For this purpose, the annotation can have any text representation style, for instance: html tags. Annotations do not participate in the validation process. For interoperability reason, the annotation component is not recommended to use as an additional exchange pattern or context between the service and its consumer. The following picture is a screen snippet from the WSDL 1.1 spec:
As you can see in the above picture, there are lines such as 5, 8, 14, 19, etc. for wsdl component documentation and the spec said: 2.1.4 DocumentationWSDL uses the optional wsdl:document element as a container for human readable documentation. The content of the element is arbitrary text and elements ("mixed" in XSD). The documentation element is allowed inside any WSDL language element. The current release of the .NetFX 3.5 SP1 still doesn't have a built-in feature for annotating a wsdl document for any of its component. On the other hand, the WCF has a strong extensibility parading for plugging-in a custom annotator to describe your service metadata, as you will see more details later in this article. Now, the questions are coming. Why and what do I need to annotate in the wsdl document? How much do I do it, etc.? Well, there is no unique answer and guidelines for that. This article shows implementation of one example of this guideline, where the service contract is divided into two parts, such as partitioned wsdl documentations and xml schema annotation for object contract. 1. Service Contract Documentation in the WSDL MetadataFor documenting Service and Operation Contract in the WCF Service we can use C# Xml Comments and custom attribute such as
The documentation of the
The documentation of the
As you can see, there are only two wsdl components for mapping documentation from the WCF Service. That's the feature which is supported by this article. The second part of the WCF Service metadata annotation is related to the object and its serializable elements. In the above example, we have one object in the 2. Object annotation in the Xml Schema MetadataIn this case, similarly to previous one, we have C# Xml Comment lines and custom attribute such as Ok, let's continue with the following picture, where the
As the above code snippet shows, there are standard C# Xml Comment lines and additional custom attributes for annotation in the class. Note, the custom The following picture shows a snippet from the annotated XML Schema located in the metadata section:
As you can see in the above snippet, the
OK, but here is something different. The custom
As the above example shows, the The other feature of the WCF Service annotation is having the capability for controlling the annotation in the export metadata from the service. This task can be accomplished by custom service behavior and configuration in the config section. The following code snippet shows this extension: <behaviors>
<serviceBehaviors>
<behavior name='mex'>
<serviceMetadata/>
<annotation enable='true' exportAsText='false'/>
</behavior>
</serviceBehaviors>
</behaviors>
The My solution also includes a small tool called
Ok, back to the initial thinking about the annotations in the Service Metadata. Basically the service metadata such as wsdl document and XML schemas can be annotated in the following ways:
As you can see, both cases require public access to the existing service, in other words, the service has to be deployed and ran. The other case is an annotation of the metadata during the modeling time, where metadata is created in the Contract First fashion, for example: please see my recent article Contract Model for Manageable Services. In this case, the annotations are stored in the Repository like another resource. This approach will be more interesting, when upcoming Microsoft new model driven platform (OSLO) will be introduced at PDC 2008 at the end of this month. Ok, that's all for now, I hope you got the overall picture about the annotation in the service metadata. Let's start digging in the design and implementation of the WCF Service Extension for exporting annotations. Design and ImplementationThe design and implementation of the metadata annotation in the WCF Service model is based on the The following picture shows a class diagram of the
The DocumentAttribute class implements few interfaces such as Implementation of these interfaces are straightforward and well documented in the MSDN and WCF Samples, where examples for custom DataContract Surrogate and Wsdl Documentation are available. I am going to focus only on the implementation of the For this purpose, the following class has been designed to encapsulate the annotation process of the class decorated by
The public static void Export(WsdlExporter exporter, WsdlEndpointConversionContext context)
{
// MessageContractExporter
object messageContractExporterObj = null;
Type type = Type.GetType(
"System.ServiceModel.Description.MessageContractExporter+MessageExportContext,
System.ServiceModel, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089");
if (exporter.State.TryGetValue(type, out messageContractExporterObj))
{
// walk through the Elements
IDictionary o =
(IDictionary)messageContractExporterObj.GetType().GetField("ElementTypes",
BindingFlags.NonPublic | BindingFlags.Instance).GetValue(
messageContractExporterObj);
foreach (var item in o.Values)
{
XmlSchemaElement elem =
(XmlSchemaElement)item.GetType().GetProperty("Element",
BindingFlags.NonPublic | BindingFlags.Instance).GetValue(item, null);
OperationDescription operation =
(OperationDescription)item.GetType().GetProperty("Operation",
BindingFlags.NonPublic | BindingFlags.Instance).GetValue(item, null);
var operAnnotation =
operation.SyncMethod.GetCustomAttributes(typeof(DocumentationAttribute),
false).FirstOrDefault() as DocumentationAttribute;
if (operAnnotation != null)
{
elem.Annotation =
CreateXmlSchemaAnnotation(operAnnotation.Documentation,
operAnnotation.ExportXmlDoc, operation.SyncMethod);
}
// [MessageBody]
if (ExportAnnotationForMessageBody(exporter, operation, elem)) continue;
// [MessageHeader]
if (ExportAnnotationForMessageHeaders(exporter, operation, elem)) continue;
// multi-parts
if (ExportAnnotationForMessageParameters(exporter, operation, elem)) continue;
}
}
}
I mentioned earlier that, the The process of the
When the
Again, as you can see, the class is internal, therefore I used, in the above Export method, reflection to obtain Once we have access to the internal static bool ExportAnnotationForMessageHeaders(
WsdlExporter exporter, OperationDescription operation, XmlSchemaElement elem)
{
bool retVal = false;
MessageDescription md =
operation.Messages.Cast
As you can see, the first step is to query the internal static XmlSchemaAnnotation CreateXmlSchemaAnnotation(
string text, bool bExportXmlDoc, MemberInfo memberInfo)
{
XElement element = null;
XmlDocument doc = new XmlDocument();
if (memberInfo != null && bExportXmlDoc)
{
Type type =
memberInfo.DeclaringType == null ? (Type)memberInfo : memberInfo.DeclaringType;
string memberName = XmlDocumentation.CreateMemberName(memberInfo);
element = XmlDocumentation.Load(memberName, type);
}
if (element == null)
element = new XElement("member", new XText(text));
else
element.AddFirst(new XText(text));
doc.LoadXml(element.ToString());
XmlSchemaAnnotation annotation = new XmlSchemaAnnotation();
XmlSchemaDocumentation documentation = new XmlSchemaDocumentation();
documentation.Markup = doc.DocumentElement.ChildNodes.Cast<XmlNode>().ToArray();
annotation.Items.Add(documentation);
return annotation;
}
The responsibility of the above method is to create an annotation element XmlSchemaElement based on the C# Xml Comments and AnnotationAttribute. There is a Note that using the C# XML Comments must be enabled in the project by setting a path to the file, where these comments are going to be stored in the flat xml fashion. That's all, let's do some testing. Installation and TestingThe solution for WCF Service Extension for metadata annotation is divided into the following 3 projects:
The first project is a standalone Windows Form application for retrieving metadata from any wsdl or mex enabled endpoints. In addition, there is a feature for getting the metadata of the service contract located in the assembly. The second project is a host console program for demonstrating different contracts and operations. Finally, the third project is Note, the Testing annotations in the WCF Service Metadata is very straightforward based on the following steps:
The following picture shows a portion of the last MetadataSection for
That's all. You are ready to annotate your WCF Service. Please, have a look at the ConclusionThis article described an extension to WCF Service for exporting Metadata annotations. This feature is not built-in the WCF paradigm, but it can be easily added. The annotation is an optional element in the Metadata and having this human readable information embedded in the wsdl documentation and/or xml schemas is giving more explanation about the service contract for its consumers. Upcoming Microsoft vision for modeling platform, the metadata are stored in the repository where they play a key position between the logical model and decentralized runtime components (services, applications, etc.). Having more annotations of the metadata is a significant advantage for modeling and discovery. The Repository can easily hold thousands of resources such as endpoints, bindings, contracts, messages, schemas, policies, etc. The annotation will be a good helper for discovery and managing metadata in the Repository. We can hear more details about the modeling strategy at the Microsoft PDC 2008 conference, where project OSLO and its technologies will be introduced. See you there. References
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||