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); }}serviceusing 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(""); } }}clientusing 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 } }}