2013-04-24 79 views
0

我有兩個Web服務,GetStreetsGetCities,在那個叫同時使用另一個Web服務位置(不要問我爲什麼這樣做,我只需要)。問題在於這些Web服務在我的.net應用程序中幾乎同時被調用。Web服務

我的問題是,我怎麼能讓他們中的一個人等待,直到Soap擴展中的位置寫入完成才能寫入,或者如何將文件重命名爲寫入的位置,以便兩個服務每次寫入不同的文件。

創建文件名中的GetInitializer方法:

public override object GetInitializer(Type WebServiceType) 
    { 
     string filename = string.Format("{0}_{1}_{2}.log", WebServiceType.FullName, methodName, DateTime.Now.ToString("yyyyMMdd")); 
     return ConfigurationManager.AppSettings["SoapLogPath"] + "\\" + filename; 
    } 

PS:我已經使用了使用和鎖:

public override void ProcessMessage(SoapMessage message) 
    { 
     lock (thisLock) 
     { 
      switch (message.Stage) 
      { 
       case SoapMessageStage.BeforeSerialize: 
        //WriteOutput(message); 
        break; 
       case SoapMessageStage.AfterSerialize: 
        WriteOutput(message); 
        break; 
       case SoapMessageStage.BeforeDeserialize: 
        WriteInput(message); 
        break; 
       case SoapMessageStage.AfterDeserialize: 
        break; 
       default: 
        break; 
      } 
     } 
    } 

    /// <summary> 
    /// The method writes away the input message 
    /// </summary> 
    /// <param name="message">The soap message that will be logged</param> 
    public void WriteOutput(SoapMessage message) 
    { 
     newStream.Position = 0; 
     using(FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write)) 
     { 
      using(StreamWriter w = new StreamWriter(fs)) 
      { 
       string soapString = (message is SoapServerMessage) ? Environment.NewLine + "Output SoapResponse" : "Output SoapRequest"; 
       w.WriteLine("-----" + soapString + " at " + DateTime.Now); 
       Copy(newStream, fs); 
       newStream.Position = 0; 
       Copy(newStream, oldStream); 
      } 
     } 
     //Force clean up 
     GC.Collect(); 
    } 

回答

0

應用鎖是一步到位,但我也給讀訪問: (FileStream fs = new FileStream(filename,FileMode.Append,FileAccess.ReadWrite))