Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Tuesday, November 11, 2008

How to Handle an Unhandled Exception in WPF?

By default Windows Presentation Foundation (WPF) catches unhandled exceptions and notifies the user. once user answer to the dialog box the application will shutsdown automatically.


Its also possible to handle this default behaviour and provide custom unhandled exception for that we need to override the DispatcherUnhandledException event. this way we can have centralized unhandled exception processing.



Namespace
System.Windows.Application.DispatcherUnhandledException Event
Occurs when an exception is thrown by an application but not handled by user.


DispatcherUnhandledException is raised by an Application for each exception that is unhandled by code running on the main UI thread. If an exception is not handled on either a background user interface (UI) thread (a thread with its own Dispatcher) or a background worker thread (athread without a Dispatcher), the exception is not forwarded to the main UI thread. Consequently, DispatcherUnhandledException is not raised.


In these circumstances, you will need to write code to do the following:


Handle exceptions on the background thread.


Dispatch those exceptions to the main UI thread.


Rethrow them on the main UI thread without handling them to allow DispatcherUnhandledException to be raised.






Once you handle the unhandled exception from DispatcherUnhandledException, you need to set the Handled property to true otherwise WPF will continue processing it,


The following example shows how to process unhandled exceptions by
handling the DispatcherUnhandledException event.





using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace ProjectExp {
///
/// Interaction logic for App.xaml
///

public partial class App : Application
{
private void App_Unhandle(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
string msg = string.Format("Expection occurs in {0}", e.Exception.Message);
MessageBox.Show(msg);
e.Handled = true;
}
}
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProjectExp {
///
/// Interaction logic for Window1.xaml
///

public partial class Window1 : Window {
public Window1() {
InitializeComponent();
}

private void btnUnhandle(object sender, RoutedEventArgs e)
{
throw new NotImplementedException("Failed"); // Here i am not handling the exception .
}

private void btnhandle(object sender, RoutedEventArgs e)
{
try
{
throw new NotImplementedException();
} catch (NotImplementedException ex) // Here Exception is handled
{

MessageBox.Show(ex.Message);
}
}
}
}



XAML CODE



Thanks
SreenivasaRagavan

Friday, November 7, 2008

Calling JAVA Web Service from .NET 3.5 framework clients [Win-Forms, WPF, and Silver Light]

Many Enterprise companies have built their Enterprise application in Java technologies as middle tier or server side. While they want to build a compelling UI with Microsoft’s latest technologies, such as WPF 3.5 and Silver light 2.0, they still want to benefit from those existing investments instead of rewriting them. In order to do so, we have to bridge between those technologies and allow client side technologies consume Java web services. In this blog I am going to show you a step by step guide for building a Java EE Web Service, and a .Net client application that consumes it.

First of all let make sure that we have the following Software components installed on your machine.
Windows Presentation Foundation (WPF) is a next-generation presentation system for building Windows client applications with visually stunning user experiences. With WPF, you can create a wide range of both windows standalone and web browser-hosted applications.
WPF uses XAML (Zammel) Extensible Application Markup language to design the GUI.
Silver Light: This is Microsoft latest SDK for to develop RIA.

JAVA
1. Java JDK 6.1
2. Net Beans IDE 6.1

.NET :
1. VS.NET 2008
2. Silver Light 2.0 SDK


First we need to create a new java web project , for that you can use either eclipse or Netbeans. here i downloaded a free Net beans 6.1 from Net Beans download







Here i have added two WebMethods Add and Greetings . This is same as .NET ASMX web service we use [WebMethod] attribute to define a method which can invoked across the internet.












Once Java WebService Developed and Deployed successfully now we need to create WPF windows application. add Service Reference as a Java web service as shown.



Define WPF UI using XAML as shown .





same way we can use SilverLight 2.0 client to invoke Java WebService.

Thanks
Sreenivasaragavan