欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

wcf 不会rollback,请大伙儿帮忙看看

更新时间: 2014-01-05 01:59:35 责任编辑: Author_N1

 

wcf 不会rollback,请大家帮忙看看。
在学习wcf,最近学到wcf事务。自己写了一个示例,就是不会rollback,请大家帮忙看看。
服务器端代码:
namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(Calc));

            WSHttpBinding wsbinding = new WSHttpBinding();

            wsbinding.TransactionFlow = true;
            host.AddServiceEndpoint(typeof(ICalc), wsbinding, "http://localhost:8003");

            host.Opened += delegate
            {
                Console.WriteLine("services is open!");
            };

            host.Open();
            Console.ReadKey();
        }
    }

    [ServiceContract]
    public interface ICalc
    {
        [OperationContract]
        [TransactionFlow(TransactionFlowOption.Mandatory)]
        double Add(double x);
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class Calc : ICalc
    {
        [OperationBehavior(TransactionScopeRequired = true)]
        public double Add(double x)
        {
            Console.WriteLine(Transaction.Current.TransactionInformation.CreationTime);
            Console.WriteLine(Transaction.Current.TransactionInformation.LocalIdentifier);
            return x + 1;
        }
    }
}

然后客户端代码:
    class Program
    {
        static void Main(string[] args)
        {
            WSHttpBinding ws = new WSHttpBinding();
            ws.TransactionFlow=true;

            ChannelFactory<ICalc> mychannel = new ChannelFactory<ICalc>(ws, "http://localhost:8003");
            mychannel.Open();

            ICalc myclient = mychannel.CreateChannel();

            double x = 3.5;
            double y = 0;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
            {                
                try
                {
                    Console.WriteLine(Transaction.Current.TransactionInformation.CreationTime);
                    Console.WriteLine(Transaction.Current.TransactionInformation.LocalIdentifier);
                    y = myclient.Add(x);
                    throw new Exception("throw new exception");
                    scope.Complete();
                    mychannel.Close();
                }
                catch (Exception ex)
上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 01:59:35
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/4185/
本文WWW.DOC100.NET DOC100.NET版权所有。