当前位置:编程学习 > 网站相关 >>

我的WCF之旅(10):如何在WCF进行Exception Handling

在任何Application的开发中,对不可预知的异常进行troubleshooting时,异常处理显得尤为重要。对于一般的.NET系统来说,我们简单地借助try/catch可以很容易地实现这一功能。但是对于 一个分布式的环境来说,异常处理就没有那么简单了。按照面向服务的原则,我们把一些可复用的业务逻辑以Service的形式实现,各个Service处于一个自治的环境中,一个Service需要和另一个Service进行交互,只需要获得该Service的描述(Description)就可以了(比如WSDL,Schema和Strategy)。借助标准的、平台无关的通信构架,各个Service之间通过标准的Soap Message进行交互。Service Description、Standard Communication Infrastructure、Soap Message based Communication促使各Service以松耦合的方式结合在一起。但是由于各个Service是自治的,如果一个Service调用另一个Service,在服务提供方抛出的Exception必须被封装在Soap Message中,方能被处于另一方的服务的使用者获得、从而进行合理的处理。下面我们结合一个简单的Sample来简单地介绍我们可以通过哪些方式在WCF中进行Exception Handling。

一、传统的Exception Handling

我们沿用我们一直使用的Calculator的例子和简单的4层构架:

\
1.    Service Contract- Artech.ExceptionHandling.Contract

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

namespace Artech.ExceptionHandling.Contract
{
    [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        double Divide(double x, double y);
    }
}

定义了一个单一的进行除法运算的Operation。

2.    Service:Artech.ExceptionHandling.Service. CalculatorService

 

using System;
using System.Collections.Generic;
using System.Text;
using Artech.ExceptionHandling.Contract;

namespace Artech.ExceptionHandling.Service
{
    public class CalculatorService:ICalculator
    {
        ICalculator Members#region ICalculator Members

        public double Divide(double x, double y)
        {
            if (y == 0)
            {
                throw new DivideByZeroException("Divide by zero");
            }

            return x / y;
        }

        #endregion
    }
}

如果被除数是零,抛出一个DivideByZeroException Exception。

3.    Service Hosting

Configuration:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="calculatorServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="calculatorServiceBehavior" name="Artech.ExceptionHandling.Service.CalculatorService">
                <endpoint binding="basicHttpBinding" bindingConfiguration="" contract="Artech.ExceptionHandling.Contract.ICalculator" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8888/Calculator" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

Program


using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Artech.ExceptionHandling.Service;

namespace Artech.ExceptionHandling.Hosting
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost calculatorHost = new ServiceHost(typeof(CalculatorService)))
            {
                calculatorHost.Opened += delegate
                {
                    Console.WriteLine("The Calculator service has begun to listen via the address:{0}", calculatorHost.BaseAddresses[0]);
                };
                calculatorHost.Open();
                Console.Read();
         &nb

补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,