wcf服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MyWcfService
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [TransactionFlow(TransactionFlowOption.Allowed)]
        string ExecuteSql(string value);
    }

}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MyWcfService
{

    [ServiceBehavior(ReleaseServiceInstanceOnTransactionComplete = false, InstanceContextMode = InstanceContextMode.PerCall)]
    public class Service1 : IService1
    {

        [OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
        public string ExecuteSql(string value)
        {

            SqlHelper sql = new SqlHelper();
            return sql.ExecuteNonQuery(value).ToString();//如果是mysql帮助文档中的此方法,会报错,因为mysql数据库不支持分布式事务
        }

    }
}

  宿主serviceHost

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using MyWcfService;

namespace serviceHost
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(Service1));
            host.Open();
            Console.WriteLine("open");
            Console.ReadKey();
        }
    }
}

  宿主的配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<system.serviceModel>
		<behaviors>
			<serviceBehaviors>
				<behavior name="serviceDebuBehavior">
					<!--可以在客户端获取异常-->
					<serviceDebug includeExceptionDetailInFaults="true" />
				</behavior>
			</serviceBehaviors>
		</behaviors>
		<services>
			<service name="MyWcfService.Service1" behaviorConfiguration="serviceDebuBehavior">
				<!--以上异常配置-->
				<endpoint address="net.tcp://127.0.0.1:3721/calculatorservice"
						  binding="netTcpBinding"
						  contract="MyWcfService.IService1"
						  bindingConfiguration="transactionBinding"
						  />
			</service>
		</services>
		<bindings>
			<netTcpBinding>
				<!--transactionFlow=true 开启事务  必须配置,而且宿主和客户端都要配置-->
				<binding name="transactionBinding"  transactionFlow="true" >
					<reliableSession  enabled="true"/>
					<security></security>

				</binding>

			</netTcpBinding>

		</bindings>
	</system.serviceModel>
</configuration>

  //客户端 调用者ClinetAPP

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Transactions;
using MyWcfService;

