Wednesday, November 28, 2007

Throwing custom Web Service Exception from Web Service

Suppose in a real scenario an exception is generated in the Data access or any other layer of our application and we want to show a custom message then we can use SoapException class in System.Web.Services.Protocols which will do the trick for us.

This is sample DAO class which has a method getEmpData which throws an exception

1    public class DAO
2 {
3 public DAO()
4 {
5 //
6 // TODO: Add constructor logic here
7 //

8 }
10 public static string getEmpData(int id)
11 {
12 throw new Exception("Method not supported");
14 }
15 }
The following is a sample Webservice with the webmethod in the same project which used the above DAO class
1    using System;
2 using System.Web;
3 using System.Web.Services;
4 using System.Web.Services.Protocols;
6 [WebService(Namespace = "")]
7 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
8 public class Service : System.Web.Services.WebService
9 {
10 public Service () {
12 //Uncomment the following line if using designed components
13 //InitializeComponent();

14 }
16 [WebMethod]
17 public string HelloWorld() {
18 try
19 {
20 return DAO.getEmpData(9);
21 }
22 catch (Exception ex)
23 {
24 throw new SoapException(ex.Message, SoapException.ServerFaultCode, Context.Request.Url.AbsoluteUri);
25 // return;
26 }
27 }
29 }

line 20 getEmpData pf the DAO class is called. The exception thrown is catched in the webmethod of the webservice in line 22, In this fasion you can bubble up the exception from any layer (even if it is a custom exception ) and handle that in the webmethod, it is better that you handle the soapException in webmethod, (if you want it to be present in any other class you will have to import System.Web.Services.Protocols namespace).

In this manner you can handle any Exception and pass the custom error messages to the client rather than the default error messages.

