Thursday, August 21, 2008

WCF Global Exception Handlers

in WCF if you handle an exception and convert it to a fault, this can travel thru the wire to the client. however you can not anticipate all exceptions that might be thrown by the service. In this case you may want to create a gloabl Exception handler that deals with any unhandled exception, convert it to a fault and send it through to the client.

The simple way to do this is by creating an attribute (ServiceExceptionHandlerAttribute) class and an error handler class (ServiceExceptionHandler) as follows

----------- the attribute --------------
using System.ServiceModel.Channels;
using System.ServiceModel.Description;


namespace Ayman.ExceptionHandling
{
public class ServiceExceptionHandlerAttribute : Attribute,IServiceBehavior
{
#region IServiceBehavior Members
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
dispatcher.ErrorHandlers.Add(new ServiceExceptionHandler());
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
#endregion
}




------------ the error handler -----------------


using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;



namespace Ayman.ExceptionHandling
{
public class ServiceExceptionHandler : IErrorHandler
{
public bool HandleError(Exception error)
{

return true;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
ValidationFault validationFault = new ValidationFault();
FaultException fe = new FaultException(validationFault, error.Message);
MessageFault mf = fe.CreateMessageFault();
fault = Message.CreateMessage(version, mf, fe.Action);
}
}
}





And now all you need to do, is to decorate your service with this attribute as follows

using Ayman.ExceptionHandling;
namespace Ayman.MyService
{

[ServiceExceptionHandler]
public class MyService : IMyService
{

}
}

Thursday, August 14, 2008

Forward your hotmail to gmail

When I got my iphone, I searched for a program to forward my hotmail to gmail since the iphone does not support hotmail. many services were present at this time but all of them require you to pay. finally I found this program which is called getmail. you can download it for free from this link

Thursday, August 07, 2008

Globalization

I have been reading through Globalization and wanted to post a quick summary on how to show your applications in many languages. The way it works is as follows You set your UI forms to localizable and change the culture in the form properties (ex. ar-EG) {for Arabic Egypt} and start playing around with the interface, change the position of the labels, change their text to display in arabic, etc... this interface will be stored associated with the culture that you selected (ar-EG).

.Net actually offers great features specially for arabic language or for Right to Left languages in general. .Net offers two new properties at the form level, if you change these properties, all the controls in the form will be affected.





The first property as per the screen shot above is called RightToLeft, when you set this property to Yes, all controls will be displaying text from Right to Left.

The other property is RightToLeftLayOut, when you set this property to True all the controls in the form will change their positions automatically to appear in the correct arabic way.

What you need to do next is, decide which culture you want to use by calling the following function to set the culture for your UI before the form is displayed.

System.Threading.Thread.CurrentUICulture = new System.Globalization.CultureInfo ("ar-EG")

note the above CurrentUICulture not CurrentCulture

the above line will instruct your application to show all the screens in arabic (ar-EG) in the same way you designed them when the culture was set to arabic (ar-EG) during design time.





Simple?