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 <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <system.serviceModel> <bindings> <netMsmqBinding> <binding name="netmsmqbind1" exactlyOnce="true" useActiveDirectory="true" durable="true" > <security mode="None" > </security> </binding> </netMsmqBinding> </bindings> <behaviors> <endpointBehaviors> <behavior name="batchingMessages"> <transactedBatching maxBatchSize="22"/> </behavior> </endpointBehaviors> </behaviors> <services> <service name="GreetingService"> <endpoint address="net.msmq://localhost/private/queue4demo" binding="netMsmqBinding" bindingConfiguration="netmsmqbind1" behaviorConfiguration="batchingMessages" contract="IGreetingService" /> </service> <service name="GreetingService1"> <endpoint address="net.msmq://localhost/private/queue4demo" binding="netMsmqBinding" bindingConfiguration="netmsmqbind1" contract="IGreetingService1" /> </service> </services> </system.serviceModel> </configuration> 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 { /// <summary> /// //behaviorConfiguration="batchingMessages" 应用了batchingMessages(事务处理批处理要求),下面的ReleaseServiceInstanceOnTransactionComplete要设置为false,默认为true /// </summary> [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 <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <system.serviceModel> <bindings> <netMsmqBinding> <binding name="bindWithNoneSecurityMode"> <security mode="None"> </security> </binding> </netMsmqBinding> </bindings> <client> <endpoint name="GreetingService" address="net.msmq://localhost/private/queue4demo" binding="netMsmqBinding" bindingConfiguration="bindWithNoneSecurityMode" contract="IGreetingService"> </endpoint> <endpoint name="GreetingService1" address="net.msmq://localhost/private/queue4demo" binding="netMsmqBinding" bindingConfiguration="bindWithNoneSecurityMode" contract="IGreetingService1"> </endpoint> </client> </system.serviceModel> </configuration> 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<IGreetingService> ChannelFactory = new ChannelFactory<IGreetingService>("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<IGreetingService1> ChannelFactory = new ChannelFactory<IGreetingService1>("GreetingService1")) { using (TransactionScope scope = new TransactionScope()) { IGreetingService1 proxy = ChannelFactory.CreateChannel(); proxy.SaveHello("hello1"); proxy.SaveGoodBye("goodbye1"); ((ICommunicationObject)proxy).Close(); scope.Complete(); } } #endregion } } }