2012-01-03 85 views
3

当前对于所有的wcf操作,我必须将OperationBehavior属性(用于模拟)放在每个方法的顶部。wcf服务(类)级别OperationBehavior

[OperationBehavior(Impersonation = ImpersonationOption.Allowed)] 

把它用于每种方法似乎是浪费时间。我需要的是消除将行为应用于所有可用操作的必要性。有没有办法把这个属性类的级别,以便它影响该服务类中的所有方法?

回答

4

创建自己的属性,实现IServiceBehavior和应用正确的操作行为,所有操作:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] 
public class AllowImpersonationAttribtute : Attribute, IServiceBehaviour 
{ 
    void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription desc, ServiceHostBase host) 
    { 
    var operations = desc.Endpoints.SelectMany(e => e.Contract.Operations); 
    foreach (var operation in operations) 
    { 
     operation.Behaviors.Add(new OperationBehaviorAttribute{Impersonation = ImpersonationOption.Allowed}); 
    } 
    } 
    ... // remaining methods empty 
} 
+0

喜丰富,我不需要实现任何属性。我需要的是消除将行为应用于所有可用操作的必要性。 – rovsen 2012-01-03 15:22:28

+0

@archstanton查看预期用法的更新。只需将上述属性添加到您的服务中,所有操作都将获得允许模拟行为。 – 2012-01-03 15:49:01

+0

不幸的是它没有帮助。它说“服务操作'someMethod'属于与'ISomeService'名称和'http://tempuri.org/'命名空间的合同不允许模仿” – rovsen 2012-01-04 11:34:20