Translate

Tuesday, August 14, 2012

Contract First Web Services

Preface:

Recently I was thrust into the need to create a new Web Service from scratch, but the client required that the service not be written code first. There are a lot of good arguments on why Contract First development is useful. Here is a chapter on why "Contract First" is preferred, a StackOverflow discussion, and a list of Six Reasons (page is slow to load, but worth it).

I found an MSDN blog that gave be a good middle point to work from, but it didn't contain the entire process from start to finish. So here it is, a three-part walk-through on how to create a .net contract first web service from scratch. Here is an overview of the process:
  • Create a Visual Studio Project
  • Create a WSDL using Altova XMLSpy, and add it to the project
  • Convert the WSDL to a web-service interface
  • Implement and test the web-service with soapUI

Here we go - Create a Visual Studio Project

In Visual Studio 2010, create a new ASP.NET Web Application; I called mine ContractFirstWebServiceExample.

Creating the Visual Studio Project
Now that the solution has been created, it's time to start working on the WSDL. A very good tool for authoring the WSDL is Altova XMLSpy, as mentioned earlier. But before I get into that, think about the specific web service we're going to implement.

The web service will be pretty strait forward, a "Hello Contract First Web Service World" kind of thing. The web service will simply take someones full name as the request, and return a response like "Hello, Abraham Dybvig." The request and response can be a string type:


Simple Web Service Diagram
Now that we know what our web service does, it's time to design a contact for it. In XMLSpy, create a new WSDL file. For this walk-through I'm using a v1.1 WSDL:

Create New Document Dialogue

This generates a stub WSDL with default values. I expanded all nodes on the right panel in this example:

Overview Pane (Expanded)

Edit the details of the WSDL, to names that make sense for the project and save the WSDL in the same folder as the Visual Studio project created earlier. The center pane of XMLSpy now looks something like this (I expanded all the nodes):

WSDL Definition

Here are my modifications:
  • Changed the namespace to http://contract.first.project.namespace
  • Changed the request and response messages to HelloUserRequest and HelloUserResponse respectively
  • Changed the request and response parameters to userName and reply respectively
  • Changed the port to IHelloUserPort (The reasoning for IHelloUserPort is discussed in a later section)
  • Changed the operation to SayHello
  • Changed the binding to HelloUserBinding
  • Changed the service to HelloUserService
One could go on, defining a complete WSDL with imported XSD types, and a robust set of operations in the service. Before the WSDL is published to clients, it can be iterated over in XMLSpy to include more capabilities. Save it to the existing project folder (from earlier) and add it to the Visual Studio project.

WSDL Added to Project
Now that the WSDL is part of the project, we can generate an interface, contained in a .cs file. Visual Studio does not come with a built in tool to create code interfaces from WSDLs, but it can be configured as an external tool by calling SvcUtil. This tool is included as part of the Windows SDK.
  • Add a new menu item, I called it Create WSDL Interface
  • The Command, as of this posting, is located at
    C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SvcUtil.exe
  • For parameters, I used
    /language:C# /out:$(ProjectDir)$(ItemFileName).cs $(ItemPath) /noConfig
    which instructs SvcUtil to create a new C# class file, of the same name as the WSDL in the same directory as the project. Config saving is disabled to prevent UAC errors on Windows 7.
Add External Tool

Once ScvUtil is ready the WSDL can be processed from within Visual Studio. With the WSDL open in Visual Studio, click on the new menu uption Tools/Create WSDL Interface. The results are given in the output window, and a new code file is created in the project folder ready for addition:

Create WSDL Interface
Add the interface to the Visual Studio project and create a new Web Service to implement it.
  • Add the interface .cs file to the project, mine is called IHelloUser.cs
  • Add a new web service to the project, I called mine HelloUserWebService
  • Include and implement the IHelloUserPort interface and add the [WebService] method decoration
The web service is ready for a basic test. Using soapUI to test the service will generate an error response since the method isn't implemented. (For those who prefer TDD, this is expected and encouraged!) Now the service can be fully implemented.

