开发者

OperationBehaviorAttribute inheritance

开发者 https://www.devze.com 2023-03-20 19:06 出处:网络
I have base class for my services. Attribute OperationBehavior doesn\'t apply if it defined in base class and I override method in derived class. Of cause, I can duplicate code, but maybe there is oth

I have base class for my services. Attribute OperationBehavior doesn't apply if it defined in base class and I override method in derived class. Of cause, I can duplicate code, but maybe there is other way...

    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]开发者_JS百科
        void DoWork();
    }

    public class MyServiceBase
    {
        [OperationBehavior(TransactionScopeRequired = true)]
        public virtual void DoWork()
        {
        }
    }

    public class MyService : MyServiceBase, IMyService
    {
        public override void DoWork()
        {
            //No Transaction, because attribute OperationBehavior doesn't apply.
        }
    }


You will need to do something like the following:

public class MyServiceBase
{
    [OperationBehavior(TransactionScopeRequired = true)]
    public void DoWork()
    {
        DoWorkImpl();
    }

    protected virtual DoWorkImpl()
    {
    }
}

public class MyService : MyServiceBase, IMyService
{
    protected override void DoWorkImpl()
    {
        //Should have a Tx here now
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消