步骤1 服务端契约使用强制事务传播流

步骤2 服务端实现使用事务特性

步骤3 host配置中使用支持事务传播的binding

不受4 客户端采用TransactionScope

经销商库存 提供创建的功能呢

namespace GreatWall.VMS.DealerStock.ServiceContract
{[System.ServiceModel.ServiceContract]public interface IDealerStockService{[System.ServiceModel.OperationContract][TransactionFlow(TransactionFlowOption.Mandatory)]void CreateDealerStock(DealerStock dealerStock);}
}

service1-

public class DealerStockService:IDealerStockService{private IBLDealerStock blDealerStock;private IBalanceVoucherService iBalanceVoucherService;public DealerStockService(){blDealerStock = DealerStockFactory.CreateDealerStock();}#region IDealerStockService 成员[OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]public void CreateDealerStock(GreatWall.VMS.DealerStock.ServiceContract.DealerStock dealerStock){Console.Write("service-DealerStock-localidentifier:" + Transaction.Current.TransactionInformation.LocalIdentifier);Console.Write("service-DealerStock-DistributedIdentifier:" + Transaction.Current.TransactionInformation.DistributedIdentifier);BLLContract.DealerStock stock = new BLLContract.DealerStock();stock.PK = -1;stock.Vin = dealerStock.Vin;stock.Amount = dealerStock.Amount;blDealerStock.SaveDealerStock(stock);}#endregion}

contract2-

namespace GreatWall.VMS.BalanceVoucher.ServiceContract
{[System.ServiceModel.ServiceContract]public interface IBalanceVoucherService{[System.ServiceModel.OperationContract][TransactionFlow(TransactionFlowOption.Mandatory)]void CreateBalanceVoucher(BalanceVoucherEntity entity);}
}

service2-

namespace GreatWall.VMS.BalanceVoucher.Service
{public class BalanceVoucherService:IBalanceVoucherService{#region IBalanceVoucherService 成员[OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]public void CreateBalanceVoucher(ServiceContract.BalanceVoucherEntity entity){Console.Write("service-DealerStock-localidentifier:" + Transaction.Current.TransactionInformation.LocalIdentifier);Console.Write("service-DealerStock-DistributedIdentifier:" + Transaction.Current.TransactionInformation.DistributedIdentifier);GreatWall.VMS.BalanceVoucher.BLLContract.IBLBalanceVoucher ibl = BalanceVoucherFactory.CreateBalanceVoucher();BLLContract.BalanceVoucherEntity blEntity = new BLLContract.BalanceVoucherEntity();blEntity.OccurTime = entity.OccurTime;blEntity.VoucherID = entity.VoucherID;ibl.CreateBalanceVoucher(blEntity);}#endregion}
}

servicehost-config

<?xml version="1.0" encoding="utf-8" ?>
<configuration><system.serviceModel><services><service name="GreatWall.VMS.DealerStock.Service.DealerStockService" behaviorConfiguration="DealerStockServiceBehavior"><endpoint address="http://localhost:8888/DealerStock" binding="wsHttpBinding" bindingConfiguration="wsTransitactionBinding" contract="GreatWall.VMS.DealerStock.ServiceContract.IDealerStockService"></endpoint></service><service name="GreatWall.VMS.BalanceVoucher.Service.BalanceVoucherService" behaviorConfiguration="BalanceVoucherServiceBehavior"><endpoint address="http://localhost:8887/BalanceVoucher" binding="wsHttpBinding" bindingConfiguration="wsTransitactionBinding" contract="GreatWall.VMS.BalanceVoucher.ServiceContract.IBalanceVoucherService"></endpoint></service></services><behaviors><serviceBehaviors><behavior name="DealerStockServiceBehavior"><serviceMetadata  httpGetEnabled="true" httpGetUrl="http://localhost:8888/DealerStock/mex"/></behavior><behavior name="BalanceVoucherServiceBehavior"><serviceMetadata  httpGetEnabled="true" httpGetUrl="http://localhost:8887/BalanceVoucher/mex"/></behavior></serviceBehaviors></behaviors><bindings><wsHttpBinding> <binding name="wsTransitactionBinding" transactionFlow="true">
        </binding></wsHttpBinding></bindings></system.serviceModel>
</configuration>

servicehost-

namespace GreatWall.VMS.ServiceHost
{class Program{static void Main(string[] args){System.ServiceModel.ServiceHost hostForDealerStock = new System.ServiceModel.ServiceHost(typeof(DealerStock.Service.DealerStockService));System.ServiceModel.ServiceHost hostForBalanceVoucher = new System.ServiceModel.ServiceHost(typeof(GreatWall.VMS.BalanceVoucher.Service.BalanceVoucherService));try{hostForDealerStock.Open();hostForBalanceVoucher.Open();Console.Write("服务已经启动");Console.Read();}catch(Exception ex){hostForDealerStock.Abort();hostForBalanceVoucher.Abort();throw ex;}finally{hostForDealerStock.Close();hostForBalanceVoucher.Close();}}}
}

client