There you have it a soup to nuts walkthrough of contract first web service development. I really enjoyed learning about this the first time trough, and gathering information from various sources (linkes provided in the article).

~AD

P.S. Based on recent experiences working with web services, I have become huge fan of Contract First Web Service Development for a couple reasons. I prefer to use TDD whenever possible and Contract First makes it easy to generate a set of tests for the stub service. One odvious test: does the stub service respond to a soap call (using soapUI, for example) based on the contract WSDL?

Thursday, January 26, 2012

Basic and Advanced Forms for a Single List

Included here, is an old post I began working on at my previous employer while we were researching the usefulness of Infopath 2010. I was unable to finish this post, before transitioning from then to now. As such, I am posting this incomplete blog, as is, so that it might help others realize the full value of Infopath 2010 in their SharePoint projects.

The article is topic complete, all it is really missing is the screen captures to go along with the content...

BEGIN

One great tool that Infopath 2010 offers is different views for the same form. By using two views we can build a form with basic and advanced data entry versions. For this lab I created a list that has seven custom fields. Three number fields for ratings, and a text field for comments.

My intention is to make a simple survey where a user can quickly rate the overall presentation. I also want an optional advanced form to give the presentation seperate overall, content, and presenter scores, as well as leave comments.

After creating the list I have this form by default:

{Picture of Default Form}

As you can see, there are some changes that need to be made. The Title field isn't particularly useful, so I want to automatically fill it with some anonymous data, (See Making Title Fields More Useful with InfoPath 2010). Secondly, I want to keep the function of the number fields, for reporting and metrics, but change their appearance.

I start by customizing the form in InfoPath 2010.

{Picture of InfoPath before customizations}

First I select and delete the entire layout. Here are the customiations:
  • Create a centered page layout with a title, and call it "Rate this Presentation"
  • Add a two column table
  • In the first row, right column, add a custom 3x1 table and set the width of each column to 150px
  • Drag the Overall Rating field control into the first cell of the 3x1 table
  • Delete the label text above the field control
  • Change the control type to a option button
  • Copy and paste the button into the two remaining cells of the 3x1 table
  • Set the properties of the buttons such that the first one's value is 0, the middle to 1 as default, and the right one to 2
  • Enter "Overall Rating" in the label area
  • Use "below expecations," "met expectations," and "exceeded expectations" as descriptive text next to each option button
  • Delete remainint rows of the main table
  • Add two buttons to the form called "Advanced" and "Submit" respectively
The form should look something like this:

{Picture of WIP Form}

Now we need to create a new view for the advanced version of the form. Start by clicking "New" in the Views section of the Page Design tab.

{Pic of the New View button in action}

I called my new view "Edit item (advanced)." Copy the contents of the default view into the advanced view to use as a starting point. Here are the rest of the edits:
  • Add three rows to the main layout table
  • Create "Content Rating" and "Presenter Rating" controls similar to the existing "Overall Rating" control (Short-cut! Copy and paste the existing table and controls and just change the binding property to the correct field)
  • Add the Comments field controls to the last row of the layout table
  • Change the "Advanced" button to "Basic"
The new view looks something like this:

{Picture of the advanced view}

Now we need to connect the rules to the buttons. Switch back to the basic (default) view and bring up the rules manager for the "Advanced" button. Add a new action rule to tbe button and give it a name, "Advanced Clicked" will do. Add an action to switch views to the "Edit item (advanced)" view. Verify that the Submit button is set to submit the form.

{Picture of switch item rule dialogue}

All that remains is to configure the basic button on the other view. Switch to the advanced view and add a new rule to change views back to the default edit item view. Double check that the Submit button is submitting the form and we're all set to publish the form to the list.

Here is what my advanced form looks like in SharePoint:

{Picture of finished advanced form}

You might notice that whatever you set "Overall Rating" to is maintained when you switch between basic and advanced forms. Indeed, all the fields values are kept and saved on submit, even when not shown in the form. This hints at the possibility of building a multi-page form to fill out a complex list entry - but I think I'll save that for another time.

END