开发者

OperationBehaviorAttribute inheritance

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