Use of [FaultContract] in Silverlight-WCF Service

Some Intro:

First of all before starting this post I want to mention that this idea I am going to discuss is all depend on the codes generated by the problem solver blog. My aim is to make it simple to understand for the reader.

Before starting this, in Silverlight WCF Service do not have [FaultContract], which means your application fails in a simple WCF communication error. To prevent this we have to create a structure to handle this drawback of the Silverlight. In doing this I google this issue an at the end come up with a solution that Problem Solver mentioned in the blog.

Let’s get started:

First to keep it simple I created WCF Service by using the Visual Studio “Add Service Rerence” tool not to make it more complex.

We have our traditional Interface IService1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService1
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string getir();
        [OperationContract]
        string getirHatali(out MyFaultContract myFault);
        [OperationContract]
        string hata();
        // TODO: Add your service operations here
    }
    [DataContract]
    public class MyFaultContract
    {
        [DataMember]
        public string FaultType { get; set; }
        [DataMember]
        public string Message { get; set; }
    }
}

Then Service1.svc.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService1
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
    public class Service1 : IService1
    {
        public string getir()
        {
            string result = "WCF Serviceden gelen string";
            return result;
        }

        public string hata()
        {

            string result = getir();

            throw new FaultException<ArgumentException>(
                new ArgumentException("WCF Service hatasi olustu, "));

            return result;

        }

        public string getirHatali(out MyFaultContract myFault)
        {
            string result="";
            myFault = null;

            try
            {
                result = hata();
            }
            catch (FaultException<ArgumentException> ex)
            {
                myFault = new MyFaultContract()
                {
                    FaultType = ex.Detail.GetType().FullName,
                    Message = ex.Detail.Message
                };
            }

            return result;
        }
    }
}

hata()is the function to create an error in the application and getirHatali(out MyFaultContract myFault) for displaying error message.

And also Page.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApplication1.ServiceReference1;
namespace SilverlightApplication1
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Service1Client x = new Service1Client();
            x.getirCompleted += new EventHandler<getirCompletedEventArgs>(x_getirCompleted);
            x.getirAsync();

        }

        void x_getirCompleted(object sender, getirCompletedEventArgs e)
        {
            Sonuc.Text = Convert.ToString(e.Result);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Service1Client y = new Service1Client();
            y.getirHataliCompleted += new EventHandler<getirHataliCompletedEventArgs>(y_getirHataliCompleted);
            y.getirHataliAsync();

        }

        void y_getirHataliCompleted(object sender, getirHataliCompletedEventArgs e)
        {

            Sonuc.Text=Convert.ToString(e.myFault.Message+"Aciklama:"+e.myFault.FaultType);

        }
    }
}

Download Sample Solution

Thanks for your patience!

kick it on DotNetKicks.com

About these ads