Today I passed Exam 70-503: .Microsoft .NET Framework 3.5 - Windows Communication Foundation.
This was the hardest of the exams I've taken thus far in going for the
Microsoft Certified Professional Developer (MCPD): Enterprise Application Developer 3.5.
I probably spent the most time studying for this exam (other than the Fundamentals), and am glad to be done with it.
Now, onto ADO.NET, LINQ, and Entity Framework.
Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts
Tuesday, December 1, 2009
Monday, November 30, 2009
WCF Security
The Thanksgiving break was nice ... pretty much spent all my time (other than family time) studying from my WCF book and reading WCF articles online.
Starting to focus on Security in WCF.
Security in WCF is classified as either Transport-level Security or Message-level Security.
Transport-level Security is known as point-to-point security (also termed "hop-to-hop security" on Microsoft sites), and secures data as it is transferred across the network. It is vulnerable to messages traveling through intermediary, unsecured points between the sender and receiver of the Message.
Message-level Security is known as end-to-end security, and involves securing the message itself, ensuring message privacy and integrity, regardless of the path taken from sender to receiver.
There are 3 Security modes:
Once the security mode has been selected, the developer needs to select a Client Credential Type. The clientCredentialType is an attribute on the transport or message tag under security in the binding. There are different clientCredentialTypes available for transport or message.
Here is an MSDN article on Selecting a Credential Type.
The final step is to set the Client Credential Values.
Security in WCF is classified as either Transport-level Security or Message-level Security.
Transport-level Security is known as point-to-point security (also termed "hop-to-hop security" on Microsoft sites), and secures data as it is transferred across the network. It is vulnerable to messages traveling through intermediary, unsecured points between the sender and receiver of the Message.
Message-level Security is known as end-to-end security, and involves securing the message itself, ensuring message privacy and integrity, regardless of the path taken from sender to receiver.
There are 3 Security modes:
- Transport
- Message
- TransportWithMessageCredential
Once the security mode has been selected, the developer needs to select a Client Credential Type. The clientCredentialType is an attribute on the transport or message tag under security in the binding. There are different clientCredentialTypes available for transport or message.
Here is an MSDN article on Selecting a Credential Type.
The final step is to set the Client Credential Values.
Sunday, November 29, 2009
WCF Hosting
There are several options available when choosing a Service Host for a WCF Service. These include:
Service hosts must do the following:
- IIS
- WAS (Windows Process Activation Services), built into Vista and Windows Server 2008
- Windows Service (starts and stops with the OS)
Service hosts must do the following:
- Instantiate System.ServiceModel.ServiceHost
- Add endpoints to the host
- Start the host listening
- WAS enables hosting of services that do not rely upon HTTP.
- When hosting in IIS, must choose a binding that specifies HTTP as the transport, such as basicHttpBinding, wsHttpBinding, wsDualHttpBinding. Choosing a binding based on a different transport causes an error.
- aspNetCompatibilityEnabled attribute of
must be set to true - AspNetCompatibilityRequirements must be set to Allowed at Service Behavior level.
Friday, November 27, 2009
WCF Study Resources
Links I found while studying:
- ABCs of Programming WCF (MSDN Magazine)
- WCF Federation. This ended up being an important article since my WCF book did not cover federation in any depth.
- WCF Transaction Propagation
- Specifying an Endpoint Address (MSDN). This cleared up a lot of questions about endpoints for me.
- Streaming Message Transfer
WCF Bindings
Bindings are pre-packaged channel stacks. WCF provides 12 bindings:
netNamedPipeBinding is for binary, local machine communication only (and consequently has the highest performance metrics). The address will take the following form: net.pipe://localhost/{service}.
netTcpBinding addresses will take the format: net.tcp://{hostname}.
netMsmqBinding addresses will take the form: net.msmq://{hostname}/[private/public]/{queueName}. The default port is 1801 and cannot be changed.
- basicHttpBinding - WS-I Basic Profile, and ASMX Web Services
- wsHttpBinding - advanced WS-* Web Services, WS-Security, WS-Transactions
- wsDualHttpBinding - Duplex Web Services
- webHttpBinding - REST / POX-based Web Services using XML and JSON.
- netTcpBinding - communication between two .NET systems
- netNamedPipeBinding - Communication between one or more .NET Systems.
- netMsmqBinding - Asynch communication via MSMQ
- netPeerTcpBinding - P2P networking applications
- msmqIntegrationBinding - Sending and receiving messages via MSMQ
- wsFederationHttpBinding - Web Services that use Federated Identity
- ws2007HttpBinding - same as wsHttpBinding, with enhanced support for changes in 2007
- ws2007FederationHttpBinding - same as wsFederationHttpBinding, with enhanced support for changes in 2007
netNamedPipeBinding is for binary, local machine communication only (and consequently has the highest performance metrics). The address will take the following form: net.pipe://localhost/{service}.
netTcpBinding addresses will take the format: net.tcp://{hostname}.
netMsmqBinding addresses will take the form: net.msmq://{hostname}/[private/public]/{queueName}. The default port is 1801 and cannot be changed.
WCF Channels
WCF Applications communicate by sending messages across channels.
There are two types of channels: Transport Channels and Protocol Channels. Transport channels sit at the lowest layer and provide the actual transport for messages. Protocol channels provide security, transactions, sessions, and message reliability.
Channels are layered upon one another, to create a channel stack. Bindings represent channel stack configurations, and simply the process of creating and referencing channel stacks.
The 3 communication patterns:
Channel listeners can be closed without closing down the channel on which they were listening.
Channel factories are responsible for closing down their channels.
ICommunication object is the basis of all communication objects in WCF, which includes channels, channel factories, and channel listeners. Communication objects go through states: Created, Opening, Opened, Closing, Closed, and Faulted. Events are thrown when the Communication object transitions between these states.
There are two types of channels: Transport Channels and Protocol Channels. Transport channels sit at the lowest layer and provide the actual transport for messages. Protocol channels provide security, transactions, sessions, and message reliability.
Channels are layered upon one another, to create a channel stack. Bindings represent channel stack configurations, and simply the process of creating and referencing channel stacks.
The 3 communication patterns:
- One-way
- Request-Response
- Duplex
- IOutputChannel (IOutputSessionChannel). Supports one-way communication.
- IInputChannel (IInputSessionChannel). Supports one-way communication.
- IDuplexChannel (IDuplexSessionChannel). Supports duplex communication.
- IRequestChannel (IRequestSessionChannel). Supports request-response communication.
- IResponseChannel (IResponseSessionChannel). Supports request-response communication.
Channel listeners can be closed without closing down the channel on which they were listening.
Channel factories are responsible for closing down their channels.
ICommunication object is the basis of all communication objects in WCF, which includes channels, channel factories, and channel listeners. Communication objects go through states: Created, Opening, Opened, Closing, Closed, and Faulted. Events are thrown when the Communication object transitions between these states.
Thursday, November 26, 2009
WCF Contracts
3 Types of WCF Contracts:
- Service contracts: Maps class methods to WSDL services.
- Data contracts: Maps .NET classes to XML Schemas.
- 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>
Wednesday, November 25, 2009
WCF Client
Visual Studio .NET 2008 provides a GUI interface for "Add Service Reference", which calls to svcutil.exe under the covers.
I created a new Windows Console project in VS.NET 2008 and right clicked on the project and selected "Add Service Reference", and was presented with the following dialog:

