Thursday, November 26, 2009

WCF Contracts

3 Types of WCF Contracts:


  1. Service contracts: Maps class methods to WSDL services.

  2. Data contracts: Maps .NET classes to XML Schemas.

  3. Message contracts: Maps .NET classes to SOAP messages

I read through the section entitled "Service Contracts" and the message exchanges described follow standard communication patterns:

  • Request-Response (Synchronous and Asynchronous)

  • One-way

  • Duplex communication

I started working with the [DataContract] and [DataMember] attributes, which handle the manner in which model classes are mapped to the XSD ComplexType.

Very cool stuff.

Here's some sample code I wrote:



using System;
using System.ServiceModel;
using System.Runtime.Serialization;

namespace WinCommFoundation
{
[DataContract (Namespace="http://philiptenn.com/wcf", Name="PersonComplexType")]
public class Person
{
[DataMember(Name = "FirstName", Order = 1, IsRequired = true)]
public String FirstName;

[DataMember(Name = "LastName", Order = 2, IsRequired = true)]
public String LastName;

[DataMember(Name = "ID", Order = 0, IsRequired = true)]
public Int64 PersonId;

[DataMember(Name = "Birthday", Order = 3, IsRequired = true)]
public DateTime DateOfBirth;

[DataMember(Name = "LastAccessDate", Order = 4, IsRequired = false)]
public DateTime LastAccessed;

}
[ServiceContract]
public interface IPersonService
{
[OperationContract]
Person GetPerson();
}

public class PersonService : IPersonService
{
public Person GetPerson()
{
Person person = new Person();
person.PersonId = 0L;
person.FirstName = "John";
person.LastName = "Smith";
person.DateOfBirth = DateTime.Parse("1990-10-01");
person.LastAccessed = DateTime.Now;
return person;
}
}

public class Service
{
public static void Main(string[] args)
{
ServiceHost serviceHost = new ServiceHost(typeof(PersonService));
serviceHost.Open();

Console.WriteLine("Running PersonService, hit any key to end.");
Console.ReadLine();
serviceHost.Close();
}
}
}


And here is the corresponding App.config file:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WinCommFoundation.PersonService" behaviorConfiguration="personServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/wcfTest"/>
</baseAddresses>
</host>
<endpoint address=""
binding="basicHttpBinding"
contract="WinCommFoundation.IPersonService" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>

<behaviors>
<serviceBehaviors>
<behavior name="personServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>


This created the following XSD:


<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" targetNamespace="http://philiptenn.com/wcf" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://philiptenn.com/wcf">
<xs:complexType name="PersonComplexType">
<xs:sequence>
<xs:element name="ID" type="xs:long" />
<xs:element name="FirstName" nillable="true" type="xs:string" />
<xs:element name="LastName" nillable="true" type="xs:string" />
<xs:element name="Birthday" type="xs:dateTime" />
<xs:element minOccurs="0" name="LastAccessDate" type="xs:dateTime" />
</xs:sequence>
</xs:complexType>
<xs:element name="PersonComplexType" nillable="true" type="tns:PersonComplexType" />
</xs:schema>

No comments:

Post a Comment