Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Building a Family History Web Service Client

  This article is basically about Family History Web service using Visual Web Developer 2005 Express Edition In this article a client application is shown which is built as a front end to the Web service.
3Leaf Development

Difficulty: Easy
Time Required: 1-3 hours
Cost: Free
Software: Visual Basic or Visual C# Express Editions,
Hardware:
Download: Download

Last time, I walked through putting together a Family History Web service using Visual Web Developer 2005 Express Edition. To complete the circle, in this installment I'll show you a client application I built as a front end to the Web service. Before getting into the application itself, I want to report that since my last entry I've updated my development environment to use the latest beta release of the Microsoft Visual Studio Express Editions. If you haven't yet made the move yourself, I urge you do so at your earliest opportunity. Because the update introduces some breaking changes with earlier beta releases, this will likely inflict some pain if you've already built some applications. But for me, at least, the pain is far outweighed by the gain in bug fixes and features improvements, one of which I am pleased to highlight in this post. But I hold that for a bit later.

The client application for my Family History Web service is a Windows Forms application. As you may recall, the Web service exposes two methods; a search method and an update method. This provides the bare bones functionality I need to start gathering information about my ancestors. I thus started by creating a new Windows Application in Microsoft Visual C# 2005 Express Edition. Next, I added a connection to the Family History Web service as a data source.

All versions of Visual Studio provide an easy-to-use wizard for configuring a Web service as a data source for an application. I've described how to use the wizard in previous entries; you simply select the Data | Add New Data Source menu item. This launches a wizard that lets you choose from a Database, Web service or custom object as a data source. After selecting Web Service as my data source type, I clicked Next to open the Add Web Reference dialog.

 

One of the great benefits of Visual Studio 2005 is that you don't need to run Internet Information Services (IIS) on your development machine to build and test Web-based applications. Along with all that goodness, however, in certain circumstances you may have a minor problem when you go to write a client for a Web service. If you are building the client in a separate solution, as I am in this case, and haven't deployed the Web service to a publicly available address, which I have not, you won't be able to browse to the Web service using the page provided in the Add Web Reference dialog. To solve this, all I had to do was open the Family History Web service in Visual Web Developer 2005 Express Edition and run the application in debug mode. (And it's worth noting that I kept the Web service running while I built the client application to provide an end-to-end development environment.)

In the Web service summary page, I just copied the Url to the asmx page and copied it in to the URL text box of the Add Web Reference. I clicked Go and the wizard did the rest.

 

Alright, I've held you in suspense long enough and now I'll tell you about one of the great things in the latest release of Visual Studio 2005. If you did read my earlier entry about using the Google Web service, you may remember the problem I had with getting the wizard to properly display the data types exposed by a Web service. This was the result of a bug in earlier releases of Microsoft Visual Studio 2005 wherein in the subsystem responsible for generating the Web service proxy classes did not properly translate the schema exposed in the Web services contract (WSDL) into public properties. That meant that the data types did not appear correctly in the Data Sources window, which in turn meant that you could not take advantage of drag-and-drop databinding. Eventually I found a workaround for the problem and got everything working correctly, but only after a lot of frustration and cursing of the Microsoft Beta Gods.

To my great delight, after I successfully added a reference to the Family History Web service, I opened the Data Sources window and found that the data type was exposed in all its glory.

I quickly set about taking advantage of the tools provided in the Data Sources window: I updated associated controls for the BirthDate and DeathDate fields to use a DateTimePicker, and I changed the ID field to use a Label control

 

This made building the user interface very simple. I started by adding a SplitContainer control on the form and changing its orientation property to Horizontal. The modest design goal of my application was to provide a search capability on the top half of the form (using the FindPerson Web method exposed by the Web service) and using the bottom half of the form to add or update family member data (using the UpdatePerson Web method of the Web service). So, in the top panel of the SplitContainer I added to TextBox controls and a Button control to enable searching. To build the bottom half, in the Data Sources window I first changed the associated control for the top-level Person class to Details and then dragged it onto the form surface. Visual Studio drew all the necessary controls on the form and created the BindingSource and BindingNavigator components for handling the chore of binding the Web service data to the controls on the form. I rearranged the controls and added two Button controls to allow users to add new family members and update existing family members, respectively.

When a user executes a search using the top half of the form, the Web service returns an array of Person objects. The code below is from the private searchPersons method, which is called from the search button's click event handler. This shows the code that creates a new instance of the Web service proxy class, calls the FindPerson Web method using the first and last names input into the user interface. The array of Person objects returned from the Web service is bound to the controls on the form by assigning the array to the DataSource property of the BindingSource control.

Visual C#

localhost.FamilyHistory ws = new localhost.FamilyHistory();
localhost.Person[] persons = ws.FindPerson(firstNameSearch.Text, lastNameSearch.Text);

// Bind results to UI
personBindingSource.DataSource = persons;

Visual Basic

Dim ws As localhost.FamilyHistory = New localhost.FamilyHistory()
Dim persons() As localhost.Person = ws.FindPerson(firstNameSearch.Text, lastNameSearch.Text)

' Bind results to UI
personBindingSource.DataSource = persons

