WCF Web Services

These notes introduce web services and WCF web services.

Web Service Basics

  1. A web service as we will view it is a set of (related) classes that exist on the internet and can be used by developers writing either web applications or other types of applications including desktop.
  1. Google Maps is a web service. You give at latitude and longitude and it returns a map. There are lots of examples of web services that are available: Google, Google Data API’s, Yahoo, ProgrammableWeb, WebserviceX. Programmers also build custom web services for companies as well as for the 3rd party market.
  1. From a programmer’s perspective, a web service is simply an Application Programming Interface (API) or web API that can be accessed over the internet.
  1. The purpose of a web service is to provide independent pieces of functionality via web methods. A result is computed in a single transaction. In other words, a client calls a web method, a result is computed and the complete result is sent back. A web service is not intended for the situation where a sequence of steps need to be accomplished that depend on one another. In this case the server would need to remember which step (and data) a client is on. (Example – 2-step login service). Some reasons why stateless is preferred are found here.

Google Mail (and tons of others) is a web service which obviously does maintain state through a database. You could certainly do that with ASP.NET.

  1. Web services allow communication between different platforms and languages through open standards. Two approaches:
  1. SOAP (Simple Object Access Protocol, an XML based language) envelope transports data across the internet via HTTP. The data is defined in XML. This is what VS does by default.
  1. Tutorial –
  2. Wiki –
  3. Describes the MS history with remote access protocols –
  1. REST (REpresentational State Transfer).
  1. Comparison of SOAP and REST
  2. How To in VS
  1. Web services typically have a machine-readable description of the operations offered in a WSDL (Web Services Description Language, an XML based language) file.
  1. UDDI (Universal Description, Discovery, and Integration) is a directory service that uses WSDL to describe interfaces to web services that can be discovered. This is not used very much, but the ideas are important and may become used in the future.
  1. Mashups refer to applications that combine the functionality of one or more web services. Service Oriented Architecture (SOA) can refer to the design of software systems around web services.
  1. See also:

Web Services Defined, Web Services Explained, WSDL, SOAP, SOA

  1. See below. Shows lecture notes about how WS work in VS 2013:

Suppose you have a service named MyService.

IService1.cs

[ServiceContract]

publicinterfaceIService1

{

[OperationContract]

double Multiply(double x, double y);

[OperationContract]

PersonGetModifiedPerson(Personperson);

}

[DataContract]

publicclassPerson

{

stringname;

...

[DataMember]

publicstringName

{

get { return name; }

set { name= value; }

}

...

}

Service1.svc -> Service1.svc.cs

publicclassService1 : IService1

{

publicdouble Multiply(double x, double y)

{

return x * y;

}

publicPersonGetModifiedPerson(Person person);

{

person.Name="blah blah”;

returnperson;

}

}

Default.aspx.cs

// Create an instance of the Person

ServiceReference1.Personperson = newServiceReference1.Person();

person.Name ="Gomer Pyle”;

// Create a reference to an instance of the web service.

ServiceReference1.IService1 service = newServiceReference1.Service1Client();

// Use the service method.

ServiceReference1.Personperson2 = service.GetModifiedPerson(person);

txtMsg.Text = "Webservice modified person: " + person2.Name;

Differences between WCF Service Application and Service Library

  1. Explanation

Source:

A service application includes a website host already setup for you. A service library is a library of services that a host can reference and startup.

If you start with a service library (recommended) you can then choose any host you wish (a windows service, IIS/ASP.NET, or even a console application) and you'd just reference your library from your new host. Choosing a Service Application limits your host to just IIS/ASP.NET (though this might be ok for your purposes, but will limit the protocols you can use).

Edit: Changes in IIS since I wrote this allow for a wider variety of protocols on ASP.NET activated services, so choosing a service application is much less limiting than before.

  1. Explanation: see Source.

Source:

  1. Explanation

Source:

WCF Service application is out right hosted as a web site in IIS where as for WCF Service library, the hosting part needs to be taken care of explicitly.

One of the differences I noticed, is that you can add ADO.NETDataService to WCF Service Application while you cannot do so with WCF Service Library

  1. Explanation: see Source.

Source:

Difference between a Web Service & a WCF Service Application (Optional)

  1. Explanation

Source:

WCF is a replacement for all earlier web service technologies from Microsoft. It also does a lot more than what is traditionally considered as "web services".

WCF "web services" are part of a much broader spectrum of remote communication enabled through WCF. You will get a much higher degree of flexibility and portability doing things in WCF than through traditional ASMX because WCF is designed, from the ground up, to summarize all of the different distributed programming infrastructures offered by Microsoft. An endpoint in WCF can be communicated with just as easily over SOAP/XML as it can over TCP/binary and to change this medium is simply a configuration file mod. In theory, this reduces the amount of new code needed when porting or changing business needs, targets, etc.

ASMX is older than WCF, and anything ASMX can do so can WCF (and more). Basically you can see WCF as trying to logically group together all the different ways of getting two apps to communicate in the world of Microsoft; ASMX was just one of these many ways and so is now grouped under the WCF umbrella of capabilities.

Web Services can be accessed only over HTTP & it works in stateless environment, where WCF is flexible because its services can be hosted in different types of applications. Common scenarios for hosting WCF services are IIS,WAS, Self-hosting, Managed Windows Service.

The major difference is that Web Services UseXmlSerializer. But WCFusesDataContractSerializerwhich is better in Performance as compared toXmlSerializer.

Sources:

1