Fault Contract support in WSCF.blue

by Alex Meyer-Gleaves 31 May 2010 - 12:23 AM

Adding support for fault contracts to WSCF.blue has been on my TODO list for quite a while now. It was left behind when the port to WCF was done because ASMX did not have proper support for fault contracts so it was never actually part of WSCF.classic. Back in the old ASMX days you had to manually set the Detail property in the SoapException yourself and then add the fault details to the WSDL by hand.

In WCF your fault messages are declared on your operation using the FaultContractAttribute. When using WSCF.blue you could manually add the fault details to your WSDL and the appropriate FaultContractAttribute would be added to the generated service interface code. The problem was that if you edited your WSDL in the wizard the manually added fault details would be lost. Introducing a manual step like that into your process is something that we all want to avoid. Well, you no longer need to worry about that, because I have now added support for fault contracts to WSCF.blue.

Defining fault messages in your WSDL can be a little different to defining your input and output messages. For example, you can have multiple fault messages for a single operation and the same fault message is often reused for different operations. I decided that the message mapping page of the WSDL Wizard would be a reasonable place to put the configuration for your fault contract.

When you select an operation in the tree control a link label is displayed in the properties pane. Clicking the link will add a new fault message to the operation.

Operation Properties

When you select the fault message in the tree control you can then select a type from your schema to be used as the message body. You must provide a name for the message and that name must be unique to the operation. It does not have to be unique to the entire contract though. This means that you can reuse the same message name for a different operation and only a single message will be added to the WSDL. There is also a link label that allows you to remove the fault message if required.

Fault Message Properties

Lets use the fault message being configured in the screen capture above as an example of how this fits together in regards to the XSD and WSDL. The type for the fault in the XSD is called customFault and is an explicitly named complexType.

<xs:complexType name="customFault">
  <xs:sequence>
    <xs:element name="errorCode" type="xs:string"/>
    <xs:element name="message" type="xs:string"/>
    <xs:element maxOccurs="unbounded" minOccurs="0" name="messages" type="xs:string" />
  </xs:sequence>
</xs:complexType>

The message is then defined as an element with the type set to the customFault type. Do not create your fault type as an anonymous type definition directly under the element for the message. Doing so will result in an error about a missing data type during code generation.

<xs:element name="customFault" type="import:customFault" />

When the type and message schemas are run through the WSDL Wizard, and the fault message is added as shown in the screen capture above, the resulting WSDL will have a new message defined as follows.

<message name="customFaultMessage">
  <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
  <part name="fault" element="import0:customFault" />
</message>

The portType will then have the fault defined for the operation.

<operation name="getRestaurants">
  <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
  <input message="tns:getRestaurantsIn" />
  <output message="tns:getRestaurantsOut" />
  <fault name="customFaultMessage" message="tns:customFaultMessage" />
</operation>

The binding will also have the fault defined for the matching operation.

<operation name="getRestaurants">
  <soap:operation soapAction="http://www.thinktecture.com:getRestaurantsIn" style="document" />
  <input>
    <soap:body use="literal" />
  </input>
  <output>
    <soap:body use="literal" />
  </output>
  <fault name="customFaultMessage">
    <soap:fault use="literal" name="customFaultMessage" namespace="" />
  </fault>
</operation>

Now when you generate your service code a FaultContractAttribute will be applied to the method on your service interface that declares the operation. Note that the FaultContractAttribute specifies customFault as the type to be used for the message body.

