实现IServiceBehavior会影响ServiceBehavior属性吗?

时间:2009-08-17 08:19:08

标签: c# .net wcf attributes servicebehavior

我有一个WCF服务。我需要在实现ServiceContract的类中实现IServiceBehavior。我在该类上有一些指定服务行为的属性。我想询问在实现属性中指定的IServiceBehavior行为后是否仍然适用。

基本上

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, 
                 ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyService : IMyService
{
...
}

相同
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, 
                 ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyService : IMyService, IServiceBehavior
{
...
}

同样的意思是我仍然有ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)

我对IServiceBehavior的实现如下:

void AddBindingParameters(ServiceDescription serviceDescription, 
                          ServiceHostBase serviceHostBase, 
                          Collection<ServiceEndpoint> endpoints, 
                          BindingParameterCollection bindingParameters)
{            
}

void ApplyDispatchBehavior(ServiceDescription serviceDescription, 
                           ServiceHostBase serviceHostBase)
{
    IErrorHandler handler = new ErrorHandler();
    foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
    {
        dispatcher.ErrorHandlers.Add(handler);
    }
}

void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{            
}

我只是想实现集中错误处理,我不想以任何其他方式改变服务行为。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

是的,ServiceBehaviorAttribute中的行为仍然适用;您的IServiceBehavior只是为您提供了一种进一步定制运行时的方法。