您的位置:首页 > 其它

【WCF--初入江湖】06 WCF契约服务行为和异常处理

2014-07-05 04:44 405 查看

06 WCF契约服务行为和异常处理




一、WCF契约服务行为


  【1】
  服务行为可以修改和控制WCF服务的运行特性。
  在实现了WCF服务契约后,可以修改服务的很多执行特性。
  这些行为(或者特性)是通过配置运行时属性或者通过自定义行为来控制的。
【2】分类

  WCF的行为分为两类:

    服务行为(Service Behavior)

    操作行为(Operation Behavior)

【3】应用的位置

应用在实现接口的类上,而不是接口上。

[ServiceBehavior]
public class ServiceClass:IService1
{
[OperationBehavior]
public int AddNumber(int x,int y)
{
return x+y;
}


  【4】服务的行为配置有两种方式:

  【4-1】使用代码配置

示例:在宿主中,使用 

ServiceHost的Description.Behaviors.Add()方法


//地址
Uri pipeaddress = new Uri("net.pipe://localhost/NetNamedPipeBinding");
Uri tcpaddress = new Uri("net.tcp://localhost:8088/TcpBinding");
。。。
//服务宿主对象
host = new ServiceHost(typeof(WcfServiceLibrary1.Service1), pipeaddress, tcpaddress);
。。。
//添加元数据 行为
ServiceMetadataBehavior mBehavior = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(mBehavior);


或者是:

[ServiceBehavior]
public class ServiceClass:IService1
{
[OperationBehavior]
public int AddNumber(int x,int y)
{
return x+y;
}
[OperationBehavior]
public int SubtractNumber(int x,int y)
{
return x-y;
}
}


  【4-2】使用配置文件配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfServiceLibrary1.Service1" behaviorConfiguration="textBehavior">
。。。。。。
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="textBehavior">
<serviceMetadata/>
<serviceDebug/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>


【5】[ServiceBehavior]的属性

AutomaticSessionShutdown                     客户端关闭会话时服务是否自动关闭
ConcurrencyMode                              线程的支持,默认:服务是单线程
IgnoreExtensionDataObject                    跟序列化有关
IncludeExceptionDetaillnFaults               如何处理未被处理的异常
InstanceContextMode                          服务实例对象创建的模式,PerCall 表示每次来一个请求为其创建一个对象的实例,调用后回收;PerSession是每次一个请求来只创建一个该服                                             务对象的实例直至其销毁,调用后进行回收,并且会话间不能共享; Single表示只允许创建一个该服务的实例,调用后不回收
ReleaseServiceInstanceOnTransactionComplete
TransactionAutoCompleteOnSessionClose
TransactionlsolationLevel
TtansactionTimeout
AutoDisposeParameters
ReleaseInstanceMode                           操作完后回收对象、调用操作前回收、根据自动方式回收
TransactionAutoComplete                       使用自动提交事务,默认为true


一个配示例:

<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="unity" type="ByteartRetail.Infrastructure.UnityExtensions.UnityBehaviorExtensionElement, ByteartRetail.Infrastructure,  Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<unity operationContextEnabled="true" instanceContextEnabled="true" contextChannelEnabled="true" serviceHostBaseEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>


另一个:

<serviceBehaviors>
<behavior name="WcfService1.Service1Behavior">
<serviceAuthorization ></serviceAuthorization>
<serviceTimeouts/>
<serviceThrottling maxConcurrentCalls="" maxConcurrentInstances="" maxConcurrentSessions=""/>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>



二、异常处理



  【1】种类:

    FaultException 创建非类型化的错误,在服务端进行创建,然后用SOAP方式返回客户端

TimeoutException

CommunicationException

FaultException异常:

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

namespace Keasy5.DataObject
{
/// <summary>
/// Represents the data to be transferred through the
/// network which contains the fault exception information.
/// </summary>
[DataContract]
public class FaultData
{
#region Public Properties
/// <summary>
/// Gets or sets the message of the fault data.
/// </summary>
[DataMember(Order = 0)]
public string Message { get; set; }
/// <summary>
/// Gets or sets the full message of the fault data.
/// </summary>
[DataMember(Order = 1)]
public string FullMessage { get; set; }
/// <summary>
/// Gets or sets the stack trace information of the fault exception.
/// </summary>
[DataMember(Order = 2)]
public string StackTrace { get; set; }
#endregion

#region Public Static Methods
/// <summary>
/// Creates a new instance of <c>FaultData</c> class from the specified <see cref="System.Exception"/> object.
/// </summary>
/// <param name="ex">The <see cref="System.Exception"/> object which carries the error information.</param>
/// <returns>A new instance of <c>FaultData</c> class.</returns>
public static FaultData CreateFromException(Exception ex)
{
return new FaultData
{
Message = ex.Message,
FullMessage = ex.ToString(),
StackTrace = ex.StackTrace
};
}
/// <summary>
/// Creates a new instance of <see cref="FaultReason"/> class from the specified <see cref="Exception"/> object.
/// </summary>
/// <param name="ex">The <see cref="System.Exception"/> object which carries the error information.</param>
/// <returns>A new instance of <see cref="FaultReason"/> class.</returns>
public static FaultReason CreateFaultReason(Exception ex)
{
return new FaultReason(ex.Message);
}
#endregion
}
}


View Code
  第二步:

// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“OrderService”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 OrderService.svc 或 OrderService.svc.cs,然后开始调试。
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class OrderService : IOrderService
{
private readonly IOrderService orderServiceImpl = ServiceLocator.Instance.GetService<IOrderService>();
public Int32 GetShoppingCartItemCount(Guid userID)
{
try
{
return orderServiceImpl.GetShoppingCartItemCount(userID);
}
catch (Exception ex)
{
throw new FaultException<FaultData>(FaultData.CreateFromException(ex), FaultData.CreateFaultReason(ex));
}
}


使用了这个构造函数:

//
// 摘要:
//     初始化使用指定详细信息对象和错误原因的 System.ServiceModel.FaultException<TDetail> 类的新实例。
//
// 参数:
//   detail:
//     用作 SOAP 错误详细信息的对象。
//
//   reason:
//     SOAP 错误的原因。
public FaultException(TDetail detail, FaultReason reason);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