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)
{
Transaction.Current.Rollback();
mychannel.Abort();
Console.WriteLine(ex.Message);
}
}
Console.WriteLine(x);
Console.WriteLine(y);
Console.ReadKey();
}
}
然后每次都是打印
x=3.5
y=4.5
好郁闷啊,我不是有抛出异常了吗?为什么不会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)
{
Transaction.Current.Rollback();
mychannel.Abort();
Console.WriteLine(ex.Message);
}
}
Console.WriteLine(x);
Console.WriteLine(y);
Console.ReadKey();
}
}
然后每次都是打印
x=3.5
y=4.5
好郁闷啊,我不是有抛出异常了吗?为什么不会Rollback呢。
...全文
相关推荐
WCF分布式事务Demo(EF)
context.Database.BeginTransaction()) { try { // 执行数据库操作 // ... transaction.Commit(); } catch { transaction.Rollback
Visual Basic.NET数据库编程从入门到精通
通过BEGIN TRANSACTION、COMMIT和ROLLBACK命令控制事务的开始、提交和回滚。9.
.net与SQL编程
**事务管理**:确保数据库操作的一致性和完整性,如BEGIN TRANSACTION、COMMIT、ROLLBACK。6.
传智播客IT技术全攻略:.NET与SQL学习宝典
NET开发平台是微软推出的一个软件框架,它为开发人员提供了丰富的功能,支持多种编程语言,包括C#、VB.NET等,使得开发者能够创建多平台应用程序。本章将带您了解.NET的基本概念,核心技术,以及它在企业级开发中的重要性。.NET是一种由微软公司开发的,旨在使开发者能够使用一种语言构建跨平台应用程序的软件框架。它的核心优势体现在以下几个方面:跨平台能力:.NET Core是.NET的开放源代码版本,支持在Windows、Linux和macOS上运行,为开发者提供了更大的灵活性。
29Linux基础命令详解——用户管理.docx
29Linux基础命令详解——用户管理


Web Services
