Sei sulla pagina 1di 34

Visual Studio® 2008:

Windows®
Communication
Foundation
Module 7: Improving WCF Service Quality
• Managing WCF Service Instances

• Managing Concurrency Issues

• Improving WCF Service Quality

• Improving Data Transfer Throughput


Lesson: Managing WCF Service Instances
• Instance Life Cycle Options

• Creating a WCF Service Instance Per-Call

• Creating a WCF Service Instance Per-Client

• Creating a Single WCF Service Instance

• Requiring Session Capability

• Demonstration: Instance Management


Instance Life Cycle Options
PerSession
Service
Clients Instances

Service
Host

Each model has


different implications
PerCall
for state management
and concurrency.
Single
Creating a Service Instance Per-Call
• New instance created for every call and disposed of afterwards

• As many instances as concurrent calls to the service

• Only ever one client call in the instance

• No state held in instance between calls

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
public class BankService : IBank

Creation, destruction,
Session timeout
data access cycle

Concurrency

Memory and other


server resources
Creating a Service Instance Per-Client
• New service instance created per-client

• As many instances exist as there are clients

• Must to handle concurrent calls from the same client

• In-memory state held in instance between calls

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class BankService : IBank

Creation, destruction,
Session timeout
data access cycle

Concurrency

Memory and other


server resources
Creating a Single Service Instance
• Service instance created when service starts

• Only one single instance of the service exists

• This handles all calls from all clients so concurrency is implicit

• In-memory state held between calls

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class BankService : IBank

Creation, destruction,
Data security
data access cycle

Lots of concurrency

Memory and other


server resources
Requiring Session Capability
• Relates client to service over a session-aware binding

• Specify the SessionMode property on the service contract

Allowed
NotAllowed
Required

[ServiceContract(Namespace="http://myuri.org/Simple",
SessionMode=SessionMode.Required)]
public interface IBank
{
...
}
Demonstration: Instance Management
In this demonstration, you will see how to change the
instance management strategy to change the behavior of
service-side objects and state
Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lesson: Managing Concurrency Issues
• Concurrency Issues

• Concurrency in WCF Services

• Single Threaded Mode

• Reentrant Threaded Mode

• Multiple Threaded Mode


Concurrency Issues
Potential contention with PerSession and Single instancing

Intermittent

Difficult to Subtle
reproduce corruption

Two approaches

Limit the number of threads to a safe number—one

Protect resources from concurrent access


Concurrency in WCF Services
Specify the ConcurrencyMode property for the service behavior

Multiple
Reentrant
Single

Use .NET Framework threading mechanisms to protect


resources

Locks, monitors, mutexes, and interlocking.


Single Threaded Mode
• Single request active at any one time

• Other calls are queued until current request ends

• Limits throughput for PerSession and Single


instancing

[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Single)]
public class BankService : IBank

Safest option
No potential for deadlock
Reentrant Threaded Mode
• Single request thread active at any one time

• Other calls are queued until current thread leaves

• Limits throughput but better than Single threaded


mode

[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant)]
public class BankService : IBank

Semi-safe option
Some potential for deadlock
and corruption
Multiple Threaded Mode
• Multiple request threads active at any one time

• Number of calls limited only by system resources

• Highest throughput option

[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple)]
public class BankService : IBank

Most dangerous option


Lots of potential for deadlock
and corruption
Lesson: Improving WCF Service Quality
• Improving Service Quality

• Throttling Service Access

• Throttling, Instance Management, and Threading

• Improving Service Availability and Throughput

• Impact of WCF Settings on Load-Balancing and Failover


Improving Service Quality

You want to serve as many clients as possible, but…

Everyone ends Limited


up unhappy Resources

Some happy at the Limited


expense of others Resources
Throttling Service Access
Set properties on serviceThrottling service behavior

MaxConcurrentCalls
MaxConcurrentInstances
MaxConcurrentSessions