namespace ClinetAPP
{
    class Program
    {
        static void Main(string[] args)
        {

            ChannelFactory<IService1> channl = new ChannelFactory<IService1>("WcfService");
            try
            {
                IService1 service = channl.CreateChannel();
                using (TransactionScope trans = new TransactionScope())
                {
                    string sql1 = "insert into demo values('1','2')";
                    string sql2 = "update demo set pwd2='2'";
                    string s1 = service.ExecuteSql(sql1);
                    string s2 = service.ExecuteSql(sql2);
                    Console.WriteLine(s1 + s2);
                    Console.ReadKey();
                    trans.Complete();//此处提交事务。如果没有执行此方法,事务就会回滚。
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}

  //客户端配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<system.serviceModel>
		<client>

			<endpoint  name="WcfService" address="net.tcp://127.0.0.1:3721/calculatorservice"
					  binding="netTcpBinding"
					  contract="MyWcfService.IService1"
					     bindingConfiguration="transactionBinding"/>
		</client>
		<bindings>
			<netTcpBinding>
				<binding name="transactionBinding" transactionFlow="true" >
					<reliableSession  enabled="true"/>
					<security></security>

				</binding>

			</netTcpBinding>

		</bindings>
	</system.serviceModel>
</configuration>

  

wcf事务的更多相关文章

  1. WCF入门(十)---WCF事务

    事务处理在WCF(Windows Communication Foundation)是一套遵循一些性质,统称为ACID的操作.这里,如果一个操作出现故障,整个系统就会自动失败.如网上订单生成,就可能使 ...

  2. wcf事务(随记)

    ----------------------------------------------------wcf事务:1.ACID:原子性.一致性.隔离性.持久性:2.事务:添加命名空间(using S ...

  3. WCF分布式开发步步为赢(12):WCF事务机制(Transaction)和分布式事务编程

    今天我们继续学习WCF分布式开发步步为赢系列的12节:WCF事务机制(Transaction)和分布式事务编程.众所周知,应用系统开发过程中,事务是一个重要的概念.它是保证数据与服务可靠性的重要机制. ...

  4. WCF事务应用[转]

    在B2B的项目中,一般用户注册后,就有一个属于自己的店铺,此时,我们就要插入两张表, User和Shop表. 当然,要么插入成功,要么全失败. 第一步: 首先看一下项目的结构图: 第二步: 准备工作, ...

  5. WCF学习笔记之事务编程

    WCF学习笔记之事务编程 一:WCF事务设置 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元: WCF通过System.ServiceModel.TransactionFlowA ...

  6. WCF基础之事务

    说到事务,我最先想到的是“回滚”. 百科:事务是恢复和并发控制的基本单位.事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.好了,具体的就不多复制了. 我小试了一下 ...

  7. 跟我一起学WCF(13)——WCF系列总结

    引言 WCF是微软为了实现SOA的框架,它是对微乳之前多种分布式技术的继承和扩展,这些技术包括Enterprise Service..NET Remoting.XML Web Service.MSMQ ...

  8. 跟我一起学WCF(10)——WCF中事务处理

    一.引言 好久没更新,总感觉自己欠了什么一样的,所以今天迫不及待地来更新了,因为后面还有好几个系列准备些,还有很多东西需要学习总结的.今天就来介绍下WCF对事务的支持. 二.WCF事务详解 2.1 事 ...

  9. C#综合揭秘——细说事务

    引言 其实事务在数据层.服务层.业务逻辑层多处地方都会使用到,在本篇文章将会为大家一一细说. 其中前面四节是事务的基础,后面的三节是事务的重点,对事务有基础的朋友可以跳过前面四节. 文章有错漏的地方欢 ...

随机推荐

  1. 注册页面手机验证码无跳转接收[html+js+ajax+php]

    [学习笔记] 来源:注册时需要使用短信验证码,但是注册的时候,点击接收验证码时,会产生跳转(尼玛,这不是我想要的啊!o(╥﹏╥)o) 查询资料和看书之后,知道使用js+ajax可以实现,就从网上找了一 ...

  2. Uva101-STL模拟

    一道有点复杂的STL模拟题,对STL迭代器不太熟悉改了好久,最后总算A了出来. 感觉用数组更方便...但是为了练习STL嘛 对比白书上的代码,我写的还是傻了点.一开始没有理解四个操作的意思,单纯的模拟 ...

  3. hdu 6319 Problem A. Ascending Rating (2018 Multi-University Training Contest 3 A)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=6319 思路: 单调队列倒着维护,队列里面剩下的值的数量就是这一段区间的count值,如样例第一个区间:3 ...

  4. MT【213】二次曲线系方程

    (2013北大夏令营)函数$y=x^2+ax+b$与坐标轴交于三个不同的点$A,B,C$,已知$\Delta ABC$的外心$P$在$y=x$上,求$a+b$的值. 解:由二次曲线系知识知三角形的外接 ...

  5. 【POJ 3176】Cow Bowling(DP)

    题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...

  6. 自学Linux Shell6.2-用户自定义环境变量

    点击返回 自学Linux命令行与Shell脚本之路 6.2-用户自定义环境变量 1.设置局部用户定义变量 一旦启动bash shell(或者执行一个shell脚本),你就能创建这个shell进程可见的 ...

  7. luogu2577/bzoj1899 午餐 (贪心+dp)

    首先,应该尽量让吃饭慢的排在前面,先按这个排个序 然后再来决定每个人到底去哪边 设f[i][j]是做到了第i个人,然后1号窗口目前的总排队时间是j,目前的最大总时间 有这个i和j的话,再预处理出前i个 ...

  8. A1008. Elevator

    The highest building in our city has only one elevator. A request list is made up with N positive nu ...

  9. 织梦dedecms自定义搜索可以按照附加表字段进行搜索

    因为需要在网上搜索了一下找到此修改方法: 首先 是新建模型:商标信息 模型:附加表为dede_shangbiao ,附加字段先添加了2个,sbID(商标ID)和chuangyi(创意说明)然后 打开需 ...

  10. PrintNumber.java

    /****************************************************************************** * Compilation: javac ...