This generated the Client Proxy necessary to consume the Service. It actually created a new folder under my project called "Service References", and beneath that, an additional folder for each Service Reference.
Here is the client code that uses the generated proxy.
My thoughts on this:
I created a new Windows Console project in VS.NET 2008 and right clicked on the project and selected "Add Service Reference", and was presented with the following dialog:

This generated the Client Proxy necessary to consume the Service. It actually created a new folder under my project called "Service References", and beneath that, an additional folder for each Service Reference.
Here is the client code that uses the generated proxy.
using System;
using WCFClient.ServiceReference;
namespace WCFClient
{
public class Client
{
public static void Main(string[] args)
{
GreetingServiceClient serviceClient = new GreetingServiceClient();
Console.Write(serviceClient.GetGreeting("Philip", "Tenn"));
serviceClient.Close();
Console.ReadKey();
}
}
}
My thoughts on this:
- Really like being able to choose the namespace of the generated client proxy.
- Creating the client code itself was simple ... just construct an instance of the GreetingServiceClient, and call the methods as if they were any regular class method. The details are hidden under the covers of WCF.
WCF Services
Next up: Exam 70-503: Windows Communication Foundation
Windows Communication Foundation Notes:
WCF was released as part of .NET 3.0, initially code-named Indigo. It is intended to support distributed applications and evolve from past attempts such as Remoting, Web Services, DCOM, and MSMQ.
It is focused on hosting, consuming, securing services to support distributed application.
It was enhanced as part of .NET 3.5 to support integration with Windows Workflow Foundation, as well as Web-based application support.
The "ABCs" of WCF are:
Communication between WCF Clients and Service are done via channels. Messages are passed through the channel. The channel itself is composed of binding elements, which are stacks. The service endpoint provides a contract / interface. The WCF binding will define the channel, and there are built in bindings, such as BasicHttpBinding, WSHttpBinding, NetTcpBinding, and others.
Here is the list and definition of WCF System-Provided Bindings.
I wrote the following as my first WCF Service, based on examples in the book and on the Web.
The code sample above uses programmatic definition of the Service Address URI as well as the Bindings.
However, we could also declaratively define these in a web.config or app.config Configuration file.
Here is an example of an app.config file that provides the address, bindings, and exposes the Metadata Exchange information.
When I open the URL: http://localhost:8000/wcfTest, I see the following in my browser:
Windows Communication Foundation Notes:
WCF was released as part of .NET 3.0, initially code-named Indigo. It is intended to support distributed applications and evolve from past attempts such as Remoting, Web Services, DCOM, and MSMQ.
It is focused on hosting, consuming, securing services to support distributed application.
It was enhanced as part of .NET 3.5 to support integration with Windows Workflow Foundation, as well as Web-based application support.
The "ABCs" of WCF are:
- Address
- Binding
- Contract
Communication between WCF Clients and Service are done via channels. Messages are passed through the channel. The channel itself is composed of binding elements, which are stacks. The service endpoint provides a contract / interface. The WCF binding will define the channel, and there are built in bindings, such as BasicHttpBinding, WSHttpBinding, NetTcpBinding, and others.
Here is the list and definition of WCF System-Provided Bindings.
I wrote the following as my first WCF Service, based on examples in the book and on the Web.
using System;
using System.ServiceModel;
using System.Text;
namespace WCF
{
[ServiceContract]
public interface IGreetingService
{
[OperationContract]
String GetGreeting(String firstName, String lastName);
}
public class GreetingService : IGreetingService
{
public string GetGreeting(String firstName, String lastName)
{
StringBuilder greeting = new StringBuilder();
greeting.Append("Hello ");
greeting.Append(" ");
greeting.Append(firstName);
greeting.Append(" ");
greeting.Append(lastName);
return greeting.ToString();
}
}
public class Service
{
public static void Main(string[] args)
{
ServiceHost serviceHost = new ServiceHost(typeof(GreetingService), new Uri("http://localhost:8000/wcfTest"));
serviceHost.AddServiceEndpoint(typeof (IGreetingService), new BasicHttpBinding(), "");
serviceHost.Open();
Console.WriteLine("Running GreetingService, hit any key to end.");
Console.ReadLine();
serviceHost.Close();
}
}
}
The code sample above uses programmatic definition of the Service Address URI as well as the Bindings.
However, we could also declaratively define these in a web.config or app.config Configuration file.
Here is an example of an app.config file that provides the address, bindings, and exposes the Metadata Exchange information.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WCF.GreetingService" behaviorConfiguration="greetingServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/wcfTest"/>
</baseAddresses>
</host>
<endpoint address=""
binding="basicHttpBinding"
contract="WCF.IGreetingService" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="greetingServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
When I open the URL: http://localhost:8000/wcfTest, I see the following in my browser:
Subscribe to:
Posts (Atom)