Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

#111

Get More Refcardz! Visit refcardz.com

CONTENTS INCLUDE:
About WCF
Getting Started with
n

n
Configuration Overview
WCF Contracts
Windows Communication Foundation 4.0
n

n
Bindings
n
Behaviors
Hosted Services and more...
By Scott Seely
n

diagnostics Diagnostic settings for WCF, including WMI, performance


ABOUT WCF counter, message filters, and other settings.

extensions Defines configuration extensions for the bindings, behaviors,


Since .NET 3.0, Windows Communication Foundation (WCF) is the bindingElement (for extending CustomBinding configuration),
and standardEndpoints.
preferred messaging system in .NET applications. It provides an
abstraction layer over transports. This abstraction allows developers machineSettings Allows the user to log personally identifiable information
to focus on the types of messages their applications need to send in traces and message logs via the XML attribute,
enableLoggingKnownPii. This section can only be edited in
and receive. It removes the need for developers to have intimate machine.config, located in the framework directory.
knowledge of how the messages themselves are sent and received.
protocolMapping Used to map a protocol to a binding for easier configuration.
Instead, developers focus on a concept called an endpoint which serviceHostingEnvironment Used to configure services hosted in ASP.NET.
listens for and receives messages. Endpoints are built using three
services Defines which contracts a service instance will listen for
items: Address, Binding, and Contract. messages on and any base addresses the service will use. A
service may also add metadata and discovery endpoints via this
The address defines where a message is sent. mechanism.
http://www.dzone.com/ is an address.
standardEndpoints Contains configuration for endpoints specified by the kind
attribute on any service endpoint configuration.
www.dzone.com

A binding describes how to send the message. The binding


contains information such as the transport to use, how to encode routing Provides message routes that a routing service uses to move
messages closer to its final destination.
messages, and security requirements.
tracking Defines tracking settings for a workflow service.
A contract defines how a message is structured. The contract
defines the message exchange pattern (MEP) used for each
exchange as well as which messages initiate a conversation or stop a WCF CONTRACTS
conversation. All message exchanges fall into one of three message
exchange patterns, or MEPs: one-way, request-response, and
duplex. One-way messages can act as event notifications. Request- WCF developers create two types of contracts: service contracts
response messages send some data and expect a response in a and data contracts. Service contracts inform the WCF runtime how
particular format. Duplex messages are a conversation. In duplex, to read, write, and dispatch received messages. Data contracts
when one caller initiates a conversation, that caller promises to inform the serialization infrastructure how to translate CLR objects
make a set of one-way and request-response messages available to to and from an XML Infoset representation.
the callee. Typically, duplex MEPs make use of sessions as well.
Service Contracts
Getting Started with Windows Communication Foundation 4.0

The WCF runtime creates the infrastructure to host services and


CONFIGURATION OVERVIEW dispatch messages. Developers declare the pieces of infrastructure
they need by marking up classes with attributes. The WCF runtime
Configuration is a big feature in WCF. It permeates much of the then uses this information to allow for communication with callers
messaging framework. This section provides a map of configuration. and the hosted classes. This information is also shared via Web
The configuration section group, <system.configuration>, Services Description Language (WSDL) that WCF services can
contains all WCF configuration settings. Within this configuration generate. To declare a class or interface represents the contract for
section group, 14 different configuration sections exist. Some a WCF service, mark the class with [ServiceContract].
of those sections have relationships with other sections. One,
system.serviceModel/extensions, is a configuration section for
configuration only!

behaviors Contains configuration for shared behaviors that can be applied


Get over 90 DZone Refcardz
bindings
to endpoints and services.

Contains information for shared bindings used to create


FREE from Refcardz.com!
message processing pipelines. A computer program that can
run independently and can propagate a complete working
version of itself onto other hosts on a network.

client Contains information for clients to communicate with services.


Lookups are performed using a two part key: the name of the
configuration and the name of the contract.

comContracts An integration point used between COM+ and WCF to


configure the name and namespace for any hosted services.
This mimics the functionality normally provided by the
[ServiceContract] attribute via the Name and Namespace
properties.

commonBehaviors Defines a collection of behaviors applied to all services and


endpoints. These behaviors add to any others already present
on a service.

DZone, Inc. | www.dzone.com


2
Getting Started with Windows Communication Foundation 4.0

[System.ServiceModel.ServiceContract] The commonly set properties on [DataContract] are:


public interface IMyService {
}
Namespace Defines the URL used as the targetNamespace in the XSD describing the
type and used when serializing the data contract as an XML Infoset.
Through [ServiceContract], you will normally set the following
Name Defines the name of the item when read and written to an XML Infoset.
properties about the collection of operations on the service:
The commonly set properties on [DataMember] are:
Namespace Sets the default namespace for the XML Schema Documents (XSD) for the
request and response messages. Default is http://tempuri.org.
Name Defines the name of the item when read and written to an XML Infoset.
SessionMode One of three values: Allowed, Required, NotAllowed. By default,
IsRequired Set to true if the field must be present when the type is deserialized.
SessionMode is Allowed. If your contract requires session semantics,
set this value to System.ServiceModel.SessionMode.Required. If your Default value is false.
contract will fail with sessions, set it to System.ServiceModel.SessionMode.
NotAllowed. EmitDefaultValue Indicates whether or not to write the member when set to the default
value. The default value of this member is true.

CallbackContract CallbackContract: If the contract implements a duplex MEP, set this to the Order An integer used to alter the ordering of values when read or written.
interface representing the other side of the duplex conversation. By default, values are read and written in alphabetic order. The default
value of Order is 0. Members with the same Order value are serialized
To make methods visible to external callers, mark the methods with alphabetically. Order is sorted ascending.

[OperationContract].