By default, the data for the first person in the array is displayed in the bottom half of the form. The BindingNavigator component allows users to navigate through the list of returned family members. Using a similar pattern to the search operation, the click event handlers for the new and update buttons call a private method called submitPerson. The new button differs from the update button only in that clear the value of the id field for the person. When the Web service receives an UpdatePerson message where the person id value is empty, this indicates this is a new record; otherwise, the person data is treated as an update to an existing record.

Visual C#

// Load data in Person object
localhost.Person person = new localhost.Person();
person.FirstName = firstNameTextBox.Text;
person.MiddleName = middleNameTextBox.Text;
person.LastName = lastNameTextBox.Text;
person.BirthDate = birthDateDateTimePicker.Value.ToShortDateString();
person.BirthLocation = birthLocationTextBox.Text;
person.DeathDate = deathDateDateTimePicker.Value.ToShortDateString();
person.DeathLocation = deathLocationTextBox.Text;
if (personId.Text != string.Empty)
{
person.ID = Convert.ToInt32(personId.Text);
}

// Send update to Web service

Visual Basic

' Load data in Person object
Dim person As localhost.Person = New localhost.Person()
person.FirstName = firstNameTextBox.Text
person.MiddleName = middleNameTextBox.Text
person.LastName = lastNameTextBox.Text
person.BirthDate = birthDateDateTimePicker.Value.ToShortDateString()
person.BirthLocation = birthLocationTextBox.Text
person.DeathDate = deathDateDateTimePicker.Value.ToShortDateString()
person.DeathLocation = deathLocationTextBox.Text
If (personId.Text <> String.Empty) Then
person.ID = Convert.ToInt32(personId.Text)
End If

' Send update to Web service
Dim ws As localhost.FamilyHistory = New localhost.FamilyHistory()
personId.Text = ws.UpdatePerson(person).ToString()
localhost.FamilyHistory ws = new localhost.FamilyHistory();
personId.Text = ws.UpdatePerson(person).ToString();

The return value from the UpdatePerson Web method is the id of the new or updated person record. This value is displayed back to the form.

Finally, I wanted to point out a special application setting that gets created when you add a connection to a Web service. Visual Studio 2005 automatically names the settings, and sets its value to the Url of the Web reference.

 

Application settings get stored in the application's configuration file, allowing you to update settings without having to recompile an application. Here's a snippet of the app.config file for the application.

<applicationSettings>
    
<FamilyHistoryClient.Properties.Settings>
        
<setting name="FamilyHistoryClient_localhost_Family_x0020_History"
            serializeAs
="String">
            
<value>http://localhost:1065/CS/Service.asmx</value>
        
</setting>
    
</FamilyHistoryClient.Properties.Settings>
</applicationSettings>

When the application is compiled, this file appears in the output directory as <appName>.exe.config.

To appreciate the beauty of this, I looked at the code for the proxy class that Visual Studio 2005 automatically generated for the Web service. You may not see the proxy class because it is hidden by default. To view the file, titled Reference.cs, but clicking the Show All Files button in the Solution Explorer toolbar and then expand Web References | localhost | Reference.map as shown below.

 

In Reference.cs I found the Web Service URL application setting is accessed in the constructor of the proxy to establish the Url for the target Web service.

Visual C#

this.Url = FamilyHistoryClient.Properties.Settings. Default.FamilyHistoryClient_localhost_Family_x0020_History;

Visual Basic

Me.Url = FamilyHistoryClient.Settings.
Default.FamilyHistoryClient_localhost_Family_x0020_History

While the auto-named property setting name is a bit unwieldy, this represents a big improvement on past implementations of Visual Studio 2005 Web service tools that hard-coded the Web reference Url in the proxy class. This made it difficult to use configuration settings to control the location of Web service endpoints at runtime. Of course, this will make it much easier for me to deploy my client application once I've found a home for the Family History Web service.

Follow the Discussion

  • orangeCatorangeCat

    Interesting article. Can you tell me whether or not Visual Studio express can create a client  (say windows client (c#)) to access/interact with  web service that is built in java and residing on an apache server?

    I see examples, as yours, that create a web service on IIS and create C# client. However, I'd like to consume an existing webservice that is not on IIS.

    I have downloaded VS Express C#, VWD,Vb.net.

    I have the wsdl for the service, but am totally confused as to how the pieces interconnect.

    Any help would be appreciated.

    Thanks

  • Chris PalmerChris Palmer

    orangeCat, one of the beauties of Web Services is that it shouldn't matter what platform (java/apache, IIS/.Net, etc.) a web service is built with - you should be able to consume that webservice using a client built using any platform you wish - properly built Web Services enable "mix and match" clients and services - that's really the whole point.

  • MosesMoses

    I would like to thank you guys for the tutorials you gave us, for free. Now I think they will help me grow in to knowing programming

    Thank you so much

    I promise to refere others to you site so they will learn more

    masukums@webmail.co.za

  • rohit negirohit negi

    hello,i want some guide of your because i m creating a aplication of family tree in asp.net 1.1 with c#.so can you give me some advise how to create family tree.

  • Sheldon NguyenSheldon Nguyen

    Thank you Microsoft and all of you guys for sharing

  • psps

    plz i need the total code 4 dis

  • Clint RutkasClint I'm a "developer"

    @ps:  Link for this is at the top of the page.

  • yelugabyelugab

    Thanks for the tuition. Its helping direct me in the right direction at long last.

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.