博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
msmq
阅读量:6636 次
发布时间:2019-06-25

本文共 6361 字,大约阅读时间需要 21 分钟。

Contract:using System;using System.Collections.Generic;using System.Linq;using System.ServiceModel;using System.Text;using System.Threading.Tasks;namespace Contract{    [ServiceContract(ConfigurationName = "IGreetingService")]    public interface IGreetingService    {        [OperationContract(IsOneWay = true)]        void SaveHello(string msg);        [OperationContract(IsOneWay = true)]        void SaveGoodBye(string msg);    }    [ServiceContract(ConfigurationName = "IGreetingService1",SessionMode =SessionMode.Required)]    public interface IGreetingService1    {        [OperationContract(IsOneWay = true)]        void SaveHello(string msg);        [OperationContract(IsOneWay = true)]        void SaveGoodBye(string msg);    }}service
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.ServiceModel;using System.Messaging;using Contract;using System.Data.Common;using System.Transactions;namespace ConsoleApplication1{ /// /// //behaviorConfiguration="batchingMessages" 应用了batchingMessages(事务处理批处理要求),下面的ReleaseServiceInstanceOnTransactionComplete要设置为false,默认为true /// [ServiceBehavior(ConfigurationName = "GreetingService",ReleaseServiceInstanceOnTransactionComplete =false)] public class GreetingService : IGreetingService { [OperationBehavior(TransactionScopeRequired =true)] public void SaveHello(string msg) { Console.WriteLine(msg+" "+Transaction.Current.TransactionInformation.DistributedIdentifier); } [OperationBehavior(TransactionScopeRequired = true)] public void SaveGoodBye(string msg) { Console.WriteLine(msg +" "+ Transaction.Current.TransactionInformation.DistributedIdentifier); } } [ServiceBehavior(ConfigurationName = "GreetingService1", TransactionAutoCompleteOnSessionClose = true)] public class GreetingService1 : IGreetingService1 { [OperationBehavior(TransactionScopeRequired = true,TransactionAutoComplete =false)] public void SaveHello(string msg) { Console.WriteLine(msg + " " + Transaction.Current.TransactionInformation.DistributedIdentifier); } [OperationBehavior(TransactionScopeRequired = true,TransactionAutoComplete =false)] public void SaveGoodBye(string msg) { Console.WriteLine(msg + " " + Transaction.Current.TransactionInformation.DistributedIdentifier); } } class Program { static void Main(string[] args) { #region 1 //string queueName = @".\private$\queue4demo"; //MessageQueue.Delete(queueName); //if (!MessageQueue.Exists(queueName)) // MessageQueue.Create(queueName, true);//第二参数代表是否是事务性队列,如果是那么binding.ExactlyOnce要设置为true,否则非事务性对要设置为false //using (ServiceHost host = new ServiceHost(typeof(GreetingService))) //{ // var binding = new NetMsmqBinding(NetMsmqSecurityMode.Message); // binding.ExactlyOnce = true; // //host.AddServiceEndpoint("IGreetingService",binding , "net.msmq://localhost/private/queue4demo"); // host.Open(); // Console.WriteLine("寄宿服务成功"); // Console.ReadKey(); //} #endregion #region 2 string queueName = @".\private$\queue4demo"; MessageQueue.Delete(queueName); if (!MessageQueue.Exists(queueName)) MessageQueue.Create(queueName, true);//第二参数代表是否是事务性队列,如果是那么binding.ExactlyOnce要设置为true,否则非事务性对要设置为false using (ServiceHost host = new ServiceHost(typeof(GreetingService1))) { var binding = new NetMsmqBinding(NetMsmqSecurityMode.Message); binding.ExactlyOnce = true; //host.AddServiceEndpoint("IGreetingService",binding , "net.msmq://localhost/private/queue4demo"); host.Open(); Console.WriteLine("寄宿服务成功1"); Console.ReadKey(); } #endregion Console.WriteLine(""); } }}client
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.ServiceModel;using Contract;using System.Transactions;namespace ConsoleApplication2{ class Program { static void Main(string[] args) { #region 1 //using (ChannelFactory
ChannelFactory = new ChannelFactory
("GreetingService")) //{ // IGreetingService proxy = ChannelFactory.CreateChannel(); // proxy.SaveHello("foo"); // proxy = ChannelFactory.CreateChannel(); // proxy.SaveGoodBye("bar"); // proxy = ChannelFactory.CreateChannel(); // proxy.SaveHello("foo"); // proxy = ChannelFactory.CreateChannel(); // proxy.SaveGoodBye("bar"); // Console.Read(); //} #endregion #region 2 using (ChannelFactory
ChannelFactory = new ChannelFactory
("GreetingService1")) { using (TransactionScope scope = new TransactionScope()) { IGreetingService1 proxy = ChannelFactory.CreateChannel(); proxy.SaveHello("hello1"); proxy.SaveGoodBye("goodbye1"); ((ICommunicationObject)proxy).Close(); scope.Complete(); } } #endregion } }}

 

转载于:https://www.cnblogs.com/kexb/p/8545996.html

你可能感兴趣的文章
react 新手学习笔记1
查看>>
安装CentOS 6.7以及CentOS 7.1
查看>>
域用户自动映射文件服务器分配的空间(只能自己看到,其它域用户无查看权限)...
查看>>
eclipse安装activiti插件和基本使用
查看>>
Linux下的DHCP中继代理配置
查看>>
计算机基本知识(8005)---HDD(硬盘驱动器Hard Disk Drive)
查看>>
什么是云计算
查看>>
ArrayList 和 CopyOnWriteArrayList
查看>>
生成树【01】生成树简介及STP原理详解
查看>>
常用命令
查看>>
Spring依赖注入的三种方式详解之三:工厂方法注入
查看>>
Exchange Server2010五大角色
查看>>
博为峰Java技术题 ——JavaSE Scanner类Ⅰ
查看>>
DUBBO - Netty 交互 服务器端
查看>>
重新学习tomcat +eclipse+mysql配置
查看>>
关于宕机
查看>>
kgdb搭建内核调试环境
查看>>
dell-r610 标配机型上增加磁盘问题 iDRAC6问题
查看>>
sshd(pam_google_authenticator)[12060]: Failed to read "/root/.google_authenticator"
查看>>
js中call用法
查看>>