Monthly Archives: August 2012

Deployment of the WCF Web Service

By Tim Daniels
Deployment of the World Of Art Web Service app was a very simple one step process.

From the Build Menu in Visual Studio 2010, select the Publish WorldOfArtServiceApp option. This will launch the Publish Web dialogue box.

As you can see from the illustration below, my hosting provider, WinHost, supports the one click publishing feature in Visual Studio 2010. I’m using the same server URL and domain name as the World of Art Web App. Notice the check box to indicate this is an IIS application on destination.

By structuring the WCF web service as a separate project I have the flexibility to publish to other domain names I have hosted at WinHost. The only disadvantage to this approach, is that anytime the World of Art Web App is re-published with new development, the World of Art Web Service also must be re-published. It appears that the World of Art Web Service looses the reference to the ArtWorldObjects in the World of Art Domain.

Publish WCF Web Service in Visual Studio 2010

Publish WCF Web Service in Visual Studio 2010 to hosting provider Winhost

My hosting provider, WinHost, fully supports IIS 7 hosting for my .NET web apps and WCF web services. In the illustration below, I’m using the IIS 7 Manager to remotely manage the IIS 7 server where my applications have been deployed.

Internet Information Services (IIS) 7 Manager is an administration UI that provides you with a new way to remotely manage IIS 7.x servers. I recommend reviewing the Getting Started with IIS Manager article by Tobin Titus on the IIS.net website.

IIS 7  Support at WinHost

IIS 7 Support at WinHost

As you can see, deployment and remote administration of the World Of Art Web Service, is a very straight forward process. The important thing to remember, is that I put some careful forethought into selecting a hosting provider, that fully supports the combination of technologies exploited in the architectural design of the applications;

  • ASP.net
  • MS SQL Server
  • WCF Web Services
  • IIS 7 Remote Management
  • One-Click Publishing

Visit WinHost for more information.

In my next post, we begin discussions about the World of Art Client, which is consuming the artist data from the World of Art Web Service.

Configuration of the WCF Web Service

Depending on the requirements of the application, configuration of a WCF web service can be simple and straightforward, or sophisticated and complex. This first iteration of the World of Art Web Service has very simple requirements, so I’ve attempted to keep the configuration as straightforward as possible. Here is the Web.Configuration file from the web service.

The first thing to notice is the ConnectionStrings tag. Since the World of Art Web Service references the Data Access Layer and the Data Model, from the World of Art Web App, it is necessary to provide the database connection information to the web service.

Now, lets discuss the configuration of the World of Art Web Service. Configuring the service in the Web.config file, gives us the same flexibility in deployment and management of the web service, as configuring a web application in the Web.config file. Instead of hard coding configuration elements in the application code, it is preferable to expose this information in the configuration file. This will allow an administrator to configure the application for final deployment, without having to modify code and rebuild the application.

The World of Art Web Service, is based on the Windows Communication Foundation (WCF) and will be hosted by Internet Information Services (IIS). The web service run time environment is .NET Framework version 4. Therefore, this configuration is specific to a WCF services hosted in IIS. IIS depends on the mark up found in the .svc file to implement the WCF service. My hosting provider, Winhost, supports all of these components.

The WCF configuration scheme includes three major sections;

  • serviceModel
  • bindings
  • services

Here is the serviceModel seciton ;

The entire service configuration is bound by the system.serviceModel elelment. Lets first examine the services element. Within services we find one or more service elements. It has a name attribute to identify the service. Within the service element, the endpoint element is used to configure one or more service endpoints. Notice how the address attribute has a null value. Since this service is hosted in IIS, it’s not necessary to specify an endpoint address. In fact, specifying a fully qualified endpoint address can lead to deployment errors in IIS. With an IIS hosted WCF service, the end point address is always relative to the .svc file that represents the service.

The binding attributes of the endpoint element, specify how the endpoint communicates with the world, in terms of transport and protocol schemes. In this case, I specified the WebHttpBinding Class. A binding used to configure endpoints for WCF Web services that are exposed through HTTP requests instead of SOAP messages. I am also utilizing the bindingConfiguration attribute of the endpoint element. This attribute is used in conjunction with binding to reference a specific binding configuration in the configuration file. In this case it is referencing a binding elelment in the webHttpBinding section named “webHttpBindingWithJsonP”, where I specify a custom binding configuration using the crossDomainScriptAccessEnabled, which is set to true. I will discuss cross domain access to web services in a future post.

The final attribute of the endpoint element is the behavior configuration attribute. I gave the behavior configuration a unique name of “webHttpBehavior”. The configuration of the endpoint itself can be found in the behavior element of endpoint behaviors section. The only required element I utilized is the webHttp element. This element specifies the WebHttpBehavior on an endpoint through configuration. This behavior, when used in conjunction with the webHttpBinding standard binding, enables the Web programming model for a Windows Communication Foundation (WCF) service. I simply took all of the default values for attributes in the webHttp element.

