Skip to Content
Programming WCF Services
book

Programming WCF Services

by Juval Lowy
February 2007
Intermediate to advanced
634 pages
16h 1m
English
O'Reilly Media, Inc.
Content preview from Programming WCF Services

Service Synchronization Context

The programming techniques showed so far put the onus of accessing the resource on the correct thread squarely on the service or resource developer. It would be preferable if the service had a way of associating itself with a particular synchronization context, and then have WCF detect that context and automatically marshal the call from the worker thread to the service synchronization context. WCF lets you do just that. You can instruct WCF to maintain affinity between all service instances from a participle host and a specific synchronization context. The ServiceBehavior attribute offers the UseSynchronizationContext Boolean property, defined as:

[AttributeUsage(AttributeTargets.Class)]
public sealed class ServiceBehaviorAttribute : ...
{
   public bool UseSynchronizationContext
   {get;set;}
   //More members
}

Affinity between the service type, its host, and a synchronization context is locked in when the host is opened. If the thread opening the host has a synchronization context and UseSynchronizationContext is true, WCF will establish an affinity between that synchronization context and all instances of the service hosted by that host. WCF will automatically marshal all incoming calls to the service’s synchronization context. All the thread-specific information stored in the TLS, such as the client’s transaction or the security information (discussed in Chapter 10) will be marshaled correctly to the synchronization context.

If UseSynchronizationContext ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming WCF Services, 2nd Edition

Programming WCF Services, 2nd Edition

Juval Lowy
Pro WCF: Practical Microsoft SOA Implementation

Pro WCF: Practical Microsoft SOA Implementation

Chris Peiris, Dennis Mulder, Shawn Cicoria, Amit Bahree, Nishith Pathak
Mastering ASP.NET Web API

Mastering ASP.NET Web API

Mithun Pattankar, Malendra Hurbuns

Publisher Resources

ISBN: 0596526997Supplemental ContentErrata Page