Tuesday, 17 June 2008

TRY CATCH in SQL Code

This is quite a handy feature in SQL Server that has been introduced recently.

In your stored procedures or functions,you can now write a try catch block and catch errors as we do in the more proper programming lanugages.


USE [SAMPLEDB]

GO

- StoredProcedure [dbo].[GetStudents]

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE dbo.GetStudents
AS

SET NOCOUNT ON;

--Error variables
DECLARE @errMessage VARCHAR(2000)
@errNum INT,
@errSeverity INT,
@errState INT,
@errProcedure NVARCHAR(500),
@errLine INT


BEGIN TRY

-- Do some processing here...
Select * from Students
-- Open Cursor etc

END TRY
BEGIN CATCH

--capture error information into variables
SELECT @errNumber = ERROR_NUMBER(),
@errSeverity = ERROR_SEVERITY(),
@errState = ERROR_STATE(),
@errProcedure = ERROR_PROCEDURE(),
@errLine = ERROR_LINE()

--raise an error now

RAISERROR (@errMessage, @errSeverity, 1, @errNumber, @errSeverity, @errState, @errProcedure, @errLine) WITH NOWAIT;

--rollback any open transaction

IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END

GOTO OutOfCatch

END CATCH

OutOfCatch:

- Finishing code here

Tuesday, 3 June 2008

WCF, SOA and AJAX quick reading!

WCF, SOA and AJAX

The latest programming framework from microsoft for distributed applications. WCF is a single answer to various approaches existed earlier which are WebServices,Remoting and COM+, DCOM etc. In other sense WCF is replacement for .Net 1.0/1.1 's Web Services and Remoting technologies.

WCF enables implimentation of SOA (Service Oriented Architecture) where application is seperated into consumable (functions/methods) services which can be consumed by client applications over a distributed environment.

WCF can use SOAP messages for communication between two processes. WCF implements many advanced web services (WS) standards such as WS-Addressing, WS-ReliableMessaging and WS-Security.

WCF implimentation
WCF Service is implimented as a class. WCF Service involves following concepts.

Service Contract
An interface that defines operations/methods that are available on the service. These methods transfer data which

Data Contract
Date Contract defines the so called data transfer objects in/out of the WCF Service.

Implimenting a WCF Service Example

http://msdn.microsoft.com/en-us/library/ms733764.aspx

WCF Service run time behaviour can be controlled. Checkout the following example.

http://msdn.microsoft.com/en-us/library/ms734715.aspx

End Points
WCF client connects to a WCF Service via an EndPoint.

End point defines (ABC) Address/Binding/Contract which specifies

- An address that indicates where the endpoint can be found.
- A binding that specifies how a client can communicate with the endpoint.
- A contract that identifies the operations available.
- A set of behaviors that specify local implementation details of the endpoint.

END POINT Example

http://msdn.microsoft.com/en-us/library/ms734786.aspx

Create Endpoint in code

http://msdn.microsoft.com/en-us/library/ms731080.aspx


Message Contracts

Data contracts carry data in and out of the operations provided by WCF. Message contracts extend or enable them over to SOAP.

WCF supports two kinds of operations.
-RPC (Remote procedure call Style) or Messaging Style

RPC uses its binary approach hence can serialise any type of data.

Messaging style is to enable SOAP with seemless programming feature for developers.
which enables developers use SOAP messages quickly and easily create and use service applications without learning SOAP protocall in much detailed.

Using Message Contracts

http://msdn.microsoft.com/en-us/library/ms734715.aspx

Hosting and Consuming WCF Services.

http://msdn.microsoft.com/en-us/library/bb332338.aspx

Now coming to consuming WCF using AJAX

I find the following example, a simple and clear demonstration of how WCF call works below the surface of new AJAX script manager reference model.

This example uses basic xmlhttprequest object to make the asynch call.

http://blogs.msdn.com/alikl/archive/2008/02/18/how-to-consume-wcf-using-ajax-without-asp-net.aspx

AJAX call to WCF service can be made using the new scriptmanagerproxy control which automatically generates a dynamic javascript object which can then be consumed in javascript.

Few Links to WCF Tools

servicesengine

Service Trace Viewer

More on this in another article soon.

Happy Coding !