As you can see, it is absolutely necessary to completely understand the operational requirements of the WCF web service, prior to configuration. There is a lot of flexibility in this area of WCF. However, with more flexibility comes more responsibility. In my next post I will cover deployment of the WCF web service.

Data Contract and Data Member Attributes Complete the Service Contract

As I stated several times in recent posts, the purpose of the Art Service is not to expose the business logic of the World of Art web app. Instead, the purpose of the Art Service, is to simply expose specific data elements from the World of Art domain, which can be served upon request, to various software clients. We have already discussed in detail, how the service contract attribute and the operation contract attribute, transform a simple interface and class implementation, into an actual web service. Now lets look at how we can complete the details of the service contract, by specifying exactly which data elements are to be exposed by the web service.

In the early phases of development, I isolated the entities of the world of art domain into a separate class library called ArtWorldObjects. Artist is one of the entities defined in this class library. At this time, I want the WorldOfArtServiceApp to only expose the Artist object, and more specifically, only certain properties of the Artist object. To acomplish this, I will utilize the Data Contract Attribute Class and the Data Member Attribute Class. The data contract attribute specifies that the type defines or implements a data contract. The data member contract, when applied to the member of a type, specifies that the member is part of a data contract, and can be serialized by the DataContractSerializer Class.

Here is the source code for the IArtist interface and the Artist class, with data contract attribute and data member attribute applied;

Click on this link; Artists in JSON Format to send a “GET” request to the World of Art Web Service app. You can also copy the following link into your browser’s address bar to retrieve the JSON data;

Upon examining the JSON text returned by the service, you will see that the only properties of artist returned by the web service, are those enhanced with the Data Member attribute. Here is what the artist data in JSON format looks like;

For the sake of convenience I added three properties, of type string, to the IArtist Interface;

  • ExceptionMessage – messages to communicate any problems to the client
  • DateOfBirthShortString – date in “mm/dd/yyyy” format
  • DateOfBirthLongString – date in fomat “Monday, August 06, 1928″

The short and long data formats where necessary, because I discovered that when type System.DateTime is serialized to JSON, a very nasty date format is produced. Something like this; “DateOfBirth”:”\/Date(-296931600000-0700)\/”. Therefore, I excluded the date of birth property with a type of System.DateTime from being a Data Member. However, you can see in the source code, that the short and long date of birth properties are derived from the System.DateTime date of birth property.

In my next post, I will cover the configuration and deployment of the World Of Art Web Service. After that, I will cover the client, which uses JavaScript to consume artist data from the web service. This link; Artists from the Web Service will launch the client in the World of Art Web App.

Implementation of the Web Service Interface

In my last post, we discussed the interface of the WCF application and how it forms a mutually binding contract, between the client and the service. The client must make their request in a format the service is expecting, and the service must fulfill the request in the format specified in the contract. This is all defined in the operation contract of the IArtService interface.

  • The request must be an XMLHttpRequest using the “GET” verb
  • The “GET” request must specifically request access to the ArtService.svc and the GetArtists method
  • The service must respond to the request with data serialized into the Json format

Here is a simple JavaScript function which would satisfy the contractual requirements for a request;

Once again, here is the IArtService interface where the mutually binding contract between the service and clients is defined;

As you can see, the interface also uses the System.Runtime.Serialization and System.ServiceModel.web name spaces to fulfill the request.

Now, lets examine the implementation of the IArtService interface. Here is the source code of the ArtService class which implements the IArtService interface;

As I stated in an earlier post, the purpose of the Art Service Class is not to expose the business logic of the World of Art web app. Instead, the purpose of the Art Service, is to simply expose specific data elements from the World of Art domain, which can be served upon request, to various software clients. In order to retrieve data from the world of art domain, the ArtService class references two name spaces from the WorldOfArtWebApp;

  • ArtWorldObjects
  • WorldOfArtWebApp.DataAccess

With these references, the GetArtist() method can reuse components of the WorldOfArtWebApp from the Data Access Layer and the Data Model defined in the Entity Framework.

The GetArtist() method is bound by the Operation Contract, defined in the IArtService interface, to return a list of artist objects. This requirement is satisfied by simply creating a new instance of the ArtWorldCollectionArtist Class, and accessing the collection of artist objects in the ArtWorldCollection property. The collection of artist objects is retrieved from the WorldOfArtContext, generated from the Entity Framework. The WCF web service will serialize each of this artist objects into JSON format as specified in the operation contract.

