本文介绍了WCF服务不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个类似的服务代码: [ServiceContract] public interface IService1 { [OperationContract] void MyOperation1(); } [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,ConcurrencyMode = ConcurrencyMode.Multiple)] public class service1:IService1 { public service1() { Console。 WriteLine( 创建实例); } public void MyOperation1() { Console.WriteLine( starting ..); Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId); Thread.Sleep( 1000 ); Console.WriteLine( Ending ..); } } 客户代码: 静态 void Main( string [] args) { Thread [] t = new 线程[ 10 ]; NetTcpBinding myBinding = new NetTcpBinding(); EndpointAddress myEndpoint = new EndpointAddress( net .tcp://本地主机:8000 /为MyService); ChannelFactory< IService1> myChannelFactory = new ChannelFactory< IService1>(myBinding,myEndpoint); IService1 instance = myChannelFactory.CreateChannel(); for ( int i = 0 ; i < 10 ; i ++) {t [i] = new 线程( new ThreadStart( delegate () { instance.MyOperation1(); })); } for ( int i = 0 ; i < 10 ; i ++) {T [I]。开始(); } } 我得到这样的输出(逐个调用进程) ): 创建实例 开始.. 3 结束。 。 开始.. 3 结束.. 开始.. 12 结束.. 开始.. 3 结束.. 开始.. 12 结束.. 开始.. 3 结束.. 开始.. 12 结束.. 开始.. 3 结束.. 开始.. 12 结束.. 开始.. 3 结束.. 为什么要打电话不是服务并行处理吗? 我希望结果类似(所有线程应该并行启动): 创建实例 开始.. 开始.. 开始.. 开始.. 3 结束.. 4 6 结束.. 解决方案 信不信由你,但在你的情况下,线程实际上同时工作。只有你没有机会注意到这一点,只是因为每个线程更快地执行它的body方法然后线程切换活动才能发挥作用。 那就是说,你对线程的使用完全违背了多线程的目的。具有这样的短线程功能几乎没有任何实际意义。您只会浪费一些操作系统和CPU资源,因为每个线程的创建和终止都会产生相当大的开销,而不会获得线程的任何好处。 -SA I have a service code something like:[ServiceContract]public interface IService1{ [OperationContract] void MyOperation1();}[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,ConcurrencyMode=ConcurrencyMode.Multiple)]public class service1 : IService1{ public service1() { Console.WriteLine("creating instance"); } public void MyOperation1() { Console.WriteLine("starting.."); Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId); Thread.Sleep(1000); Console.WriteLine("Ending.."); }}Client code :static void Main(string[] args) { Thread[] t = new Thread[10]; NetTcpBinding myBinding = new NetTcpBinding(); EndpointAddress myEndpoint = new EndpointAddress("net.tcp://localhost:8000/MyService"); ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>(myBinding, myEndpoint); IService1 instance = myChannelFactory.CreateChannel(); for (int i = 0; i < 10; i++) { t[i] = new Thread(new ThreadStart(delegate() { instance.MyOperation1(); })); } for (int i = 0; i < 10; i++) { t[i].Start(); } }I am getting output like this(calls being processes one by one):creating instancestarting..3Ending..starting..3Ending..starting..12Ending..starting..3Ending..starting..12Ending..starting..3Ending..starting..12Ending..starting..3Ending..starting..12Ending..starting..3Ending..Why are the calls not processes parallely by service?I want the result to be something like(all threads should start parallely):creating instancestarting..starting..starting..starting..3Ending..46Ending.. 解决方案 Believe or not, but in your case the thread actually work concurrently. Only you don't get a chance to notice that, just because each thread executes its body method faster then the thread switching activity can come into play.That said, your use of the threads totally defeats the purpose of multithreading. Having such short thread functions hardly makes any practical sense. You only waste some OS and CPU resources, because creation and termination of each thread impose considerable overhead, without getting any benefits of threading.—SA 这篇关于WCF服务不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-13 02:01