IServiceBehavior 接口
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供一种在整个服务内修改或插入自定义扩展的机制,包括 ServiceHostBase。
public interface class IServiceBehavior
public interface IServiceBehavior
type IServiceBehavior = interface
Public Interface IServiceBehavior
- 派生
示例
下面的代码示例演示了如何使用配置文件中所指定的服务行为来向服务应用程序中插入自定义错误处理程序。 在该示例中,错误处理程序捕获所有异常,并将它们转换为一个自定义 GreetingFault
SOAP 错误,该自定义错误会随后返回给客户端。
下面的 IServiceBehavior 实现不添加任何绑定参数对象,而会将自定义 System.ServiceModel.Dispatcher.IErrorHandler 对象添加到所有 ChannelDispatcher.ErrorHandlers 属性,并会验证服务行为所应用到的、System.ServiceModel.FaultContractAttribute 类型为 GreetingFault
的服务的每个操作。
// This behavior modifies no binding parameters.
#region IServiceBehavior Members
public void AddBindingParameters(
ServiceDescription description,
ServiceHostBase serviceHostBase,
System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints,
System.ServiceModel.Channels.BindingParameterCollection parameters
)
{
return;
}
// This behavior is an IErrorHandler implementation and
// must be applied to each ChannelDispatcher.
public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
{
Console.WriteLine("The EnforceGreetingFaultBehavior has been applied.");
foreach(ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers)
{
chanDisp.ErrorHandlers.Add(this);
}
}
// This behavior requires that the contract have a SOAP fault with a detail type of GreetingFault.
public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
{
Console.WriteLine("Validate is called.");
foreach (ServiceEndpoint se in description.Endpoints)
{
// Must not examine any metadata endpoint.
if (se.Contract.Name.Equals("IMetadataExchange")
&& se.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex"))
continue;
foreach (OperationDescription opDesc in se.Contract.Operations)
{
if (opDesc.Faults.Count == 0)
throw new InvalidOperationException(String.Format(
"EnforceGreetingFaultBehavior requires a "
+ "FaultContractAttribute(typeof(GreetingFault)) in each operation contract. "
+ "The \"{0}\" operation contains no FaultContractAttribute.",
opDesc.Name)
);
bool gfExists = false;
foreach (FaultDescription fault in opDesc.Faults)
{
if (fault.DetailType.Equals(typeof(GreetingFault)))
{
gfExists = true;
continue;
}
}
if (gfExists == false)
{
throw new InvalidOperationException(
"EnforceGreetingFaultBehavior requires a FaultContractAttribute(typeof(GreetingFault)) in an operation contract."
);
}
}
}
}
#endregion
' This behavior modifies no binding parameters.
#Region "IServiceBehavior Members"
Public Sub AddBindingParameters(ByVal description As ServiceDescription, ByVal serviceHostBase As ServiceHostBase, ByVal endpoints As System.Collections.ObjectModel.Collection(Of ServiceEndpoint), ByVal parameters As System.ServiceModel.Channels.BindingParameterCollection) Implements IServiceBehavior.AddBindingParameters
Return
End Sub
' This behavior is an IErrorHandler implementation and
' must be applied to each ChannelDispatcher.
Public Sub ApplyDispatchBehavior(ByVal description As ServiceDescription, ByVal serviceHostBase As ServiceHostBase) Implements IServiceBehavior.ApplyDispatchBehavior
Console.WriteLine("The EnforceGreetingFaultBehavior has been applied.")
For Each chanDisp As ChannelDispatcher In serviceHostBase.ChannelDispatchers
chanDisp.ErrorHandlers.Add(Me)
Next chanDisp
End Sub
' This behavior requires that the contract have a SOAP fault with a detail type of GreetingFault.
Public Sub Validate(ByVal description As ServiceDescription, ByVal serviceHostBase As ServiceHostBase) Implements IServiceBehavior.Validate
Console.WriteLine("Validate is called.")
For Each se As ServiceEndpoint In description.Endpoints
' Must not examine any metadata endpoint.
If se.Contract.Name.Equals("IMetadataExchange") AndAlso se.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex") Then
Continue For
End If
For Each opDesc As OperationDescription In se.Contract.Operations
If opDesc.Faults.Count = 0 Then
Throw New InvalidOperationException(String.Format("EnforceGreetingFaultBehavior requires a " & "FaultContractAttribute(typeof(GreetingFault)) in each operation contract. " & "The ""{0}"" operation contains no FaultContractAttribute.", opDesc.Name))
End If
Dim gfExists As Boolean = False
For Each fault As FaultDescription In opDesc.Faults
If fault.DetailType.Equals(GetType(GreetingFault)) Then
gfExists = True
Continue For
End If
Next fault
If gfExists = False Then
Throw New InvalidOperationException("EnforceGreetingFaultBehavior requires a FaultContractAttribute(typeof(GreetingFault)) in an operation contract.")
End If
Next opDesc
Next se
End Sub
#End Region
在该示例中,行为类还可实现 System.ServiceModel.Configuration.BehaviorExtensionElement,这样就可通过在应用程序配置文件中使用服务行为来插入服务行为,具体请参见下面的代码示例。
<configuration>
<system.serviceModel>
<services>
<service
name="Microsoft.WCF.Documentation.SampleService"
behaviorConfiguration="metaAndErrors">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/SampleService"/>
</baseAddresses>
</host>
<endpoint
address=""
binding="wsHttpBinding"
contract="Microsoft.WCF.Documentation.ISampleService"
/>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metaAndErrors">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true"/>
<enforceGreetingFaults />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add
name="enforceGreetingFaults"
type="Microsoft.WCF.Documentation.EnforceGreetingFaultBehavior, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
</configuration>
注解
实现 IServiceBehavior 可以在应用程序级别,修改、检查或扩展服务范围的某些执行方面:
使用 ApplyDispatchBehavior 方法可以更改运行时属性值或插入自定义扩展对象,例如错误处理程序、消息或参数拦截器、安全扩展以及其他自定义扩展对象。
使用此方法Validate在 Windows Communication Foundation (WCF 之前检查说明,) 构造执行服务,以确认它可以正确执行。
使用 AddBindingParameters 方法可以向绑定元素传递服务的自定义信息,这样绑定元素就可向服务提供正确的支持。
IServiceBehavior 对象可使用所有这些方法,但通常只有一种方法是重要的,在这种情况下,未被使用的方法可不带值返回。
备注
所有 IServiceBehavior 方法会将 System.ServiceModel.Description.ServiceDescription 和 System.ServiceModel.ServiceHostBase 对象作为参数传递。 ServiceDescription 参数只用于检查;如果您修改了这些对象,则执行行为将不确定。
若要执行 IServiceBehavior 对象的自定义任务,则必须在构造服务运行时前,将该对象添加到 Behaviors 属性。 完成此项工作有三种方法:
以编程方式,先将自定义服务行为添加到 Behaviors 属性,然后对 ICommunicationObject.Open 对象调用 System.ServiceModel.ServiceHost 方法。
创建一个可实现 IServiceBehavior 的自定义属性,并利用它来标记要修改的服务类。 ServiceHost构造对象时,WCF 使用反射来发现服务类型的属性。 如果有属性实现 IServiceBehavior,则它们将会被添加到 ServiceDescription 上的行为集合中。
扩展 System.ServiceModel.Configuration.BehaviorExtensionElement 类,以支持应用程序配置文件或计算机配置文件中的行为规范。 有关更多信息,请参见“示例”一节。
WCF 中的服务行为示例包括 ServiceBehaviorAttribute 属性、属性 System.ServiceModel.Description.ServiceThrottlingBehavior、行为 System.ServiceModel.Description.ServiceDebugBehavior 和 System.ServiceModel.Description.ServiceMetadataBehavior 行为。
方法
AddBindingParameters(ServiceDescription, ServiceHostBase, Collection<ServiceEndpoint>, BindingParameterCollection) |
用于向绑定元素传递自定义数据,以支持协定实现。 |
ApplyDispatchBehavior(ServiceDescription, ServiceHostBase) |
用于更改运行时属性值或插入自定义扩展对象(例如错误处理程序、消息或参数拦截器、安全扩展以及其他自定义扩展对象)。 |
Validate(ServiceDescription, ServiceHostBase) |
用于检查服务主机和服务说明,从而确定服务是否可成功运行。 |