silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了WCF在同步和异步调用情况下的异常捕获大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

WCF在同步和异常调用情况下的异常捕获 1 前言 关于WCF的基本信息,我就不在这里介绍了。一来是因为园子中的很多人都介绍过了,而且很是详细,再不行,还可以看书。二来是因为自己的概念表达还不是很好,别误导了大家。 在这里,我就直接讲解一种用法,然后贴点代码吧。 在WCF有一种契约,叫做错误契约FaultContract。 今天我就讲解一下,在同步和异步调用WCF服务的时候,客户端如何捕获服务端抛出

WCF在同步和异常调用情况下的异常捕获

1 前言

关于WCF的基本信息,我就不在这里介绍了。一来是因为园子中的很多人都介绍过了,而且很是详细,再不行,还可以看书。二来是因为自己的概念表达还不是很好,别误导了大家。

在这里,我就直接讲解一种用法,然后贴点代码吧。

在WCF有一种契约,叫做错误契约FaultContract。

今天我就讲解一下,在同步和异步调用WCF服务的时候,客户端如何捕获服务端抛出来的异常。捕获之后,如何处理,那就是根据项目的要求了。是提示呢?还是记录日志呢?还是其他什么的。。。。。。。。。。。。

2 正文

其他对于同步和异步来说,WCF处理异常的手段是一致的。都是将异常信息,通过我们自定义一个异常信息类,传输到客户端。客户端获取到这个类的信息,然后就知道了具体的异常。然后如何处理,就是客户端的事情了。

2.1 服务定义

错误契约定义

 [DataContract]
    public class CallException
    {
        public CallException() { }
        public CallException(String message,String detail)
        { message = message;
        Detail = detail;
        }
        [DataMember]
        public String message { get; set; }
        [DataMember]
        public String Detail { get; set; }
    }

 

接口定义

 [serviceContract]
    public interface Iservice1
    {
        [OperationContract]
        [FaultContract(typeof(CallException))]
        User GetUserByID(int id,String communCode,out CallException callException);

        [OperationContract]
        [FaultContract(typeof(CallException))]
        [serviceKNownType(typeof(User ))]
        BaseClass.EntityBase  GetByIDWithAuthentication(int id,out CallException callException);
    }

 

 

