Home > 2011 Fall DPS907, ASP.NET, Windows > SOAP XML Web Services on WCF – introduction

SOAP XML Web Services on WCF – introduction

November 30, 2011 Leave a comment Go to comments

This post will introduce you to SOAP XML Web Services on WCF.

It is the first in a two-part series. This post uses an in-memory (per-instance) data store. The next post will use a persistent data store, accessed through the Entity Framework.

This document was most recently updated in November 2011.

.

Introduction

At this point in the semester, you have some experience with one of the two styles of web services, RESTful. SOAP XML Web Services is the other style that you’ll encounter. Here are some key ideas that will help you get oriented with SOAP XML Web Services:

  • The plain-text data interchange format is XML
  • The XML conforms to SOAP
  • Often, a SOAP XML Web Service is published on a single (URL) endpoint
  • The service includes one or more methods; each method may accept parameters, and may return data
  • Service discovery is built in to the specification/standard
  • Requestors use GET or POST requests to interact with the web service
  • Requestors can use the service discovery feature to create a proxy class that models the web service (in fact, this is typical with most programming platforms)

.

On the Microsoft Web Platform, WCF technology is used to create SOAP XML Web Services. Version 4 of the .NET Framework improved the design and coding experience, and we will use version 4 in this two-part series. Scan the following article, and read about the WCF and web services topics:

What’s new in WCF 4 article

FYI – legacy versions of the Microsoft Web Platform used “asmx” item types to host SOAP XML Web Services. They were easy to create, but they were a solution for a very limited set of use cases. Do not use “asmx” item types for new web services. Scan the following article to read about the differences:

Legacy info on the difference between asmx and wcf web services

.

What is a WCF Service?

In Visual Studio 2010, a SOAP XML Web Service is created as a WCF Service. The following will introduce WCF Service concepts and topics.

A WCF Service is a program that exposes one or more endpoints, each of which exposes one or more service operations.

An endpoint specifies:

  1. An address where the service can be found (e.g. http://host.domain.com/contacts.svc)
  2. A binding that tells the client how to communicate with the service (e.g. HTTP)
  3. A contract that defines the service’s functionality (e.g. it will do “this task” and “that task”)

.

Creating a WCF Service

You create a WCF Service in Visual Studio 2010. It is an (item type) that you can add to a Project or a Web Site.

A Web Site is software and resources that you create in a specific folder/directory. A Web Site must be hosted on a web server (e.g. either the ASP.NET Development Server, or a Microsoft Web Platform server). You can deploy a Web Site using a simple copy operation. The web server will compile and execute an endpoint upon request.

.

A Project is software and resources that you create in a specific folder/directory. A Project can be hosted on a web server, or on another kind of host (e.g. an executable, or a Windows Service). You must compile (build) the project (which creates an executable, either a dll or an exe) before you deploy it.

.

The default WCF Service item type (that you create with Visual Studio 2010) will create three software objects:

1. A publicly-accessible endpoint

2. A C# code-behind for the (above) publicly-accessible endpoint, which implements…

3. A C# interface that describes the “service contract” and “service operations”

.

The following sections include important information about each object.

.

Publicly-accessible endpoint

You pick the name (e.g. “Contacts”), it adds a “svc” extension (e.g. “Contacts.svc”).

You don’t add code to this endpoint.

.

C# code-behind for the (above) publicly-accessible endpoint

This code file is located in the App_Code folder/directory.

For example, continuing to use the name above, it will be called “Contacts.cs”.

.

C# interface that describes the “service contract” and “service operations”

A “service contract” is an agreement to define and provide a number of “service operations”. An interface is used as the code container for both. By convention in the .NET Framework, an interface name begins with an upper-case letter “I”. The second letter is also upper-case.

For example, continuing to use the name above, the interface will be called “IContacts.cs”.

Each “service operation” is a method. The method will have a return value, and a parameter list.

Although not required, it is a best practice to use an interface to declare the “service contract” and “service operations”.

What is the relationship between the interface and the (class) implementation?

  • The interface defines the service’s methods.
  • The implementation contains code for these methods.

.

Method return values and parameters

As noted earlier, a method will have a return value, and a parameter list.

The types must be serializable (obviously).

Primitive types are serializable by default. Many other types in the .NET Framework are also serializable by default.

If you create your own types (e.g. Product, Supplier, Contact, Address, Customer, etc.), then you typically “mark” your types (with “attributes”) to define a “data contract”.

A “data contract” is an agreement to define and provide a number of “data members”. The default WCF Service item type adds the “data contract” to the interface code container.

For example, continuing to use the name above, it will be added to “IContacts.cs”.

Each type is a class (as it always is). You typically add the [DataContract] attribute to the class.

Public properties are used to define the publicly-accessible data members.

Each serializable property gets the [DataMember] attribute.

.

Creating a client requestor

When working on RESTful web services earlier in the semester, you learned that you could interact with your web service by using a browser, or an HTTP requestor like your Lab 1, Fiddler, or your service’s HTTP Test Client.

With SOAP XML Web Services, some of the same techniques can be used. However, the message packaging format is SOAP XML, which can be inconvenient to manually enter into a request.

Another approach is to create a client requestor. Conceptually, it will be similar to your Lab 1, because it will be able to make a request to the web service, and receive (and process) the response. That’s what we’ll do here, by creating an ASP.NET Web Forms client requestor.

The key concept is to create a proxy, which will “model” the web service. Visual Studio has built-in tooling for this. Right-click the (web site) root, and select “Add Service Reference”. This will enable you to connect to the web service, discover its functionality, and have Visual Studio create a local proxy of your web service’s classes.

Then, in your web form code-behind, you simply 1) create an instance of the proxy, and 2) call its methods.

.

On your own – create a web service, and a client requestor

Your task:

In a new empty web site, create a service with two methods.

Method 1 will simply return your name (firstname last name) as a string.

Method 2 will return a collection (of strings) of your current-semester Seneca course codes (e.g. WSA500, SYS566, etc.).

In another new empty web site, create a (Default.aspx) web form that will call and display the data from the web service. Use Label web server controls for the data.

.

Preview of the topics we’ll cover in the next lecture

A blogger’s two-part series:

service create, and service consume (from Silverlight)

Another blogger’s article, some ideas are OK

Another blogger’s article on “what is an interface

WCF and EF on StackOverflow – data contracts

.

Reference articles

Learn C# page

Interfaces – C# Programming Guide

Inheritance – C# Programming Guide

C# Language Fundamentals

.


.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: