Programming4us
         
 
 
Programming

Programming WCF Services : The Response Service (part 3) - Queued Service-Side Programming & Response Service-Side Programming

3/8/2012 11:28:33 AM

3. Queued Service-Side Programming

The service needs to construct a proxy that will dispatch messages to the client-side response service. To simplify this, use my ServiceResponseBase<T>, defined as:

public abstract class ServiceResponseBase<T> : HeaderClientBase<T,ResponseContext>
                                                                   where T : class
{
   public ServiceResponseBase();
   public ServiceResponseBase(string bindingName);
   public ServiceResponseBase(NetMsmqBinding binding);
}

					  

ServiceResponseBase<T> automates reading the response context from the message headers and writing the response itself to the outgoing headers. Other than that, you can use it like any other proxy base class. Example 9-25 demonstrates the usage of ServiceResponseBase<T>.

Example 5. Using ServiceResponseBase<T>
class CalculatorResponseClient : ServiceResponseBase<ICalculatorResponse>,
                                                               ICalculatorResponse
{
   public void OnAddCompleted(int result,ExceptionDetail error)
   {
      Channel.OnAddCompleted(result,error);
   }
}
class MyCalculator : ICalculator
{
   [OperationBehavior(TransactionScopeRequired = true)]
   public void Add(int number1,int number2)
   {
      int result = 0;
      ExceptionDetail error = null;

      try
      {
         result = number1 + number2;
      }
      //Don't rethrow
      catch(Exception exception)
      {
         error = new ExceptionDetail(exception);
      }
      finally
      {
         CalculatorResponseClient proxy = new CalculatorResponseClient();

         proxy.OnAddCompleted(result,error);

         proxy.Close();
      }
   }
}

					  

In Example 5, the MyCalculator service catches any exception thrown by the business logic operation and wraps that exception with an ExceptionDetail object. The service does not rethrow the exception. As you will see later, in the context of transactions and response services, rethrowing the exception would also cancel the response. Moreover, when using a response service, being able to respond in case of an error is a much better strategy than relying on WCF’s playback error handling.

In the finally statement, regardless of exceptions, the service responds. It creates a new proxy to the response service to enqueue the response. The proxy in Example 5 will default to using the same MSMQ binding as the host.

Example 6 shows the implementation of ServiceResponseBase<T>.

Example 6. Implementing ServiceResponseBase<T>
public abstract class ServiceResponseBase<T> : HeaderClientBase<T,ResponseContext>
                                                                   where T : class
{
   public ServiceResponseBase() : this(OperationContext.Current.Host.
                               Description.Endpoints[0].Binding as NetMsmqBinding)
   {}
   public ServiceResponseBase(string bindingName) :
                                              this(new NetMsmqBinding(bindingName))
   {}

   public ServiceResponseBase(NetMsmqBinding binding) :
                                              base(ResponseContext.Current,binding,
                      new EndpointAddress(ResponseContext.Current.ResponseAddress))
   {
      Endpoint.VerifyQueue();
   }
}

					  

The default constructor of ServiceResponseBase<T> uses the same queued binding the host was using to dequeue the client’s call. You can also specify an MSMQ binding section in the config file or provide the constructor with the binding instance to use. Both of these constructors delegate the work to the third constructor, which accepts the MSMQ binding to use. That constructor reads the response address out of the response context and provides those two along with the response context to the base constructor of HeaderClientBase<T,H>. It also verifies the presence of the response queue.

Note that ServiceResponseBase<T> sends the response service the entire response context (not just the ID). This is done both for simplicity’s sake and because it may be beneficial for the response service to have access to the fault and response address used.

4. Response Service-Side Programming

The response service accesses its response context, reads from it the method ID, and responds accordingly. Example 7 demonstrates a possible implementation of such a response service.

Example 7. Implementing a response service
class MyCalculatorResponse : ICalculatorResponse
{
   [OperationBehavior(TransactionScopeRequired = true)]
   public void OnAddCompleted(int result,ExceptionDetail error)
   {
      string methodId = ResponseContext.Current.MethodId;
      ...
   }
}


Note:

It is common for the response service to update the application’s user interfaces with the queued results (or errors). For example:

class CalculatorResponse :
                        FormHost<CalculatorResponse>,
                                  ICalculatorResponse
{
   [OperationBehavior(TransactionScopeRequired=true)]
   public void OnAddCompleted(int result,
                              ExceptionDetail error)
   {
      Text = "Add returned: " + result;
      ...
   }
}

In fact, nothing prevents you from having the client itself be the response service as well.

Other -----------------
- Programming WCF Services : Queued Versus Connected Calls - Requiring Queuing
- Programming WCF Services : Queued Services - Playback Failures
- DotNetNuke Skinning : Package and Deploy
- Unit Testing in Visual Studio 2010 (part 2) - Running a battery of tests
- Unit Testing in Visual Studio 2010 (part 1) - Creating unit tests
- Microsoft ASP.NET 3.5 : AJAX-Enabled Web Services - Remote Calls via Page Methods
- Microsoft ASP.NET 3.5 : WCF Services for ASP.NET AJAX Applications
- Mobile Game Networking Essentials : Network Programming and J2ME
- Mobile Game Networking Essentials : Multiplayer Game Basics & Network Game Problems and Solutions
- Software Testing with Visual Studio Team System 2008 : Debug and running web test (part 2) - Running the test
- Software Testing with Visual Studio Team System 2008 : Debug and running web test (part 1) - Settings for .testrunconfig file
- Visual Studio Team System 2008 : Web test editor (part 3) - Toolbar properties
- Visual Studio Team System 2008 : Web test editor (part 2) - Other request properties
- Visual Studio Team System 2008 : Web test editor (part 1) - Web test properties & Web test request properties
- Build Mobile Websites and Apps for Smart Devices : Design for Mobile - Standing on the Shoulders of Giants
- Build Mobile Websites and Apps for Smart Devices : Design for Mobile - Build a Better Mouse
- Developing BlackBerry Tablet Applications with Flex 4.5 : Create a Flex Mobile Project (part 4) - Reading and setting author information for debug
- Developing BlackBerry Tablet Applications with Flex 4.5 : Create a Flex Mobile Project (part 3) - Setup Device
- Developing BlackBerry Tablet Applications with Flex 4.5 : Create a Flex Mobile Project (part 2) - Setup Simulator
- Developing BlackBerry Tablet Applications with Flex 4.5 : Create a Flex Mobile Project (part 1)
 
 
Most View
- Performing SharePoint 2010 Installations (part 1) - SharePoint 2010 Standalone Installation
- Microsoft Exchange Server 2003: Configuring Recipient Objects (part 2) - Managing Mailboxes
- Windows7: Managing Network Connections (part 5) - Using a Network Connection to Wake Up a Sleeping Computer
- Windows Server 2008 : Schedule Regular Full Server Backups of a Domain Controller by Using the Windows Interface
- Windows 7: Managing Wireless Network Connections (part 3) - Reordering Wireless Connections
- Microsoft SQL Server 2008 R2 : Monitoring Replication (part 1) - Replication Monitoring SQL Statements
- Programming Windows Phone 7 : The Phone’s Photo Library
- SQL Server Programmability Objects
- Windows Server 2008 : Configuring FTP (part 12) - Managing FTP Site Settings
- SharePoint 2010 : Authoring Pages - Create a New Page (part 1)
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS