Friday, March 12, 2010

WCF – Exception Handling in Asynchronous Operations

Yesterday, I got a query regarding how to handle exceptions in case of asynchronous web service operations. Precise question was,

I am calling a WCF service using Asynchronous way,  the service may execute for some time, meanwhile if any exception occurred in the service side how can I come to know what happened in the service side?

I am not able to get the status in the Async Method.

There is a pretty simple solution available for this problem.

When the asynchronous method throws an exception, proxy catches it and when the client calls End Operation the proxy re-throws that exception object so that client can handle the exception. If a COMPLETION CALLBACK is provided, WCF calls that method immediately after the exception is received. Exception thrown is compliant with the fault contract and type of exception. Following code snippet shows how to do this;

Contract Code

public interface ICalculator
{
[OperationContract]
[FaultContract(typeof(Exception))]
double Add(double n1, double n2);
}



Service Code




public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
throw new FaultException<Exception>(new Exception("Gaurav is sending exception"));
return n1 + n2;
}
}



Client Code




class Client
{
static void Main()
{
Console.WriteLine("Press <ENTER> to terminate client once the output is displayed.");
Console.WriteLine();

GauravCalculator.CalculatorClient calcService = new GauravCalculator.CalculatorClient();
int sum = 0;
AsyncCallback completion = (result) =>
{
try
{
sum = (int)calcService.EndAdd(result);
Console.WriteLine("Sum is :" +sum.ToString());
calcService.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
};
calcService.BeginAdd(10, 10, completion, null);

Console.WriteLine();
}



Using this method you can receive any service side exceptions on client side.

~pEaCe~


Currently Listening to: Bekaraar from Pathshala (new Lucky Ali song)

Wednesday, March 3, 2010

GO – SQL Server

While working on SQL Server we all encounter GO statement quite often. Till today GO statement for me was just an indication sent out to SQL Server utilities about end of a batch of Transact - SQL.

Today while going through MSDN I found a new (surprising) thing about GO. Go can actually take an integer argument. Yes! it is true. Go takes an optional integer argument. Something like this,

GO [count]

As per MSDN,

count is a positive integer. The batch preceding GO will execute the specified number of times.

What this means is, suppose you have a script like the one below,

INSERT INTO TestGO VALUES (10, 'testing go')
GO 5000

Here, INSERT statement will be executed 5000 times and you will end up inserting 5000 records in table.

~eNjOy LiFe~
Currently Listening to: In the summer time - SHAGGY

Friday, February 19, 2010

Back from Redmond

Few weeks back I visited Microsoft office in Redmond, WA. That trip turned out to be a super roller coaster ride. Day I landed in our office some unexpected very high priority work came up and we started work on it without wasting any time. I got to work with some top technical and management guys.

During my work I interacted with various Microsoft GM’s and members of top leadership team. I also got a chance to work with SQL Server Query Optimization Engine team members like Lubor Kollar. Lubor is the one who designed partitioned tables and CTEs in SQL Server. He suggested few query optimization points related to my work as well. On my last day in Redmond, he gifted me one of his latest book on Query Optimization. Awesome! book.

First 2 weeks were quite hectic in terms of work. We worked for almost 12 hrs daily and yes we worked on weekends too. After first 2 weeks we got time to visit some really nice places in Redmond. We visited Snoqualmie, Mount Reiner and Seattle Downtown.

This year I’m concentrating on WCF, SQL Server Query Optimization and some misc stuff like power shell. I’ll continue posting blog entries related to these.

Friday, January 8, 2010

IIS 7.0 – Site Vs. Application Vs. Virtual Directory

Another thing with IIS 7.0 which always bothers me is exact difference between web site , application and virtual directory. There is lot of verbose information present online related to this topic. After going through few good web links I managed to prepare a summary of the differences, that is easy to understand and remember.

[ Note: It is highly possible that you might get this information from MSDN or IIS web logs as I prepared it from there :) ]

Introduction
  • A site contains one or more applications, an application contains one or more virtual directories, and a virtual directory maps to a physical directory on a computer
  • A site contains all the content, both static and dynamic, that is associated with that site.
  • Each site must contain at least one application, which is named the root application.
  • Each application (including the root application) must contain at least one virtual directory, which is named the root virtual directory. These objects work together to form the site.
  • An application can have several virtual directories, and each one will be served by the same App Domain as the application to which they belong.
Site
  • Container for applications and virtual directories, and you can access it through one or more unique bindings.
  • The binding includes two attributes important for communication: the binding protocol and the binding information (combination of IP address, port, and optional host header)
  • A site may contain more than one binding
  • In addition to containing applications (which contain virtual directories) and specifying bindings, the following configuration settings belong to the site:
        • Limits: configure settings to limit the amount of bandwidth, the number of connections, or the amount of time allowed for connections to a site.
        • Logging: configure settings for handling and storage of log files for the site.
        • Failed request trace logs: configure settings for logging failed-request traces for the site
APPLICATION
  • Group of files that delivers content or provides services over protocols
  • Application's path becomes part of the site's URL
  • In addition to belonging to a site, an application belongs to an application pool, which isolates the application from applications in other application pools on the server.
VIRTUAL DIRECTORY
  • Map to a physical directory on a local or remote server
  • Directory name then becomes part of the application's URL
  • An application can have more than one virtual directory
  • For example, you might use a virtual directory when you want your application to include images from another location in the file system, but you do not want to move the image files into the physical directory that is mapped to the application's root virtual directory.

I hope! things are much clearer now.

eNjOy cOdInG!!
Currently Listening to: John Lennon - Happy Xmas (War Is Over)

Thursday, January 7, 2010

IIS 7.0 – Classic and Integrated pipeline

While creating a new app pool, users are required to choose framework version and pipeline type. I was not sure about the difference between available pipeline types. I spent some time on this topic and reading MSDN articles. this is what I understood:

Classic Pipe Line in IIS 7.0

  • Works just like IIS 6.0
  • All requests pass through the IIS pipeline first, and then if the file extension is mapped to ASP.NET ISAPI extension, then the request is handed to aspnet_isapi.dll and passes through the ASP.NET request pipeline.
  • If the file is static file, ASP or PHP, it is handled by something else.

Integrated Pipeline IIS 7.0

  • Allows http modules to participate in all requests
  • This works also for static contents, PHP pages and image
  • Provides unified request processing, regardless of handler
  • For example, managed Forms Authentication can be used for all content, including ASP pages, CGIs, and static files.

Cheers!!!