none
WCF 要解決排隊的現象... RRS feed

  • 問題

  • 小弟剛接觸WCF,寫一個Client、Server的程式
    不過發現在二三個人同時連線送訊息的狀況,都會出現排隊處理的狀況(一個處理完後,再輪到下一個)
    改了ConcurrencyMode、InstanceContextMode 也好像沒什麼用。
    無論是Client 用Thread分開連線送message或是開二個三個Client送Message都需要排隊

    弄這個弄了二天,還是不行...

    是不是程式的寫法有問題
    想請各位前輩協助一下,看一下是哪邊的邏輯是有問題@@

    架構是用Self-Hosting的方式啟動

    Client的程式
    
    WCF class
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using WCFService.Contract;
    using MessageInfo;
    
    namespace POC_Client
    {
        
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, IncludeExceptionDetailInFaults = true, 
            ConcurrencyMode = ConcurrencyMode.Multiple ,UseSynchronizationContext = false)] 
        class GeneralCalculatorClient:ClientBase<IGeneralCalculator>,IGeneralCalculator
        {
            public GeneralCalculatorClient()
                : base()
            { }
    
            public GeneralCalculatorClient(string endpointConfigurationName)
                : base(endpointConfigurationName)
            { }
    
            public GeneralCalculatorClient(Binding binding, EndpointAddress address)
                : base(binding, address)
            { }
           
            public string ReceiveMsg(string Message)
            {
               return this.Channel.ReceiveMsg(Message);
            }
    
    
    
        }
    }
    
    
    使用class的form
    
    
     static GeneralCalculatorClient calculator_tcp;
    
     private void Go_Click(object sender, EventArgs e)
            {
    
                 for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                     ParameterizedThreadStart myPar = new ParameterizedThreadStart(threadRun);
                    Thread myThread = new Thread(myPar);
                    myThread.Start(i);           
                 }          
            }
    
      static void InvocateCalclatorServiceViaConfiguration()
            {
                calculator_tcp = new GeneralCalculatorClient("selfHostEndpoint_http");
            }
    
           public void threadRun(object Index)
           {
              InvocateCalclatorServiceViaConfiguration();
              int Int_Index = (int)Index;
              string Test_Msg=Int_Index +" test msg";
              string Result_Msg="";
              Result_Msg=calculator_tcp.ReceiveMsg(Test_Msg);
           }
    
    
    Config............Top
      <system.serviceModel>
        <client>
          <endpoint address="net.tcp://localhost:9999/generalCalculator" binding="netTcpBinding" contract="WCFService.Contract.IGeneralCalculator" name="selfHostEndpoint_tcp"/>
          <endpoint address="http://localhost:8888/generalCalculator" binding="basicHttpBinding" contract="WCFService.Contract.IGeneralCalculator" name="selfHostEndpoint_http"/>
          <endpoint address="http://localhost/wcfservice/GeneralCalculatorService.svc" binding="basicHttpBinding" contract="WCFService.Contract.IGeneralCalculator" name="iisHostEndpoint"/>
          
        </client>
      </system.serviceModel>
    Config............Down
    
    
    
    Server的程式
    
    Contract class
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ServiceModel;
    
    namespace WCFService.Contract
    {
        [ServiceContract]
    
       
        public interface IGeneralCalculator
        {
            [OperationContract]
            double Add(double x, double y);
    
        
            [OperationContract]
            string ReceiveMsg(string Message);
           
        }
    }
    
    
    
    Service class
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using WCFService.Contract;  //contract 包成一個Dll
    using MessageInfo;
    using System.ServiceModel;
    using System.Threading;
    
    namespace WCFService.Service
    {
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,
                          ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
        public class GeneralCalculatorService:IGeneralCalculator
        {        
            public string ReceiveMsg(string Message)
            {
                Console.Write(DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss:ffff") + "   " +LotID + " Msg In " + "\n");
                clsCallSendMsg aa = new clsCallSendMsg();
                aa.Send(ref Message);
                Console.Write(DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss:ffff") + "   " + LotID +" Msg out " + "\n");
                return Message;
            }
    
        }
    }
    
    Host 啟動 WCF的程式
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ServiceModel;
    using WCFService.Contract;
    using WCFService.Service;
    using System.ServiceModel.Description;
    using System.Configuration;
    
    namespace WCFService.Hosting
    {
        class Program
        {
            static void Main(string[] args)
            {
                HostCalculatorSerivceViaConfiguration();
            }
    
             static void HostCalculatorSerivceViaConfiguration()
            {
                using (ServiceHost calculatorSerivceHost = new ServiceHost(typeof(GeneralCalculatorService)))
                {               
                    calculatorSerivceHost.Opened += delegate
                    {
                        //Console.WriteLine("Calculator Service has begun to listen ... ...");
    
                        Console.WriteLine("POC Server程式執行中,請勿關閉..");
                    };
    
                    calculatorSerivceHost.Open();
    
                    Console.Read();
                }
    
    
            }        
        }
    }
    
    Config-------------Top
    <configuration>
      
        <system.serviceModel>
            <behaviors>
                <serviceBehaviors>
                    <behavior name="calculatorServieBehavior">
                        <serviceMetadata httpGetEnabled="true" />
                    </behavior>
                </serviceBehaviors>
            </behaviors>
          
            <services>
                <service name="WCFService.Service.GeneralCalculatorService" behaviorConfiguration="calculatorServieBehavior">
                    <endpoint address ="" binding="basicHttpBinding" contract="WCFService.Contract.IGeneralCalculator" />
                    <endpoint  address ="" binding="netTcpBinding" contract="WCFService.Contract.IGeneralCalculator" />
                    <host>
                        <baseAddresses>
                            <add baseAddress="http://localhost:8888/generalcalculator" />                      
                            <add baseAddress="net.tcp://localhost:9999/generalCalculator" />
                        </baseAddresses>
                    </host>
                </service>
            </services>
        </system.serviceModel>
    </configuration>
    Config-------------Down


    • 已編輯 阿哲仔 2014年8月28日 上午 04:06 打錯字
    2014年8月28日 上午 03:57

所有回覆