wcf 不会rollback,请大家帮忙看看。

专注研发ERP低代码开发平台 专注研发ERP低代码开发平台 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在学习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呢。
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部