[System.ServiceModel.OperationContractAttribute(Action="http://www.foo.com/:getRestaurantsIn", ReplyAction="*")]
[System.ServiceModel.FaultContractAttribute(typeof(customFault), Action="http://www.foo.com/:getRestaurantsIn", Name="customFault", Namespace="urn:thinktecture-com:demos:restaurantservice:messages:v1")]
[System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
getRestaurantsResponse getRestaurants(getRestaurantsRequest request);

There have been a number of requests for fault contract support on the CodePlex forum and I hope that you too will find this helpful. A new V1.0.9 update release is now available on CodePlex for download. Please put this new feature through its paces and report any problems you find on the discussion forum.

Tags: ,

Categories: Web Services | WSCF | Development Tools

WSCF.blue Roadmap

by Alex Meyer-Gleaves 6 May 2010 - 12:00 AM

image The WSCF.blue team has been discussing the features we should include in the next release. Benjy has done a great job summarising the current roadmap on his blog and is inviting feedback. You might feel that important features are missing, or perhaps that a particular feature is a bad idea, and would take the tool in the wrong direction. I decided that I would share what features are important to me, and put out for consideration and feedback some concerns I have with particular features. It is worth noting at this point that this is not an official roadmap, and is really just a summary of the ideas that we have been throwing around.

Having used WSCF.blue in a team environment, I would certainly put having the ability to permanently persist the settings for a web service in a way that makes them easy to share at the top of my list. Perhaps some sort of project structure that keeps your WDSL and XSD files along with your preferences altogether. There are a number of different options that can be applied when generating your web service code, and it is important that this step be repeated consistently, regardless of the developers familiarity with that particular web service. In regards to the actual options, I would also like to see support for WS-Addressing and fault contracts in the WSDL generation process. Adding these into the contract manually becomes another step that could be forgotten.

The issues noted about T4 templates are primarily concerns that I raised with the team. You get great flexibility with such a technology but it does come at a cost. These templates are not immediately approachable to all developers, especially when you are dealing with a complex model with many possible combinations. There are tools that make developing templates a more pleasant experience, but the mixing of static and dynamic content in such a manner still makes it fairly easy to introduce errors. I worry that it would become difficult to determine if a problem exists within the codebase or has been introduced in a customised template. Being an open source project there is only so much time that can be dedicated to support, so making things simple and reliable feels important. There is of course also the issue of upgrading templates, which forces users to migrate their customisations each time changes are made to the original.

Microsoft has released Feature Builder and it looks like it would be a very good fit for a tool like WSCF.blue. It certainly appears to be a considerable improvement over the Guidance Automation Toolkit (enough said). I would think that if we chose Feature Builder as the primary user experience, we would want to write an Ultimate Feature Extension that takes advantage of the modeling and visualization features that exist in Visual Studio 2010 Ultimate Edition. This is the point that you can start to do some really interesting things with Feature Builder. The problem is of course the requirement to have the most recent and expensive version of Visual Studio. My concern here is that we would make the tool inaccessible to a number of developers, and we really want as many people as possible to use the tool and embrace the contract-first approach.

There are a lot of great ideas on the roadmap and some really interesting technology choices to be made. I would be glad to hear your thoughts on any of these issues and the roadmap in general.

Tags: ,

Categories: Web Services | WSCF | Development Tools

WSCF.blue now supports Visual Studio 2010 RC

by Alex Meyer-Gleaves 24 February 2010 - 12:56 AM

image There is another WSCF.blue update (V1.0.7) available for download on CodePlex. This update adds support for Visual Studio 2010 RC in addition to Visual Studio 2008. Please note that Visual Studio 2010 Beta 2 is not supported.

All features should work exactly the same way in both versions of Visual Studio. If you have any problems please jump onto the Issue Tracker and let us know.

I would also love to hear more about what features you would like to see in upcoming versions of WSCF.blue. If you have any thoughts please contact me or simply start a thread in the Discussions forum. Your feedback is always welcome.

Tags: ,

Categories: Web Services | WSCF

WSCF.blue V1.0.6 Update

by Alex Meyer-Gleaves 18 February 2010 - 1:20 AM

There is now a V1.0.6 update for WSCF.blue that includes fixes for a number of bugs that have been reported since the V1.0.5 release. It has taken some large and complex contracts to uncover some of the more obscure bugs. Below is a list of the bugs that have been fixed:

  • The data contract type filter was not including all the required types in some complex contracts.
  • When adjusting the casing of enumeration members references in DefaultValue attributes and constructors were not being updated.
  • When using the List<T> option along with the Public properties option the backing field used for properties was sometimes left as an array instead of a generic list.
  • When using the List<T> option along with the Public properties option the backing field used for properties with an XmlChoiceIdentifier attribute was converted to a generic list instead of being left as an array to match the property.
  • Fixed an Adjust casing bug related to enumeration values that cannot be used as valid property names. The XmlEnumAttribute was being set to the generic ItemX property name instead of being left with the value from the original enumeration.

If you have any problems with the update please let us know through the Issue Tracker. Thanks to everyone who has reported bugs.

Tags: ,

Categories: WSCF | Web Services

Contract-First article on MSDN

by Alex Meyer-Gleaves 4 October 2009 - 12:44 PM

Congratulations to Christian and Buddhike for their excellent MSDN article: Schema-based Development with Windows Communication Foundation. If you are new to contract-first web service development, or even if you have been practicing it for a while, I am sure you will really enjoy reading this article. It is great to see an article on MSDN that is focused on contract-first and how it can be achieved with WCF. Of course, there is a section dedicated to the WSCF.blue Visual Studio 2008 Add-in.

The diagram below was created for the article, and Christian has also put it on the WSCF.blue homepage on CodePlex.

The schema-first approach has five discrete steps.

I really like this diagram, it paints a picture that is very clear and easy to understand. It shows that there is a sequence of steps that are undertaken when doing contract-first development, and highlights that this entire sequence becomes an iterative process. Well done guys!

Tags: ,

Categories: Web Services | WSCF

WSCF.blue V1 Final Released

by Alex Meyer-Gleaves 30 September 2009 - 4:23 AM

image The team is very pleased to announce that WSCF.blue has achieved the important V1 milestone. Thank you to everyone who provided feedback in the form of suggestions and bug reports. This input has really helped us improve the stability of WSCF.blue and increase the available feature set. It has also given us many other good ideas that will no doubt be rolled into future releases.

The primary feature set for WSCF.blue V1 includes the following:

  • A WSDL Wizard that allows the developer to step through the creation of a WSDL from one or more XSDs.
  • A Data Contract Generator (similar to XSD.exe, XSDObjectGen.exe and SvcUtil.exe) that generates the .NET equivalent of the XSD types.
  • A Service/Endpoint Stub (SVC) Generator and
  • A Client Proxy Generator.
  • A Generate Data Contract Code feature that supports the selection of multiple XSD/WSDL source files. (More Information)
  • A Paste XML as Schema option that generates a schema for a block of XML in the clipboard. (More Information)
  • Support for C# and VB.NET code generation.
  • You can choose if operation methods on your service class will throw a NotImplementedException, call an implementation method in a partial class, or will be defined as abstract methods. (More Information)
  • Force the SOAP actions (Action and ReplyAction) applied to each operation contract follow the standard WCF format: <namespace>/<service>/<operation>[Response]
  • Errors found in your WSDL are reported in a WSCF.blue pane in the Output window. (More Information)

Like many other open source projects, I have found WSCF (classic and blue versions) to be invaluable over the years. It feels good to give something back to the .NET open source community that has provided me with such a wide range of free and quality software to choose from. Thanks to WSCF.blue contract-first web service development in WCF for .NET developers is easily achievable. Please keep the feedback coming in and enjoy the release.

Tags: ,

Categories: WSCF | Web Services

Improved WSDL error handling in WSCF.blue

by Alex Meyer-Gleaves 28 September 2009 - 4:29 AM

I finally got around to making one of those improvements to WSCF.blue that has been calling out for my attention for a while. It was to improve the error handling when the WSDL selected for Client-side proxy or Service-side stub code generation contains errors that will cause the process to fail. Previously, you would receive an exception and stack trace that unfortunately did not provide the details required to determine what was actually wrong with your WSDL. Now when these errors are encountered a message box will inform you of the problem and direct your attention to the new WSCF.blue pane in the Output window where you can see all the error details.

WSDL errors in the Output window.

As you can see in the screen capture above the error messages are very descriptive and informative, and in fact are the same as those returned from svcutil.exe. I am sure you always get your WSDL correct first go (especially with the help of WSCF.blue), but just in case your having one of those days, this should help you out.

Tags: ,

Categories: Web Services | WSCF

Paste XML as Schema in WSCF.blue

by Alex Meyer-Gleaves 21 September 2009 - 9:24 PM

The WCF REST Starter Kit includes a Visual Studio Add-in called Paste XML as Type that, as you would expect given the name, allows you to paste XML from the clipboard into your code as a .NET type definition. This works well in a REST scenario where the response from a web service is XML that you want to deserialise into a .NET type for use on the client. WSCF.blue now includes a similar feature, but one that is designed to better fit with the contract-first approach. Instead of taking the XML and creating a .NET type definition, WSCF.blue infers an XML schema that can be used in your web service contract.

Imagine that you have an application that passes XML documents around via the file system or maybe even a queue. You decide that you would like to update this application to use web services, and will of course take the contract-first approach using that WSCF.blue tool that your friends keep raving about. You can open one of those XML documents and copy the contents into the clipboard, then jump over to Visual Studio and have an XSD file containing the appropriate schema added to your project in seconds. After defining the schema for your messages, WSCF.blue will help you construct your WSDL and generate your stub service and client code.

The new feature appears as the Paste XML as Schema menu item on the Edit menu in Visual Studio. The menu item will only be enabled if a project is currently open and selected. You may notice in the screen capture that the menu items looks suspiciously familiar, maybe even to a neighbouring menu item.

Paste XML as Schema Menu Item

WSCF.blue will ensure the text in your clipboard is valid XML before inferring the schema. The paste operation actually adds a new file into your project and does not paste the XSD directly into the active editor window. I know that is a little strange for a paste operation that corresponds to a text formatted copy operation, but think of it as pasting a magically created file into your project. Your new XSD file will be named the same as the root element in the XML. For example, performing the paste with the XML below in your clipboard:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
</catalog>

Will result in the currently selected project having a file called catalog.xsd added to it. Below is the schema inferred from the XML that will appear in the XSD file:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="catalog">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="book">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="author" type="xs:string" />
              <xs:element name="title" type="xs:string" />
              <xs:element name="genre" type="xs:string" />
              <xs:element name="price" type="xs:decimal" />
              <xs:element name="publish_date" type="xs:date" />
              <xs:element name="description" type="xs:string" />
            </xs:sequence>
            <xs:attribute name="id" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Please download the latest V1 Beta 2 build (1.0.4) and take it out for a spin. Hopefully this feature will make it easier for people to create web services using contract-first and will remove the need for passing around those old XML documents. Web services are a much more fun way to pass XML around!

Tags: ,

Categories: Web Services | WSCF

Data contract generation is now available in WSCF.blue

by Alex Meyer-Gleaves 1 September 2009 - 2:47 AM

I am pleased to report that data contract generation is now available, and that the functionality has been extended to allow the selection of multiple source XSD or WSDL files. This feature is available in the V1 Beta 2 release, and as always, user feedback and bug reports are welcome.

Data contract generation was one of the features from WSCF.classic that got left behind while the work was done to support WCF in WSCF.blue. Since this was a very popular feature it was important to make sure it was available in WSCF.blue as part of the V1 release.

The generated data contract types are designed to work with the XmlSerializer. WSCF.blue uses the XmlSerializer because the DataContractSerializer is limited in regards to its support for many of the XSD constructs that often appear in contract-first WSDL. Regardless, I would like to add the ability to generate data contract types using the DataContractSerializer. Please let me know if this is a feature you would like to see implemented.

To generate data contracts start by selecting the required XSD or WSDL file in your project. You can select one or more files as required.

Source XSD files.

Right-click your selection and choose Generate Data Contract Code... from the context menu.

Generate Data Contract Code...

You will be presented with the Data Contract Code Generation Options dialog.

Data Contact Code Generation Options

You can choose to create a separate file for each type or keep them all in a single file. The usual list of Code generation options are also available. Click Generate and enjoy your data contracts!

Tags: ,

Categories: Web Services | WSCF

Controlling your Service method implementation in WSCF.blue

by Alex Meyer-Gleaves 8 August 2009 - 3:26 AM

I have added some new options to WSCF.blue that control how the methods in your service class are code generated. It was a post in the Discussions forum on our CodePlex site that prompted me to finally add the feature that I myself have wanted for some time now. The default service class code generated by WSCF.blue contains operation method implementations that throw a NotImplementedException. The issue with this approach is that you need to manually update the generated code to call another class that contains your actual service implementation, otherwise you will loose your implementation code during the regeneration process. You can now decide if you would like your service implementation to exist in a partial class or abstract class that can be defined in a separate file. These options can be found in the Service Method Implementation group in the Code Generation options dialog.

Options for Service Method Implementation

The first and default option is to generate method bodies that throw a NotImplementedException. Making this option the default keeps the behaviour consistent with previous builds of WSCF.blue. However, using this option will always result in your having to perform same manual modifications to the generated service class.

[System.ServiceModel.ServiceBehaviorAttribute()]
public class FooService : IFooService
{
    public virtual FooResponse Foo(FooRequest request)
    {
        throw new System.NotImplementedException();
    }
}

Selecting the partial class option will cause the generated service class to be marked as partial. It will also contain operation methods that call through to a partial method that can be defined in another partial class and in another file. This file must be created manually yourself, but once created it will remain untouched when you regenerate your service code.

[System.ServiceModel.ServiceBehaviorAttribute()]
public partial class FooService : IFooService
{
    public virtual FooResponse Foo(FooRequest request)
    {
        return FooImplementation(request);
    }
}

The naming convention for the partial method is the name of the operation method suffixed with Implementation. In the example above you can see the Foo operation method calls a FooImplementation method that is expected to be present in your partial class definition. You will receive a compiler error until you have defined your partial class containing the appropriate partial methods.

public partial class FooService
{
    protected FooResponse FooImplemenation(FooRequest request)
    {
        FooResponse response = new FooResponse();
        // Some web service processing code.
        return response;
    }
}

Your partial class definition should be created in a separate file to the service class that calls it. This will ensure you do not have to make any changes when regenerating your service code, which is obviously the very issue the option is designed to solve.

Because a partial class can be defined in a separate file, but must reside within the same project, an abstract class option was added that allows you to place your service implementation in another assembly. This option causes an abstract service class containing abstract operation methods to be code generated.

[System.ServiceModel.ServiceBehaviorAttribute()]
public abstract class FooService : IFooService
{
    public abstract FooResponse Foo(FooRequest request);
}

You can then inherit from the abstract service class and place your implementation in the overridden abstract methods. Similar to the partial class approach, this allows you to separate your implementation into another file that remains untouched when you regenerate your service code, but also allows for the implementation to be created in another assembly.

public class FooServiceImplemenation : FooService
{
    public override FooResponse Foo(FooRequest request)
    {
        FooResponse response = new FooResponse();
        // Some web service processing code.
        return response;
    }
}

Both the partial class and abstract class approaches are equally valid, and both result in the desired outcome of protecting your implementation code. The important points to remember are that when using the partial class option, your implementation class must have the same name as the code generated service class and must be defined in a file within the same project. When using the abstract class option, your implementation class cannot have the same name as the code generated service class but can be defined in a file that is not in the same project. Which you prefer to use entirely up to you and is probably a matter of taste for the most part.

I hope you find this feature useful, I know I certainly will. Please report any WSCF.blue bugs in the Issue Tracker and feel free to raise your suggestions in the Discussions forum.

Tags: ,

Categories: Web Services | WSCF

About the author

Alex Meyer-Gleaves I'm a software developer living in Australia (that island like continent in the southern hemisphere). I love Microsoft .NET and C#. I hate early mornings, slow drivers and Lotus Notes.

Google Reader Clips

SpringWidgets
RSS Reader
This widget is the staple of our platform. Read all your feeds right here with thisone widget - Supported feeds are OPML, RSS, RDF, ATOM. Watch your favorite Podcastin the embedded Video Player on the Desktop or publish your own video playlist toyour site for others to view!

Recent Comments

Comment RSS

Links

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008