2014-03-01 39 views
0

我想從我的客戶端應用程序發送一條消息給我的WCF服務對於一個給定的時間跨度 什麼是我想要找出多少次打電話給我WCF服務被客戶端擊中。如何找到一個WCF服務多少次由客戶端

這裏是一個客戶端應用程序:

var messages = new String[1]; 
messages[0] = "My simple request"; 
    var client2 = new Operator(); 
var startTime = DateTime.Now; 
var timeoutSpan = TimeSpan.FromMinutes(.06); 
var count =0; 

while ((DateTime.Now - startTime) <= timeoutSpan) 
{ 
    foreach (string message in messages) 
    { 
     txtRequest.Text = message; 
     sendMessageResult = client.sendMessage("02", txtRequest.Text); 
     count++; 
    } 
} 

// this counts how many request I sent 
textBox1.Text = count.ToString(); 
// this shows the server side count 
textBox2.Text = client2.TotalHits().ToString(); 

這裏是我的WCF服務應用程序

編輯:

public byte sendMessage(string strMsgId, string strMessage) 
    { 
    byte result = 1; 

    try 
    { 
     if (strMsgId == "02") 
     { 

     lock (_lock) 
       { 
        ++_count; 
       } 
      // Logging------- 
      // Build timestamp string 
      var currentDateTime = DateTime.Now; 
      string timeStampString = currentDateTime.ToString("yyyy-MM-dd hhmmssfff"); 

      // Build filename for Inbound messages, concat timestamp and .txt extension. 
      string debugFileName = "C:\\Inboundmessage" + " " + timeStampString + ".txt"; 
      var inboundMessageLog = new StreamWriter(debugFileName, false, System.Text.Encoding.Default); 

      // Write to the file: 
      inboundMessageLog.WriteLine(DateTime.Now); 
      inboundMessageLog.WriteLine("Time = {0}", currentDateTime.ToString("yyyy-MM-dd hh:mm:ss:fff tt")); 
      inboundMessageLog.WriteLine("{0}{1}{2}", "Inbound Message:", Environment.NewLine, strMessage); 
     inboundMessageLog.WriteLine("{0}{1}", "count:", _count); 
      inboundMessageLog.Close(); 

      // -------------- 
      result = 0; 
     } 
} 
catch 
{ 
    //Failed 
    result = 1; 
} 
return result; 

//這裏是一個計數方法

public int TotalHits() 
    { 
     return _count; 
    } 

我看到日誌的計數> 0,但爲什麼在客戶端上我沒有看到變化,當我打電話TotalHit方法?

+0

你只關心這個被稱爲或這是由某些特定的客戶調用的,您必須記錄一些審計數據? –

+0

這是我的客戶打電話,我想記錄審計數據 – HXD

+0

看到這個[WCF可擴展性 - IOperationBehavior文章(http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/12/wcf-可擴展性 - ioperationbehavior.aspx) –

回答

0

一個問題: 究竟是什麼你想在這裏實現?

解決的辦法是兩個步驟: 第1步:在IService啓用會話

[ServiceContract()] 
public interface IService 
{ 
    [OperationContract] 
    string SayHello(string toWhom); 

    [OperationContract] 
    int TotalHits(); 

} 

第2步:添加ServiceBehavior標籤,使辛格爾頓

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)] 
public Service: IService 
{ 
    int count; 
    object _lock = new object(); 

    string SayHello(string toWhom) 
    { 
     lock(_lock) { ++count; } 

     return "Hello, " + toWhom; 
    } 

    int TotalHits() 
    { 
     return count; 
    } 
} 
+0

我想知道在我的WCF服務的操作多少次被調用。我想添加一個計數器。 – HXD

+0

更新了代碼。這裏的關鍵字是'InstanceContextMode.Single'和'lock'休息都很明顯。爲什麼你需要計算順便說一句? –

+0

我想計算從上述代碼中設置的時間跨度指向它的兩個客戶端(相同服務)調用了多少次。 – HXD

相關問題