<serviceBehaviors>
<behavior name="bankServiceBehavior">
<serviceThrottling maxConcurrentCalls="1024"
maxConcurrentInstances="1024"
maxConcurrentSessions="10"/>
</behavior>
</serviceBehaviors>
Throttling, Instance Management, and Threading

Service
Threading
Throttling
Mode

Instancing All of these interact to


Mode restrict calls, sessions
and instances
Improving Service Availability and Throughput

Clustering

Load-balancing Must to avoid


server-affinity
Failover
Impact of WCF Settings on Load-Balancing and Failover
• Ongoing connections work against load-balancing
and failover
• PerSession instance mode

Client must talk to same instance each time

• Transport sessions
Ongoing connection between client and server

• Single instance mode requires a lot of thought in


this scenario
• Safest approach is:

Use per-call instance management

Load/save state to/from a common, persistent store


Lesson: Improving Data Transfer Throughput
• Improving Performance for Large Binary Data

• Understanding MTOM Encoding

• Enabling MTOM Encoding

• Implementing Streaming

• Stream Handling

• Comparing Streaming and MTOM

• Demonstration: Passing Large Binary Data in WCF


Improving Performance for Large Binary Data

Binary data is a problem for SOAP


Base64 encoding makes it large

XML parsers and large blocks of text

WCF offers two alternatives

MTOM encoding Streaming


Understanding MTOM Encoding
Message Transmission Optimization Mechanism (MTOM)

---------------------
MIME Part

SOAP
Envelope

Reference ---------------------
MIME Part

Binary Binary
Binary Data
Data
Data
Enabling MTOM Encoding

Set messageEncoding for binding to Mtom

<basicHttpBinding>
<binding name="binaryOverHttp" messageEncoding="Mtom"/>
</basicHttpBinding>

MTOM encoding optimises binary content handling


Uses base64 encoding in SOAP message if small

Places it in separate MIME message parts if large


Implementing Streaming
Define an operation that passes or returns a
System.IO.Stream

void UploadScannedApplicationForm(Stream uploadStream);


Stream DownloadStatementAsPDF( );

Set TransferMode on a streaming-friendly binding

Streamed
StreamedRequest
StreamedResponse

<basicHttpBinding>
<binding name="HttpStreaming"
maxReceivedMessageSize="67108864"
transferMode="Streamed"/>
</basicHttpBinding>
Stream Handling

• Sender obtains stream onto binary data

• Sender passes stream to receiver

• Sender should not close the stream

• Receiver starts to read from stream

At this point, no more WCF-related user code is


required:
WCF will close stream on sender side

User code must close stream on receiver side


Comparing Streaming and MTOM

MTOM Streaming

• Load message into memory • Link both ends directly

• Data in standard message • Optimised transfer mechanism

Large memory usage Small memory usage and


and performance impacts good performance

Interoperable and WCF-specific and


transparent interface restrictions

Restricted to 3 bindings
Any binding and security
and transport security
Demonstration: Passing Large Binary Data in WCF
In this demonstration, you will see how to:
• Pass large binary data between client and service by using
MTOM
• Pass large binary data between client and service by using
streaming
Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lab: Improving WCF Service Quality
• Exercise 1: Managing WCF Service Instances

• Exercise 2: Managing Concurrency Issues

• Exercise 3: Throttling Access to a WCF Service

• Exercise 4: Passing Bulk Data Between a WCF Client and


Service

Logon information

Virtual machine 6461A-LON-DEV-07

User name Student

Password Pa$$w0rd

Estimated time: 80 minutes


Lab Review
• What problems did you see when with singleton service
instance mode?
• What effect does throttling have on perceived performance
and overall throughput?
• Why is MTOM preferred over streaming?
Module Review and Takeaways
• Review Questions

• Common Issues and Troubleshooting Tips

• Real-world Issues and Scenarios

• Best Practices

Potrebbero piacerti anche