IServiceBehavior.ApplyDispatchBehavior 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
用于更改运行时属性值或插入自定义扩展对象(例如错误处理程序、消息或参数拦截器、安全扩展以及其他自定义扩展对象)。
public:
void ApplyDispatchBehavior(System::ServiceModel::Description::ServiceDescription ^ serviceDescription, System::ServiceModel::ServiceHostBase ^ serviceHostBase);
public void ApplyDispatchBehavior (System.ServiceModel.Description.ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase);
abstract member ApplyDispatchBehavior : System.ServiceModel.Description.ServiceDescription * System.ServiceModel.ServiceHostBase -> unit
Public Sub ApplyDispatchBehavior (serviceDescription As ServiceDescription, serviceHostBase As ServiceHostBase)
参数
- serviceDescription
- ServiceDescription
服务说明。
- serviceHostBase
- ServiceHostBase
当前正在生成的宿主。
示例
下面的代码示例演示了如何使用配置文件中所指定的服务行为来向服务应用程序中插入自定义错误处理程序。 在该示例中,错误处理程序捕获所有异常,并将它们转换为一个自定义 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>
注解
实现 ApplyDispatchBehavior 方法可以检查或修改正在构建的 ServiceHostBase 对象,从而支持某些自定义执行方案。
备注
所有 IServiceBehavior 方法会将 System.ServiceModel.Description.ServiceDescription 和 System.ServiceModel.ServiceHostBase 对象作为参数传递。 ServiceDescription 参数仅用于自定义检查和插入;如果您修改了这些对象,则执行行为将不确定。