接口实现

 [AspNetCompatibilityrequirements(requirementsMode = AspNetCompatibilityrequirementsMode.Allowed)]
    [serviceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [Common.MyserviceBehavior()]
    public class service1Impl : BaseClass.serviceBase,Iservice1
    {
       
        #region Iservice1 Members

        public User GetUserByID(int id,out CallException callException)
        {
            callException = null;
            User user = null;
            BLl.UserBLL userBll = Common.serviceLocator.Loadservice();
            user= userBll.GetUserByID(id,out callException);
            return user;
        }

        //[Common.AuthenticationBehavior()]
        public BaseClass.EntityBase GetByIDWithAuthentication(int id,out CallException callException)
        {
            callException = null;
            User user = null;
            BLl.UserBLL userBll = Common.serviceLocator.Loadservice();
            user = userBll.GetByID(id,out callException);
            return user;
        }

        #endregion
    }

 

 

业务逻辑类

 public class UserBLL : BaseClass.bLLBase
    {
        public UserBLL(Common.DALHelper dalHelper)
        {
            _dalHelper = dalHelper;
        }
        private Common.DALHelper _dalHelper;


        [Common.ExceptionCallHandler("你?没?有瓺权ā限T","","你?没?有瓺权ā限T啊?BLL")]
        public User GetByID(int id,out CallException callException)
        {
            callException = null;
            if (id < 10)
            {
                callException = new CallException()
                {
                    message = "获?取?用?户§",Detail = "必?须?大洙于等台于?0"
                };
                throw new FaultException(callException,"parameter error");
            }
            else
            {
                User user = null;
                int b = 0;
                user = _dalHelper.UserDal.GetByID(id,ref b,out callException);
                return user;
            }
        }
        [Common.ExceptionCallHandler("你?没?有瓺权ā限T","你?没?有瓺权ā限T啊?BLL")]
        public User GetUserByID(int id,out CallException callException)
        {
            User user = null;
            callException = null;
            if (id < 10)
            {
                callException = new CallException()
                {
                    message = "获?取?用?户§",Detail = "必?须?大洙于等台于?0"
                };
            }
            else
            {
                int b = 0;
                user = _dalHelper.UserDal.GetByID(id,out callException);
            } return user;
        }
    }

 

 

在业务逻辑类中判断参数的合法性,不合法抛出异常。大家看到有两个方法一个是直接抛出异常,一个是没有抛出任何异常。后面将这两个的用处。

2.2 同步调用

同步调用,我们用控制台程序来模拟客户端。

同步方式调用WCF的时候,我们可以直接try。。。catch。。。来捕获这个异常信息,然后进行处理。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.serviceModel;
using System.serviceModel.ChAnnels;

namespace ConsoleConsumer
{
    class Program
    {
        static void Main(String[] args)
        {
            service1.service1Client client = new service1.service1Client();
            using (OperationContextScope scope = new OperationContextScope((IContextChAnnel)client.InnerChAnnel))
            {
                messageHeaders messageHeaderSELER_74_11845@ent = OperationContext.Current.outgoingmessageHeaders;
                messageHeaderSELER_74_11845@ent.Add(messageHeader.CreateHeader("username","kd"));
                messageHeaderSELER_74_11845@ent.Add(messageHeader.CreateHeader("password","kd"));
            }
            Console.WriteLine("请?输?入?ID:阰");
           int id =int.Parse ( Console.ReadLine());
            service1.CallException callException = null;
            try
            {
                
                client.GetByIDWithAuthentication(out callException,id);
                Console.WriteLine("成é功|调獭用?");
            }
            catch (FaultException eX)
            {
                Console.WriteLine("半?路·接ó获?CallException Error:{0},{1}",ex.Detail.message,ex.Detail.Detail);
            }
            catch (Exception eX)
            {
                Console.WriteLine("最?后ó一?关? Error:{0},ex.message,ex.HelpLink);
            }

            Console.Read();
        }
    }
}

 

主要是看catch (FaultException eX) 这一句。就好像是,WCF将我们的异常信息类包装到FaultException这个类中。然后ex.Detail就是CallException这个我们自定义实体类型了。那就可以根据这个实体的属性进行异常的处理了。

2.3 异步调用

异步调用,我们用Silverlight来模型客户端程序。

我们都知道,异步调用,其实就是多线程。然后再callBACk中处理返回的数据。

这时候,@R_937_10585@不能try。。。catch。。。了。因为调用成功,还是失败,是在方法完成的委托函数中才知道的。没有地方给你写try。。。catch。。。了。

  service1.service1Client client = new service1.service1Client();
  client.GetUserByIDCompleted += new EventHandler(client_GetUserByIDCompleted);
  client.GetUserByIDAsync(int.Parse(textBox1.Text.Trim()),"123456");

 

void client_GetUserByIDCompleted(object sender,service1.GetUserByIDCompletedEventArgs E)
{

}

 

是通过e来获取数据的。

但是异常信息需要通过通道传递的客户端,这点和同步调用是一样的。既然这样,那我们在定义服务端方法的时候,就添加一个out类型的参数,在服务端就将CallException这个实体赋值给这个参数。然后通过e.CallException就可以获取异常信息了,如果不为空,说明有异常存在。为空,说明没有异常,访问正常。

 void client_GetUserByIDCompleted(object sender,service1.GetUserByIDCompletedEventArgs E)
        {

            #region

            if (e.callException != null)
            {
                ChildWindow win = new ChildWindow();
                win.title = e.callException.message;
                win.Content = e.callException.Detail;
                win.MinHeight = 50;
                win.MinWidth = 200;
                win.Show();
            }
            else
            {
                ChildWindow win = new ChildWindow();
                win.title = "ok";
                win.Content = "it is ok";
                win.MinHeight = 50;
                win.MinWidth = 200;
                win.Show();
            }
            #endregion

        }

 

3 结论

不知道大家在捕获和处理WCF的异常的时候,是如何处理的?有更好的办法,欢迎大家一起讨论。

源码下载:http://download.csdn.net/source/2785386

大佬总结

以上是大佬教程为你收集整理的WCF在同步和异步调用情况下的异常捕获全部内容,希望文章能够帮你解决WCF在同步和异步调用情况下的异常捕获所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。