我有两个Web服务, GetStreets 和 GetCities ,它们同时使用另一个名为 Location 的Web服务(不要问)为什么我这样做,我只需要)。这个问题是我的.net应用程序几乎同时调用这些Web服务。
我的问题是,我怎样才能让其中一个人等到Soap Extension for Location中发生的写入完成之后才能写入,或者如何将文件重命名为写入的位置以便两个服务每次都写入不同的文件。
创建文件名的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;
}
P.S。:我已经使用并锁定了:
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 :(得分:0)
应用锁是一步,但我也提供了读访问权限: using(FileStream fs = new FileStream(filename,FileMode.Append,FileAccess.ReadWrite))