In my next post, I will discuss how to specify which properties of the Artist object are exposed to the web service. Also, here is the source code for the ArtWorldCollectionArtist class, invoked by the GetArtist() method.

Web Service Operational Parameters

Today I will continue the discussion of the service contract, as defined by the interface generated from the WCF application template. Once again, here is the source code of the interface;

The operational parameters of the service contract are defined as a method. In this application, the method is called GetArtists() and it returns a list of artist objects from the world of art domain, as defined in the ArtWorldObjects name space. Therefore, a reference to the ArtWorldObjects class library is necessary, as indicated by the using statement.

The GetArtists() method is enhanced with the Operation Contract Attribute, which indicates that a method defines an operation that is part of a service contract in a application. The operation contract attribute is also defined in the System.ServiceModel name space.

The GetArtists() method is also enhanced with the WebGetAttribute Class, which indicates that a service operation is logically a retrieval operation and that it can be called by the REST programming model. The WebGetAttribute Calss is defined in the System.ServiceModel.Web name space. The constructor method of the WebGetAttribute class will accept a variety of arguments, as named parameters, which populate the properties of the class and configure the operational contract between the service and client software. This first iteration of the web service is very simple and retrieves a very small data set, so I’m only passing the Response Format argument for now.

The Response Format parameter will set the ResponseFormat property of the WebGetAttribute class. This property determines the format of responses sent from a service operation. The two possible values are Xml and Json.

As you can see, the World of Art Service will return a response in the Json format. JSON stands for JavaScript Object Notation. JSON is syntax for storing and exchanging text information. The web service is now contractually obligated, to respond to requests with text. More specifically, text that is formatted in a way that JavaScript, running on the client, can parse and evaluate as objects. In this application the objects are abstract representations of artists. The clients will receive an array of artist objects. Click on this link, Get Artists to send a request to the World of Art Service, and see the JSON text returned by the service. This link, Artist will take you to the actual website that consumes this JSON data.

In my next post, I will discuss the implementation of the IArtService interface.

Web Service based on WCF and REST

Then next phase in the evolution of the World of Art Web App is the development of a web service, based on the Windows Communication Foundation (WCF), and the Representational State Transfer (REST) architectural model. SO lets get started.

Visual Studio 2010 and the .Net Framework 4.0 make development of web services a very straight forward process. As you would expect, Visual Studio has a WCF Service Application template already installed.

Visual Studio provides a WCF application template already installed

Visual Studio provides a WCF application template already installed

I decided to make the World of Art web service a separate project from the World of Art web app. The primary reason for doing this, is that later in the project, I want to demonstrate cross-domain access to web services, and keeping the service in a separate project will make deployment to multiple domains easier. Here is the solution explorer displaying all of components generated from the WCF application template.

WCF Project in Visual Studio 2010

WCF Project in Visual Studio 2010

The WCF application template generates three very important files for defining and developing the functionality of the web service;

  • Interface defining the contract and operations of the service
  • Web Configuration file
  • Service file

Here is the source code in these three files;

The service file, which I named ArtService.svc

The web configuration file

The Interface

The interface, which I named IArtService, defines both the service contract and the operational parameters of the service contract. The implementation of the interface, will facilitate the fulfillment of contractual requirements, between the client and the web service. The interface is enhanced with the service contract attribute, which indicates that an interface or a class defines a service contract in a application. This attribute is defined in the System.ServiceModel name space of the .NET Framework 4. This name space contains the classes, enumerations, and interfaces necessary to build service and client applications that can be used to build widely distributed applications.

In my next post I will cover the operational parameters of the service contract.

Return of the Jedi Blogger with HTML5 and Web Services

It has been a very busy summer and three months have slipped away since my last post. Since then, I’ve been working on other projects and doing some summer time traveling, which included visits to Los Angeles and Las Vegas. I have also been doing some very interesting work with HTML5 and web services. Well, with summer time activities winding down, it’s time to get back to work on the World of Art Web App project. In coming posts, I will be enhancing the server side of the web app, with a new web service, based on Microsoft’s Windows Communication Foundation (WCF). I will also enhance the client, with a new artist page based on the new web standard, HTML5.

My next post will cover the WCF web service. The purpose of the web service, is to listen for requests from various software clients, and respond to the requests with information from the World of Art Web app. The architectural objective in the design of the web service, is not to expose the business logic of the web app to world wide web. Instead, the design objective is to only expose specific data elements and implement a scheme, that enables client applications to access this data, and manipulate it using their own business logic. With these design objectives in mind, I have decided to use the Representational State Transfer (REST) architectural model.

The web service will reuse all of the Entity Framework and the data access layer components already developed. As previously stated, these components will not be exposed by the web service. However, the web service will use the output of these components to satisfy a basic contract for information, as requested by a client application.