 private void btnCommit_Click(object sender, EventArgs e){SRDealerStock.DealerStockServiceClient client = new SRDealerStock.DealerStockServiceClient();SRBalanceVoucher.BalanceVoucherServiceClient clientForBalanceVoucher = new SRBalanceVoucher.BalanceVoucherServiceClient();SRDealerStock.DealerStock dealerStock = new SRDealerStock.DealerStock();dealerStock.Amount =int.Parse(this.tbAmount.Text);dealerStock.Vin = this.tbVin.Text;SRBalanceVoucher.BalanceVoucherEntity balanceVoucherEntity = new SRBalanceVoucher.BalanceVoucherEntity();balanceVoucherEntity.OccurTime = DateTime.Now;balanceVoucherEntity.VoucherID = this.tbBalanceVoucher.Text;using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew)){try{Console.Write("client-localidentifier:" + Transaction.Current.TransactionInformation.LocalIdentifier);Console.Write("client-DistributedIdentifier:" + Transaction.Current.TransactionInformation.DistributedIdentifier);client.CreateDealerStock(dealerStock);clientForBalanceVoucher.CreateBalanceVoucher(balanceVoucherEntity);scope.Complete();MessageBox.Show("创建成功!");}catch (Exception ex){throw ex;MessageBox.Show("创建失败!");}}}

clientconfig

<?xml version="1.0" encoding="utf-8" ?>
<configuration><system.serviceModel><bindings><wsHttpBinding><binding name="WSHttpBinding_IDealerStockService" closeTimeout="00:01:00"openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"bypassProxyOnLocal="false" transactionFlow="true" hostNameComparisonMode="StrongWildcard"maxBufferPoolSize="524288" maxReceivedMessageSize="65536"messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"allowCookies="false"><readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"maxBytesPerRead="4096" maxNameTableCharCount="16384" /><reliableSession ordered="true" inactivityTimeout="00:10:00"enabled="false" /><security mode="Message"><transport clientCredentialType="Windows" proxyCredentialType="None"realm="" /><message clientCredentialType="Windows" negotiateServiceCredential="true"algorithmSuite="Default" /></security></binding><binding name="WSHttpBinding_IBalanceVoucherService" closeTimeout="00:01:00"openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"bypassProxyOnLocal="false" transactionFlow="true" hostNameComparisonMode="StrongWildcard"maxBufferPoolSize="524288" maxReceivedMessageSize="65536"messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"allowCookies="false"><readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"maxBytesPerRead="4096" maxNameTableCharCount="16384" /><reliableSession ordered="true" inactivityTimeout="00:10:00"enabled="false" /><security mode="Message"><transport clientCredentialType="Windows" proxyCredentialType="None"realm="" /><message clientCredentialType="Windows" negotiateServiceCredential="true"algorithmSuite="Default" /></security></binding><binding name="" /></wsHttpBinding></bindings><client><endpoint address="http://localhost:8887/BalanceVoucher" binding="wsHttpBinding"bindingConfiguration="WSHttpBinding_IBalanceVoucherService"contract="SRBalanceVoucher.IBalanceVoucherService" name="WSHttpBinding_IBalanceVoucherService"><identity><userPrincipalName value="lizhch@GREATWALL.LOCAL" /></identity></endpoint><endpoint address="http://localhost:8888/DealerStock" binding="wsHttpBinding"bindingConfiguration="WSHttpBinding_IDealerStockService" contract="SRDealerStock.IDealerStockService"name="WSHttpBinding_IDealerStockService"><identity><userPrincipalName value="lizhch@GREATWALL.LOCAL" /></identity></endpoint></client></system.serviceModel>
</configuration>

转载于:https://www.cnblogs.com/feidaochuanqing/p/3383537.html