Some important properties of SoapException class (

  • Message : The Message property of the original exception.
  • Code: ServerFaultCode.
  • Actor:The URL of the XML Web service method.
  • Detail:a null reference (Nothing in Visual Basic), but an empty detail element is present in the fault element.

After this i created a sample web application and called the Webmethod which throws the SoapException at line 2

     1  localhost.Service myser = new localhost.Service();
2 myser.HelloWorld();

Here at line 2 i got the custom error message passed from the DAO layer of the web service.

Monday, November 26, 2007

A, B , C & now F# programming language

F# is derived from the ML family of languages, part of the functional programming language tradition. However, F# is not simply a functional language: it is a multi-paradigm langauge that attempts to capture the best of both functional and object-oriented programming in the context of .NET. Furthermore, even when used as a purely functional language F# is remarkable for the sheer power of the libraries and tools available through .NET.

The distinctive features of all ML-family languages are strong static type checking, excellent type inference, lightweight and typesafe function types, safe and syntactically convenient discriminated unions. These core work together to allow for succinct yet efficient solutions to many programming problems, and repeatedly permit common patterns of coding to be abstracted very easily.

One way of looking at it is that the purpose of F# is to solve all of the 7 major problems described in Wadler's classic paper Why no one uses functional languages: Libraries, Portability, Availability, Packagability, Tools, Training and Popularity. Of these, F# solves the issues of libraries (by immediately giving high-quality, no-wrapper access to hundreds of .NET libraries), portability (the .NET bytecode is portable, e.g. the Mono project provides an implementation for many platforms), packagability (.NET assemblies are an excellent packaging mechanism) and tools (.NET tools for other languages nearly always work with F# as well). The remaining issues are partly solved by the fact that F# has a core design similar to that of OCaml, a popular functional language implementation for which a good amount of training material is available, and also that huge amounts of excellent training material for the .NET platform is available on the web.

In more detail, the aims of F# are as follows:

  • To implement the core features of an ML programming language on the .NET platform, focusing on language interoperability via typed compilation, and to provide the an ML implementation that can co-exist as a partner language in a multi-language setting ;

  • To bring the benefits of .NET and ML-style programming to the scientific, engineering and high-performance computing communities. This also provides a setting where we can write truly significant applications in an ML-like language, thereby enabling the research community to identify and solve the remaining issues that inhibit the uptake of modern languages in the context of these software ecosystems.

There are several key application areas for a language like F# on the .NET platform. In particular, such languages excel at mathematically-oriented programming, and, given the right libraries and visualization tools, a range of scientific and engineering tasks as well. These languages are also well known to be extremely powerful when used to implement sophisticated symbolic analyses such as hardware verification, software verification, optimization, machine learning and compilation.

You can read about many applications of F# on Don Syme's blog.

The minimal technical requirements for a powerful scientific and data-intensive .NET programming language are as follows:

  • High performance

  • Succinctness

  • Simplicity

  • Interactive scripting and visualization

  • A rich developer experience (intellisense, codesense, debug, tools)

  • Support for .NET component development

  • Easy, efficient access to .NET libraries

  • Alignment with key for emerging technical trends such as multi-core computing, web services and data-oriented meta-programming.

Here are some of the things that make F# highly distinctive in this arena:

  • Its inherent technical strengths as a multi-paradigm programming language, and in particular the added productivity benefits of functional programming.

  • The use of type inference and automatic generalization to achieve both succinctness and performance.

  • The F# toolset for interactive visualization and development

  • Its support for LINQ and LINQ-style meta-programming.

  • The support of Microsoft Research for a credible, stable reputation and as a focal point for the F# community and as a key source of high-impact users at Microsoft.

  • The inherent importance of functional programming in the emerging multi-core world.

F# is, technically speaking, neutral with respect to concurrency - it allows the programmer to exploit the many different techniques for concurrency and distribution supported by the .NET platform. However, functional programming is one of the primary known techniques for minimizing, tracking and isolating the use of mutable state, essential for emerging techniques such as software-transactional-memory.

F# can directly use multi-core programming libraries such as ParallelFX. F# also includes language and library support for asynchronous workflows, a technique to write reactive programs and asynchronous message passing agents in a natural and compositional style.

One of the striking features of F# is its combination of dynamic and static features. For example, F# Interactive provides a strikingly powerful dynamic compilation environment, where dynamically entered code is executed using the full performance power of high-quality native code.

Another key feature of dynamic languages is succinctness. As with previous versions of ML, F# manages to be as succinct as many dynamic languages through its use of type inference, but like-for-like computationally-intensive samples will typically execute faster than other dynamic language implementations.

F# permits the use of other dynamic techniques such as a universal 'object' type, runtime code generation, runtime inspection of the properties of values and the runtime analysis of type parameters.

F# doesn't share all the features of dynamic languages. For example, it is not yet easily hosted inside other applications. At a language level, some operations characteristic of dynamic languages are not permitted. Likewise, it doesn't directly target the Dynamic Language Runtime, though may do so in the future.

"ML" is a language design that has had several manifestations, from Edinburgh ML to Standard ML and through to the current SML '97 and OCaml languages. F# follows the tradition of the Caml design group who seek a balance between stability and innovation. Both F# and OCaml use a simple, elegant core language and add interesting extra features. For the past several years, Ocaml has been a de-facto standard in the applied ML programming. As such, F# takes the design of core OCaml as a starting point, but combines the enormous strengths of the core OCaml language with those of .NET.


  1. - The CAML Language

Wednesday, November 14, 2007

ASP.NET MVC Framework

How often we have dreamed of a tool which would create all those patterns for us.
It seems that Gods at Microsoft have listened our prayers.

Microsoft is going to release a new framework with MVC support

What is MVC?

The domain-specific representation of the information that the application operates. Domain logic adds meaning to raw data (e.g., calculating whether today is the user's birthday, or the totals, taxes, and shipping charges for shopping cart items).
Many applications use a persistent storage mechanism (such as a database) to store data. MVC does not specifically mention the data access layer because it is understood to be underneath or encapsulated by the Model.
Renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes.
Processes and responds to events, typically user actions, and may invoke changes on the model.

ASP.NET MVC Framework

  • It enables clean separation of concerns, testability, and TDD by default. All core contracts within the MVC framework are interface based and easily mockable (it includes interface based IHttpRequest/IHttpResponse intrinsics). You can unit test the application without having to run the Controllers within an ASP.NET process (making unit testing fast). You can use any unit testing framework you want to-do this testing (including NUnit, MBUnit, MS Test, etc).
  • It is highly extensible and pluggable. Everything in the MVC framework is designed so that it can be easily replaced/customized (for example: you can optionally plug-in your own view engine, routing policy, parameter serialization, etc). It also supports using existing dependency injection and IOC container models (Windsor, Spring.Net, NHibernate, etc).
  • It includes a very powerful URL mapping component that enables you to build applications with clean URLs. URLs do not need to have extensions within them, and are designed to easily support SEO and REST-friendly naming patterns.
  • The MVC framework supports using the existing ASP.NET .ASPX, .ASCX, and .Master markup files as "view templates" (meaning you can easily use existing ASP.NET features like nested master pages, <%= %> snippets, declarative server controls, templates, data-binding, localization, etc). It does not, however, use the existing post-back model for interactions back to the server. Instead, you'll route all end-user interactions to a Controller class instead - which helps ensure clean separation of concerns and testability (it also means no viewstate or page lifecycle with MVC based views).
  • The ASP.NET MVC framework fully supports existing ASP.NET features like forms/windows authentication, URL authorization, membership/roles, output and data caching, session/profile state management, health monitoring, configuration system, the provider architecture, etc
For details view the podcast
ScottGu MVC Presentation and ScottHa Screencast from ALT.NET Conference