[System.ServiceModel.ServiceContract(
Namespace = “http://www.dzone.com/WCF”)]
BINDINGS
public interface IMyService
{
[System.ServiceModel.OperationContract] WCF supports messaging through a messaging pipeline. The
string SayHi(string name);
} pipeline itself must have stages that represent the transport and
the serialization mechanism. In WCF, the pipeline is created by a
The commonly set properties on [OperationContract] are: type derived from System.ServiceModel.Channels.Binding and the
stages are created by types derived from System.ServiceModel.
IsOneWay Can only be set on methods that return void. Use this to indicate that the Channel.BindingElement. Besides the transport and serialization
method does not send a response. Default is false.
mechanism, the BindingElement also adds stages for security,
IsInitiating Use this to state that a given method can be called to instantiate a new reliability, and transactions. When creating a Binding, the
service. Default value is true.
developer of the Binding decides how to surface the underlying
IsTerminating Use this to state that when a given method is called, the current instance can BindingElement properties. Some settings will be easily set, others
be disposed. Default value is false. If this value is set to true, you must also set
ServiceContractAttribute.SessionMode to SessionMode.Required. made private, and still others surfaced as different concepts.

Your code may also return exceptions to callers. In SOAP messaging, Every binding has settings for timeouts: OpenTimeout,
errors are returned as Fault messages. A given operation may ReceiveTimeout, CloseTimeout. These set the amount of time the
return zero or more faults. One declares the types of faults being user of the Binding will wait for the pipeline to Open, Receive a
returned through [FaultContract]. [FaultContract] has a constructor message, or Close. A Binding also identifies its URL scheme, which
that accepts a type describing what the fault details will look like. is the scheme used by its transport. WCF ships with Bindings
This description is used by the callers to read any faults your service supporting the following schemes: http, https, net.msmq, net.pipe,
might return. net.p2p, and net.tcp. WCF also supports soap.udp, but only in an
internal class used to support WS-Discovery.
[System.ServiceModel.ServiceContract(
Namespace = “http://www.dzone.com/WCF”)]
public interface IMyService
{
Each of the Bindings has a common set of properties
[System.ServiceModel.OperationContract()] not required by the base class, Binding:
[FaultContract(typeof(FaultDetails))]
string SayHi(string name);
}
EnvelopeVersion Sets the version of any SOAP Envelope. Normally, this is set to None,
Soap11, Soap12. Use None for REST or other non-SOAP messaging;
Data Contracts Soap11 to send a SOAP 1.1 envelope and related headers; Soap12 to
WCF reads and writes objects to and from different formats using send a SOAP 1.2 envelope and related headers.
serialization (write) and deserialization (read). WCF supports
MaxBufferPoolSize Most messages are received into memory. The buffer pool is used to
serialization through several mechanisms: System.Xml.Serialization,
allocate memory for receiving messages. By default, each buffer in the
System.ISerializable, [System.Serializable], and through System. pool is 65536 bytes. Adjust this value to change the size of the individual
Runtime.Serialization. The first three mechanisms exist to support buffers.
legacy code. When developing your own declarations, you will
ReaderQuotas This sets quotas on reading inbound messages. The type,
normally use System.Runtime.Serialization to describe your data System.Xml.XmlDictionaryReaderQuotas is found in
contracts. In WCF 4.0, an unattributed type will automatically read System.Runtime.Serialization. The quotas set limits which limit the
and write any public fields or properties using the name of the damage a denial of service attack or poorly formed XML document
property in the generated format. To control what gets written, mark can do. By default, arrays can be no longer that 16384 elements
the type with [DataContract] and any members to be written with (MaxArrayLength), strings must be less than 8192 characters
(MaxStringContentLength), and XML nodes must be no more than 32
[DataMember].
levels deep (MaxDepth).

[System.Runtime.Serialization.DataContract] Scheme Identifies the scheme used by the underlying transport protocol.
public class Name
{
[System.Runtime.Serialization.DataMember]
public string FirstName { get; set; } Depending on which protocol you want to use, you have different
bindings available. Most of the bindings in WCF support HTTP.
[System.Runtime.Serialization.DataMember]
public string LastName { get; set; } The rest of the supported protocols are represented by one
}
binding. The following table shows which bindings support
When you explicitly mark a member with [DataMember], that duplex communications, message level security, reliable
field will be read and written regardless if the member is private, messaging, flowing transactions, and workflow context. Transport
protected, or public. level security is available through all bindings in WCF.

DZone, Inc. | www.dzone.com


3
Getting Started with Windows Communication Foundation 4.0

Binding Duplex Message level Reliable Transactions [ServiceBehavior] contains several properties. The most commonly
security used are:
BasicHttpBinding *
ConcurrencyMode Controls the internal threading model to enable support
BasicHttpContextBinding *
for reentrant or multithreaded services. By default,
MsmqIntegrationBinding concurrency is single threaded per instance.

NetMsmqBinding  InstanceContextMode Controls how instances are created for the service. By
NetNamedPipeBinding    default, each session gets a new service instance. Set
this property to PerSCall to allow for a new instance for
NetPeerTcpBinding   every method call. Set this property to Single if you need
singleton behavior for the service.
NetTcpBinding    

NetTcpContextBinding     IncludeExceptionDetailsInFaults Returns exception details to clients when debugging


services. This property requires a request-response or
WS2007FederationHttpBinding    duplex capable binding.
WS2007HttpBinding   

WSDualHttpBinding     [DeliveryRequirements], a contract behavior, allows a contract to


specify information about reliable messaging requirements imposed
WSFederationHttpBinding   
on the service delivery.
WSHttpBinding   

WSHttpContextBinding    [CallbackBehavior], an endpoint behavior, provides configuration


settings for the callback contract on a duplex service. The settings
* The BasicHttpBinding and BasicHttpContextBinding will sign the are the same as the [ServicBehavior] except for settings for
timestamp on the message, but not sign and encrypt the body. For instancing and transactions.
full message level signing and encryption, use WSHttpBinding and In configuration, you can declare information about security,
WSHttpContextBinding. diagnostics, discovery, and throttling.

Bindings appear in configuration in the system.serviceModel/ WCF contains many configuration elements. Here, we focus on
bindings section under configuration that matches a camelCased the commonly configured behaviors for services. The following
version of the binding name. For example, the collection behaviors allow you to configure your service behaviors as
of WSHttpBinding can be found in system.serviceModel/
part of configuration within system.serviceModel/behaviors/
bindings/wsHttpBinding. Each configured binding has a
serviceBehaviors/:
name. A binding without a name becomes the default set of
settings for that particular binding. For example, to create a
reliable WSHttpBinding, use the following configuration: serviceCredentials Allows the service to pick how it authenticates itself using X.509,
Windows, username/password, and other valid tokens.

<system.serviceModel> serviceDebug Allows you to set up the HttpHelp page and to include exception
<bindings> details in faults.
<wsHttpBinding>
<binding name=”reliable”> serviceDiscovery Enables the WS-Discovery endpoint.
<reliableSession enabled=”true”/>
</binding> serviceMetadata Enables the WS-MetadataExchange endpoint.
</wsHttpBinding>
</bindings>
</system.serviceModel>
On an endpoint, you can configure other behaviors. The following
commonly used behaviors allow you to configure your endpoint
behaviors as part of configuration within system.serviceModel/
BEHAVIORS behaviors/endpointBehaviors/:

Behaviors influence the hosting environment for a service. They callbackDebug Allows you to state if a callback contract should include exception details
are used to handle instancing, expose metadata, enhance in faults.
discoverability, and more. There are four types of behaviors: service, clientCredentials Allows the client to pick how it authenticates itself using X.509, Windows,
contract, endpoint, and operation. All behaviors may be applied username/password, and other valid tokens.
in code. Service and endpoint behaviors may also be applied by enableWebScript Enables a Web Script endpoint on the service. This allows the service
configuration. (Note: There is no notion of contract configuration to return JavaScript when the URL end in /js or /jsdebug. This behavior
anywhere in WCF.) Service and contract behaviors are normally is automatically included if using the System.ServiceModel.Activation.
applied via attributes in code. WebScriptServiceHostFactory on a .SVC.

webHttp Allows the endpoint to dispatch messages based on URL. This behavior
A service behavior applies to a service instance and may alter is automatically included if using the System.ServiceModel.Activation.
aspects of the service, all endpoints, and all contracts. A contract WebScriptServiceHostFactory or System.ServiceModel.Activation.
behavior applies to a contract and all implementations of the WebServiceHostFactory as the factory name on a .SVC.
contract. Endpoint behaviors apply to a specific endpoint instances.
Finally, operation behaviors apply to specific operations.
HOSTING SERVICES
Attribute-based Behaviors
There are two service behaviors: WCF services can listen for messages anywhere: Windows
[AspNetCompatibilityRequirements] and [ServiceBehavior]. Services, desktop applications, and Internet Information Services
[AspNetCompatibilityRequirements] has a single property, (IIS). All environments use the same configuration elements. In
RequirementsMode, that allows a service to declare that ASP all cases, an instance of a ServiceHost will be used to reflect over
features such as identity impersonation are Required, Allowed, or a service implementation to determine service requirements.
NotAllowed (default). When set to Required, configuration must The ServiceHost then marries the code with any configuration
enable compatibility too: to produce an entity that can listen for, dispatch, and respond
to messages that arrive over the various transports.
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled=”true”/>
</system.serviceModel>
To demonstrate hosting, we use the following contract:

DZone, Inc. | www.dzone.com


4
Getting Started with Windows Communication Foundation 4.0

namespace DZone.Contracts private ServiceHost _host;


{
[System.ServiceModel.ServiceContract( protected override void OnStart(string[] args)
Namespace = “http://www.dzone.com/WCF”)] {
public interface IMyService ThreadPool.QueueUserWorkItem(StartListening, this);
{ }
[System.ServiceModel.OperationContract]
string SayHi(string name); static void StartListening(object state)
} {
} var service = state as DemoService;
Implemented by the following class: if (service != null)
namespace DZone.Services {
{ service._host = new ServiceHost(typeof(MyService));
public class MyService : DZone.Contracts.IMyService service._host.Faulted +=
{ (s, e) =>
public string SayHi(string name) {
{ service.StopListening();
return string.Format(“Console: Hello, {0}”, name); service._host = null;
} StartListening(service);
} };
} }
}

and use the following configuration: protected override void OnStop()


{
StopListening();
}
<configuration>
<system.serviceModel> void StopListening()
<behaviors> {
<serviceBehaviors> try
<behavior name=”MyServiceBehavior”> {
<serviceMetadata httpGetEnabled=”true”/> ((IDisposable)_host).Dispose();
</behavior> }
</serviceBehaviors> catch (CommunicationObjectFaultedException ex)
</behaviors> {
<services> // TODO: add code to log
<service name=”DZone.Services.MyService” }
behaviorConfiguration=”MyServiceBehavior”> }
<host>
<baseAddresses>
<add baseAddress=”http://localhost/Demo”/>
</baseAddresses>
The preceding code executes the initialization logic on a
</host> separate thread via ThreadPool.QueueUserWorkItem. The
<endpoint contract=”DZone.Contracts.IMyService”
binding=”basicHttpBinding” address=”/MyService”/> ServiceHost will run for a long time. If the ServiceHost gets
<endpoint kind=”mexEndpoint” address=”/mex” into a situation where it can no longer listen for messages by
binding=”mexHttpBinding” />
</service> entering the Faulted state, the code should start up a new
</services>
</system.serviceModel>
instance of the ServiceHost. When the service is done listening,
</configuration> a CommunicationObjectFaultedException may be thrown on
Dispose. Because this exception is expected, the code will log
The configuration declares that there is a serviceBehavior the exception. When the service stops, we call StopListening. If
named MyServiceBehavior which supports metadata exchange. you want to support Pause and Resume, add the following:
This behavior is attached to a service instance whose name is
DZone.Services.MyService and matches the name of the service protected override void OnPause()
implementation. The host has a base address of http://localhost/ {
StopListening();
Demo. This base address is used for any bindings that support the base.OnPause();
http scheme. The service exposes two endpoints. One endpoint }

exposes an implementation of the DZone.Contracts.IMyService protected override void OnContinue()


{
contract listening off the http base address at /MyService. The other ThreadPool.QueueUserWorkItem(StartListening, this);
endpoint hosts metadata exchange using a predefined binding base.OnContinue();
}
named mexHttpBinding listening off the http base address at /Mex.
Hosting in IIS
Hosting in a Console/GUI application
To host a service in IIS, you create a .svc file which is
Using this configuration, we can host the service in a
hosted at a path of your choosing in your web application.
Console application with the following code:
That will give you a file that looks like this:

var host = new ServiceHost(typeof(MyService))


<%@ ServiceHost Service=”DZone.Services.MyService” %>
host.Open();
Console.WriteLine(“Press [Enter] to exit”);
Console.ReadLine(); The config remains largely the same. IIS will automatically set
try
the http base address for the host to be the address of the
{ .svc file.
((IDisposable)host).Dispose();
}
catch (CommunicationObjectFaultedException ex) In IIS 7 and later, you can also host services with NetTcpBinding and
{
// TODO: add code to log NetNamedPipeBinding. To do this, run the following commands:
}

TCP Activation:
Once the using block exits, host.Dispose() is called. You
can do something similar in a WinForm/WPF application by 1. Run the following command (on one line):
opening the service on window Load and explicitly calling
ServiceHost.Dispose() when the window is closed/unloaded. %windir%\system32\inetsrv\appcmd.exe set site “Default Web Site” -+bindings.
[protocol=’net.tcp’,bindingInformation=’808:*’]
Hosting in a Windows Service
A Windows Service requires you to be able to respond to 2. Run the following command to enable the http and net. pipe
Start and Resume events very quickly. In order to do this, you protocol on your site (on one line):
do not want to block in the start if at all possible. Creating a
single WCF ServiceHost does not normally take much time. %windir%\system32\inetsrv\appcmd.exe set app
However, we should always be prepared for things to take a “Default Web Site/[your v-dir]” /enabledProtocols:http,net.tcp
while and have the services behave nicely. To host the same
WCF service in a Windows Service, write the following code: Named Pipe Activation

DZone, Inc. | www.dzone.com


5
Getting Started with Windows Communication Foundation 4.0

1. Run the following command (on one line): var client = new DZoneService.MyServiceClient(
“BasicHttpBinding_IMyService”);
try
{
%windir%\system32\inetsrv\appcmd.exe set site “Default Web Site”
Console.WriteLine(client.SayHi(“DZone”));
-+bindings.[protocol=’net.pipe’,bindingInformation=’*’]
}
catch (TimeoutException timeoutException)
{
2. Run the following command to enable the http and net. pipe client.Abort();
protocol on your site (on one line): }
catch (FaultException<KnownFaultType> faultException)
{
client.Abort();
%windir%\system32\inetsrv\appcmd.exe set app }
“Default Web Site/[your v-dir]” /enabledProtocols:http,net.pipe catch (FaultException faultException)
{
client.Abort();
With Vista SP1 and Server 2008, you can also enable these protocols }
catch (CommunicationException communicationException)
in the IIS Manager. {
client.Abort();
}
For the demo application, remove the <host> base
addresses and add in these two endpoints:
DIAGNOSTICS
<endpoint contract=”DZone.Contracts.IMyService” address=”/MyService”
binding=”netTcpBinding” />
<endpoint contract=”DZone.Contracts.IMyService” address=”/MyService”
binding=”netNamedPipeBinding” /> WCF also comes with a rich set of diagnostics information.
Unlike other aspects of WCF, diagnostics can only be set in
configuration. The diagnostics allows for tracing, message
OperationContext
logging, WMI inspection, and performance counters.
Every incoming message is associated with an OperationContext.
Think of OperationContext as WCF’s version of HttpContext.
OperationContext.Current yields the current operation context
Tracing/WMI
All production applications should be deployed with System.
with pointers to the following commonly used properties:
ServiceModel tracing configured but turned off. WMI
should be enabled so that an administrator can enable
IncomingMessageHeaders Headers on the incoming message. tracing through WMI. The configuration looks like this:
OutgoingMessageHeaders Can use this to add more headers to the response.
<system.diagnostics>
IncomingMessageProperties The message properties serve as a mechanism to send <sources>
information in between layers within the message processing <source name=”System.ServiceModel”
pipeline about a specific message. switchValue=”Off”
propagateActivity=”true”>
<listeners>
ServiceSecurityContext Gain access to the identity of the currently logged in user. <add name=”ServiceModelTraceListener” />
Also available through ServiceSecurityContext.Current. </listeners>
Contains a property, AuthorizationContext, where you can </source>
</sources>
investigate the ClaimSets for the current user. <sharedListeners>
<add initializeData=”.\logs\web_tracelog.svclog”
type=”System.Diagnostics.XmlWriterTraceListener, System,
The OperationContext also has two often used methods: Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”
name=”ServiceModelTraceListener”
SetTransactionComplete() Allows the service to complete a transaction in code instead traceOutputOptions=”Timestamp” />
</sharedListeners>
of automatically on exit. </system.diagnostics>
<system.serviceModel>
GetCallbackChannel<T>() For duplex services, allows the service to send messages <diagnostics wmiProviderEnabled=”true” />
back to the service. </system.serviceModel>

If, in production, something goes wrong, the administrator


can run the following Powershell commands to update
CONSUMING SERVICES the switchValue, capture data, change the switchValue
back to Off, and send the traces off to development:
When consuming services, you will typically create a proxy via
the Visual Studio ‘Add Service Reference’ tool. Enter the WSDL $appDomain = Get-WmiObject -Namespace root\ServiceModel
AppDomainInfo
or MetadataExchange endpoint for the service, typically just the
address of the .svc file for IIS hosted services, and then pick out the $appDomain.TraceLevel = “Verbose, ActivityTracing”
service. Visual Studio will do the rest of the work, including adding $appDomain.put()
configuration. The developer may need to edit the application
configuration file (named [app name].exe.config if the client is Note: the line to get the WMI object will change depending
an executable or web.config if the client is a web application) to on how many AppDomains hosting WCF endpoints are
enter in security credentials, but otherwise the work is done. running on the computer. Each AppDomainInfo has a
corresponding ProcessID and AppDomainId to help you pick
When consuming the previous service, write the following code, the right instance. Once that is done, you can set the trace
catching the TimeoutException, CommunicationException, level as above and call put() to save the data. Above turns on
and FaultException since any of these may be returned. For ActivityTracing which allows WCF to assign IDs to related traces
both the CommunicationException and TimeoutException, and show how one group of activity flows from another. Use
the code may want to have some retry logic built in to get a SvcTraceViewer.exe (part of the Windows SDK and ships as a
new client instance and try again up to n times, logging the part of Visual Studio) to view and interpret the traces. When the
exception each time. In all cases, the code should Abort the administrator is done collecting data, turn tracing back off:
connection to release all resources on the client machine.
$appDomain.TraceLevel = “Off”
When the configuration is generated for the client, the endpoints
will have names like binding_Contract. For IMyService, the $appDomain.put()
client endpoint is named BasicHttpBinding_IMyService
in configuration. The endpoint can then be created by Note that changes to the trace level do not get saved to the
instantiating the generated proxy using the configured name. configuration file as this would cause the AppDomain to restart
A call to a IMyService implementation looks like this: on IIS.

DZone, Inc. | www.dzone.com


6
Getting Started with Windows Communication Foundation 4.0

With WMI on, you can also inspect all the running endpoints <diagnostics>
including binding data, behaviors, and many other items. <messageLogging
logMessagesAtTransportLevel=”true”
logMessagesAtServiceLevel=”true” />
Message Logging </diagnostics>
Message logging is enabled in two places. First, create the message
logging trace source. If you are logging and tracing, you can get Typically, you will log messages at the transport level to see an
all the traces into one file with the following configuration: XML 1.0 text representation of what the ServiceModel saw including
message headers added by the message pipeline. You log at the
<system.diagnostics> service level to see what the message looked like after the message
<sources>
<source name=”System.ServiceModel” is decrypted.
switchValue=”Off”
propagateActivity=”true”>
<listeners> Performance Counters
<add name=”ServiceModelTraceListener” /> To enable performance counters, viewed in tools like PerfMon,
</listeners>
</source>
you add the following configuration to system.serviceModel:
<source name=”System.ServiceModel.MessageLogging”
switchValue=”Off”
propagateActivity=”true”> <diagnostics performanceCounters=”ServiceOnly” />
<listeners>
<add name=”ServiceModelTraceListener” />
</listeners> Performance Counters can have one of four values:
</source>
</sources>
<sharedListeners>
Default WCF_Admin is created and SQM data is collected. The values
<add initializeData=”.\logs\web_tracelog.svclog”
type=”System.Diagnostics.XmlWriterTraceListener, System, are never updated.
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”
name=”ServiceModelTraceListener” Off Performance counters are disabled.
traceOutputOptions=”Timestamp” />
</sharedListeners> ServiceOnly Only service level counters are enabled.
</system.diagnostics>
All Counters for the service, endpoints, and operations
are created.
In the ServiceModel configuration, you can set what you want to log:

ABOUT THE AUTHOR RECOMMENDED BOOK


Scott Seely is the author of several books on Web Services Essential Windows Communication Foundation
and an instructor for Pluralsight. He is a Microsoft Regional For .NET Framework 3.5
Director. Right now, he is working on Essential Windows Written by three experts at the Microsoft Technology
Communication Foundation, 2nd Edition for Addison Wesley. Center in Boston, this guide answers developers’
He helped found the Chicago Code Camp with several other questions about WCF. Throughout the book’s 13
local developers. He helps run the Lake County .NET Users’ chapters, authors Steve Resnick, Richard Crane, and
Group with Tim Stall. Throughout the year, Scott can be found Chris Bowen offer best practices, key advice, tips,
speaking at the user groups throughout northern Illinois and Wisconsin. From and problem-solving tricks. They solve developers’
2000 through 2006, Scott worked for Microsoft. He spent his first two years as problems with WCF through in-depth explanations
a developer/writer for MSDN and then moved over to the Indigo/WCF team and an extensive amount of code samples.
as a developer. Scott is also founder of Friseton, LLC. Friseton specializes in
creating highly scalable parallelized systems, solving application performance BUY NOW
problems, and building distributed applications that utilize REST and WS-*. books.dzone.com/books/essential-wcf

#82

Browse our collection of 100 Free Cheat Sheets


Get More Refcardz! Visit refcardz.com

CONTENTS INCLUDE:


About Cloud Computing
Usage Scenarios Getting Started with

Aldon Cloud#64Computing

Underlying Concepts
Cost
by...

Upcoming Refcardz
youTechnologies ®

Data
t toTier
brough Comply.
borate.
Platform Management and more...

Chan
ge. Colla By Daniel Rubio

tion:
dz. com

also minimizes the need to make design changes to support


CON

tegra ternvasll
ABOUT CLOUD COMPUTING one time events. TEN TS
INC ■
HTML LUD E:

us Ind Anti-PPaat
Basics
Automated growthHTM
ref car

Web applications have always been deployed on servers & scalable


L vs XHT technologies

nuorn

Valid
ation one time events, cloud ML
connected to what is now deemed the ‘cloud’. Having the capability to support

Apache Ant
Usef
Du
ti

ul M.
computing platforms alsoulfacilitate
#84
Open the gradual growth curves

n an
Page Source

o

s
Vis it

However, the demands and technology used on such servers Structure

C
faced by web applications. Tools

Core
By Key ■
Elem

Patte
has changed substantially in recent years, especially with Structur
E: al Elem ents
INC LUD gration the entrance of service providers like Amazon, Google and Large scale growth scenarios involvingents
specialized
NTS and mor equipment
rdz !

ous Inte Change

HTML
CO NTE Microsoft. es e... away by
(e.g. load balancers and clusters) are all but abstracted
Continu at Every e chang
m

About ns to isolat
relying on a cloud computing platform’s technology.
Software i-patter
space

Hadoop
rdz .co


n
Re fca

e Work
Build
riptio
and Ant
Desc
These companies have a Privat
are in long deployed
trol repos
itory
webmana applications
ge HTM
L BAS

Patterns Control lop softw n-con to



that adapt and scale
Deve
les toto large user
a versio ing and making them
bases, In addition, several cloud computing ICSplatforms support data
ment ize merg
rn
Version e... Patte it all fi minim le HTM
Manage s and mor e Work knowledgeable in amany ine to
mainl aspects related tos multip
cloud computing. tier technologies that Lexceed the precedent set by Relational
space Comm and XHT

ref ca

Build
re

Privat lop on that utilize HTML MLReduce,


Practice Database Systems (RDBMS): is usedMap are web service APIs,

Deve code lines a system
d as thescalethe By An
sitory of work prog foundati
Ge t Mo

Buil Repo
This Refcard active
will introduce are within
to you to cloud riente computing, with an
d units
RATION etc. Some platforms ram support large grapRDBMS deployments.

The src
dy Ha
softw
e ine loping and Java s written in hical on of
INTEG attribute
task-o it all
softwar emphasis onDeve es by
Mainl these
ines providers, so youComm can better understand
also rece JavaScri user interfac web develop and the rris
Vis it

Spring Security
codel chang desc
INUOU ding Task Level as the
e code
w ww.dzone.com

NT of buil trol what it is a cloudnize


line Policy sourc es as aplatform
computing can offer your ut web output ive data pt. Server-s e in clien ment. the ima alt attribute ribes whe
T CO cess ion con Code Orga it chang e witho likew mec ide lang t-side ge is describe re the ima
ABOU the pro ject’s vers applications. and subm with uniqu
e name
are from
sourc CLOUD COMPUTING ise hanism. fromAND
PLATFORMS web unavaila
was onc use HTML The eme pages and uages like ge file
it
(CI) is Nested s alte
rd z!

a pro evel Comm the build softw um ble. rnate can be


gration ed to Task-L Label ies to
build minim UNDERLYING CONCEPTS e and XHT rging use HTM PHP tags text that
blem activit the bare standard a very loos Tags found,
ous Inte committ to a pro USAGE SCENARIOS ate all
Autom configurat
ion cies to t
ization, ely-defi
ML as
thei
Ajax
tech L can is disp
Continu ry change cannot be (and freq
nden ymen layed
solution fective ned lang r visual eng nologies
Re fca

Label
Build
manu
al
d tool
depe deplo t need but as if
eve , a ) stalle the same nmen for stan overlap uen
(i.e. , inef Build t, use target enviro Amazon EC2: Industry standard it has
software and uagvirtualization ine. HTM , so <a>< tly are) nest
with terns problem ce pre-in whether dards
ated b></
ory. ns (i.e. Autom Redu ymen
has bec become e with a> is
via pat ticular d deploEAR) in each very little L

Subversion
reposit -patter s that Pay only cieswhat you consume
tagge or Amazon’s cloud
the curr you cho platform
computing isome
heavily based moron fine. b></ ed insid
lained ) and anti the par solution duce nden For each (e.g. WAR es t
ent stan ose to writ more e imp a></ e
not lega each othe
x” b> is
be exp text to “fi are al Depeapplication deployment
Web ge until t a
librarifew years agonmen
t enviro was similar that will softwaredard
industry standard and virtualization app
e HTM technology.
orta nt,
tterns to pro Minim packa nden
Mo re

CI can ticular con used can rity all depe all targe
s will L or XHT arent. Reg the HTM l, but r. Tags
etimes Anti-pa they
tend
es, but to most phone services:
y Integ alizeplans with le that
late fialloted resources, ts with an and XHT simplify all help ML, und ardless
L VS
XHTM <a><
in a par hes som
Centr
end,
Binar
pro cess. the practic enti ng incurred costgeme
nt
whether e a single
such
temp
resources on
were consumed
t enviro
nmen
orthenot. Virtualization MLaare your
othe
you prov
erst of L
b></
in muchallows physical piece of hardware to
ide be HTM
rily bad
Mana based
approac ed with the cial, but, implem anding
Creat targe es to actually r web
nden
cy rties are nt
of the a solid L has
into differe coding.resources
necessa pared to
chang
efi Depe prope itting utilized by multiple operating
function systems.simplerThis allows foundati job adm been arou
associat to be ben
er
Ge t

te builds commo
are not Fortuna
late Verifi e comm than on
n com Cloud computing asRun it’sremo
known etoday has changed this.
befor alitytohas irably, nd for
They they
etc.
Temp Build (e.g. bandwidth, n memory, CPU) be allocated exclusively to tely exp that som
lts whe
ually,
appear effects. Privat y, contin Every elem mov used
to be, HTML
ecte job e time
ed resu The various resourcesPerfo rm a
consumed by webperio applications
dicall (e.g. nt team
pag
individual operating entsinstances. ed to CSS
system Brow d. Earl
y HTM has expand . Whi
gration
opme because
adverse unintend d Builds sitory Build r to devel common e (HTML . ser ed far le it has don
ous Inte web dev manufacture L had very
Stage Repo
e bandwidth, memory, CPU) areIntegtallied
ration on a per-unit CI serve basis or XHT
produc tinu e Build rm an from
extensio .) All are more e its
e.com

ard ML shar limit


tern.
ack elopers rs add than
Con Refc Privat
from zero) by Perfo
(starting all majorated cloud ed man ed layout
feedb computing platforms. As a user of Amazon’s esse
term e, this
on
n. HTM EC2 cloud
ntiallycomputing es certplatform, you are result anybody
the pat
occur came
gration as based is
of the ” cycl such Build Send
autom as they builds proc
assigned esso
an operating L system
files shouin the plai
same wayain elem
as on allents
hosting The late a lack of stan up with clev y competi
supp
ous Inte tional use and test concepts
soon n text ort.
ration ors as ion with r
tinu
Integ entat
ld not in st web dar er wor ng stan
ven “build include
docum
Con be cr kar dar
the con s to the standar
oper
to rate devel
While efer of CI Gene
notion

DZone, Inc.
Cloud Computing

the
s on
expand

ISBN-13: 978-1-934238-75-2
140 Preston Executive Dr. ISBN-10: 1-934238-75-9
Suite 100
50795
Cary, NC 27513

DZone communities deliver over 6 million pages each month to 888.678.0399


919.678.0300
more than 3.3 million software developers, architects and decision
Refcardz Feedback Welcome
$7.95

makers. DZone offers something for everyone, including news,


refcardz@dzone.com
tutorials, cheatsheets, blogs, feature articles, source code and more. 9 781934 238752
Sponsorship Opportunities
“DZone is a developer’s dream,” says PC Magazine.
sales@dzone.com
Copyright © 2010 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, Version 1.0
photocopying, or otherwise, without prior written permission of the publisher.

You might also like