WCF在多个服务中使用事物相关推荐

  1. Entity Framework 6 Recipes 2nd Edition(9-7)译-在WCF服务中序列化代理

    9-7. 在WCF服务中序列化代理 问题 从一个查询里返回一个动态代理对象,想要把它序列为一个POCO(Plain-Old CLR Objects)对象. 实现基于POCO实体对象, 在运行时,EF会 ...

  2. wcf客户端动态嗅探服务实例

    参照博客园蒋金楠老师的博客和MSDN的一片文章,现在把wcf客户端动态嗅探wcf服务用一个实例来做出总结. 在以往的wcf客户端应用的时候,我们需要提供客户服务地址和端口或者管道,当然有时候需要我们服 ...

  3. PHP 如何在Redis中实现事物(事物提交和事物回滚)

    讲了这么多Redis的使用,今天我们来讲下Redis的事物 1.首先,我们来看一下Redis中事物相关的指令, 命令原型       命令描述 MULTI   用于标记事务的开始,其后执行的命令都将被 ...

  4. windows 服务中托管asp.net core

    在windows 服务中托管asp.net core SDK 2.1.300 官方示例 1.添加运行标识符 xml <PropertyGroup> <TargetFramework& ...

  5. 微服务中的面向切面编程和更多模式

    目录 介绍 如何建立这篇文章? 在Java中使用代码(11 +,Spring boot 2.2 +,Spring Boot AOP,AspectJ) 在C#中使用代码(7,.NET MVC Core ...

  6. Silverlight+WCF+ArcObjects获取地图服务图层列表的实现

    很久没有写博客了,最近一直忙考试,只是被悲催的数学物理方程真的把我给伤着了..... 忙完了考试,又开始了导师的项目,然而发现项目的很多东西,用原始的ArcGIS Api for Silverligh ...

  7. 一起谈.NET技术,使用WCF实现SOA面向服务编程—— 架构设计

    SOA本身就是一种面向企业级服务的系统架构,简单来说,SOA就是一种进行系统开发的新的体系架构,在基于SOA架构的系统中,具体应用程序的功能是由 一些松耦合并且具有统一接口定义方式的组件(也就是ser ...

  8. UML笔记一:UML概述三(UML构成、UML中的事物)

    1.UML中的构成: UML的基本结构块: ---UML中的事物 ---UML中的关系 ---UML中的图 UML的规则 UML中的公共机制 --规格说明 --修饰 --通用划分 --扩展机制 2.U ...

  9. 攻克微服务中的最大难点:用户数据

    今天 数人云与大家分享的文章将探讨微服务架构的创建与开发工作当中最为困难的部分--用户数据. 只有我们摆脱自己的依赖时微服务才能起作用,换言之,存在于单一数据库上的多任务进程并不是真正的微服务.使用S ...

  10. WCF 第二章 契约 WSDL中的操作名字、类型、操作和命名空间

    WCF 根据服务端源代码中定义的内部类名称和属性来生成外部暴露服务实现.这些实现通过服务中的MEX终结点暴露出来并在设计阶段时被客户端以WSDL形式使 用.接下来在客户端,WSDL会被用来写一些代码来 ...

最新文章

  1. R语言dplyr包对独特值(distinct)进行计数实战
  2. 计算机信息技术在教学中的发展,浅析计算机在信息技术教学中的应用
  3. 以短带长进军网综,西瓜视频能否干过“优爱腾”?
  4. Spring Batch –使用JavaConfig替换XML作业配置
  5. 分类分布(categorical分布)
  6. maven集成tomcat插件启动报错
  7. gnome 3.4 评测
  8. jsdroid 教程_服装制版教程如何利用紧身胸衣来制作无袖服装417才智服装
  9. 随机森林:对UCI数据集的分类实现
  10. synchronized锁升级之偏向锁
  11. ECharts 前端数据可视化
  12. 4芯网线和8芯网线的网速有什么区别
  13. CSS雪碧图制作emoji表情包
  14. 2021-07-27-
  15. Appium 手机 App 自动化代码说明_启动微信app
  16. ImportError: DLL load failed while importing win32api
  17. cmd脚本命令--之基本命令锦集
  18. 中国电子设计自动化(EDA)软件行业未来发展趋势与投资前景分析报告22022-2028年版
  19. 究极花里胡哨的渐变是什么样子的?
  20. 一个神奇的网站 Papers with code

热门文章

  1. 高铁:繁忙时限制上车补票
  2. THAAD反导必将部署,各方已接受事实
  3. 输入法候选词排列的两种新方式
  4. 在一台机器上使用gluster
  5. 一键安装iis php mysql环境_Windows Server服务器环境配置:一键安装IIS+PHP+MySQL环境...
  6. ubuntu 下c语言开发环境搭建,Ubuntu下Object-c的开发环境搭建
  7. timthumb+php,timthumb.php生成缩略图参数详解
  8. golang key map 所有_Golang语言小技巧(踩坑)
  9. 过滤器和拦截器的区别_拦截器和过滤器的区别
  10. Ansible:命令应用基础