WCF 自定义异常

异常数据协定代码:

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

namespace Contracts
{
    [DataContract]
   public class MyError
    {
        private string code = "";
       
        [DataMember]
        public string Code
        {
            get { return code; }
            set { code = value; }
        }
        private string message = "";

        [DataMember]
        public string Message
        {
            get { return message; }
            set { message = value; }
        }
    }
}
在服务协定中使用code:

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

namespace Contracts
{
    [ServiceContract(SessionMode=SessionMode.Required)]   
    public interface ISession
    {

       //注意:不能用单工(IsOneWay=false)
        [OperationContract(IsOneWay=false,IsInitiating=true,IsTerminating=false )]
        [FaultContract(typeof(MyError))]
        void Adds(double d);

        [OperationContract(IsInitiating=false,IsTerminating=true)]
        double GetResult();

    }
}

 

服务协定的实现Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Contracts;

namespace Services
{
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
   public class CSession:ISession
    {
       private double result = 0;
        #region ISession 成员

        public void Adds(double d)
        {
            MyError myerr = new MyError();
            myerr.Code = "001";
            myerr.Message = "这是一个错误";
            FaultException<MyError> err = new FaultException<MyError>(myerr);
            throw err;

            this.result+=d;
            System.Console.WriteLine("Adds{0}",d);
        }

        public double GetResult()
        {
            Console.WriteLine("The GetResult method is invoked and the current session ID is: {0}", OperationContext.Current.SessionId);
            return this.result;
        }

        #endregion

        public CSession()
        {
            Console.WriteLine("CSession is Created!");
        }

        ~CSession()
        {
            Console.WriteLine("CSession object has been destoried");
        }
    }
}

客户端调用Code:

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

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            ChannelFactory<Contracts.ISession> factory = new ChannelFactory<Contracts.ISession>("CSession");
            try
            {
                Contracts.ISession s = factory.CreateChannel();
                Console.WriteLine("Create s");
                Console.WriteLine("Adds(1)");
                s.Adds(1);
                Console.WriteLine("Adds(2)");
                s.Adds(2);
           
                Console.WriteLine("Result:{0}", s.GetResult());
            
                (s as ICommunicationObject).Close();

                Contracts.ISession s1 = factory.CreateChannel();
                Console.WriteLine("Create s1");
                Console.WriteLine("Adds(1)");
                s1.Adds(1);
                Console.WriteLine("Adds(2)");
                s1.Adds(2);
                double d1 = s1.GetResult();
                Console.WriteLine("Result:{0}", d1.ToString());
                (s1 as ICommunicationObject).Close();
            }
            catch (FaultException<Contracts .MyError> ex)
            {
                Console.WriteLine(ex.Detail.Message + "--" + ex.Detail.Code);
            }

           Console.Read();


       
          
        }
    }

 


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值