Wcf System.ServiceModel.FaultException-进程无法访问文件';xxx和x27;因为它正被另一个进程使用
我有一个webservice WCF,它在流程结束时记录请求:Wcf System.ServiceModel.FaultException-进程无法访问文件';xxx和x27;因为它正被另一个进程使用,wcf,c#-4.0,file-io,concurrency,Wcf,C# 4.0,File Io,Concurrency,我有一个webservice WCF,它在流程结束时记录请求: public void xxxxx(string sXmlIn, out string sXmlOut) { [...] // log log.PrintDatas = bPrintDatas; log.sXmlIn = sXmlIn; log.sXmlOut = sXmlOut; log.error = error; log.toFile(); } 这是我的日志类:
public void xxxxx(string sXmlIn, out string sXmlOut)
{
[...]
// log
log.PrintDatas = bPrintDatas;
log.sXmlIn = sXmlIn;
log.sXmlOut = sXmlOut;
log.error = error;
log.toFile();
}
这是我的日志类:
public class LogFile
{
public String sXmlIn;
public String sXmlOut;
public Error error;
private bool bPrintDatas;
public bool PrintDatas
{
set { bPrintDatas = value; }
}
private bool bInitWs;
public bool InitWs
{
get { return bInitWs; }
set { bInitWs = value; }
}
private string sMethodName;
public string MethodName
{
get { return sMethodName; }
set { sMethodName = value; }
}
private bool bCallWs;
public bool CallWs
{
get { return bCallWs; }
set { bCallWs = value; }
}
private DateTime dtDebutSession;
private DateTime dtEndSession;
private DateTime dtDebutWS;
private DateTime dtEndWS;
public void startWScall()
{
dtDebutWS = DateTime.Now;
}
public void stopWScall()
{
dtEndWS = DateTime.Now;
}
public LogFile()
{
dtDebutSession = DateTime.Now;
}
public void toFile()
{
dtEndSession = DateTime.Now;
Uri pathUri = new Uri(Path.GetDirectoryName(Assembly.GetAssembly(typeof(xxxxx)).CodeBase));
string path = pathUri.AbsolutePath + "/logs/";
path = System.Web.HttpUtility.UrlDecode(path);
System.IO.Directory.CreateDirectory(path);
string name = DateTime.Now.ToString("yyyyMMdd") + ".txt";
// create a StreamWriter and open the file
StreamWriter logFile = new StreamWriter(path + name, true, System.Text.Encoding.GetEncoding("iso-8859-1"));
logFile.Write(ToString());
logFile.Close();
}
override
public String ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Session begin : " + dtDebutSession);
sb.AppendLine("Method : " + sMethodName);
sb.AppendLine("Init WS : " + bInitWs);
sb.AppendLine("Calling WS : " + bCallWs);
sb.AppendLine("Callins WS Duration : " + (dtEndWS - dtDebutWS).TotalSeconds);
sb.AppendLine("Duration : " + (dtEndSession - dtDebutSession).TotalSeconds);
sb.AppendLine("Session end : " + dtEndSession);
sb.AppendLine("Result : " + Enum.GetName(typeof(ErrorCodes), error.RC));
if (error.RC != ErrorCodes.OK)
{
sb.AppendLine("Exception Name : " + error.ExceptionType);
sb.AppendLine("Exception Message : " + error.ErrorMsg);
}
if (error.RC != ErrorCodes.OK || bPrintDatas == true)
{
sb.AppendLine("--------------------");
sb.AppendLine(sXmlIn);
sb.AppendLine("--------------------");
sb.AppendLine(sXmlOut);
}
sb.AppendLine("----------------------------------------");
return sb.ToString();
}
public void toXML()
{
}
}
问题是,有时(我无法重做该问题),会出现异常:
日期:11/02/2014 20:19:49
例外情况:
System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]
-进程无法访问文件“C:\xxx\xxxt\xxx\bin\logs\20140211.txt”,因为它正在被删除
被另一个进程使用
堆栈跟踪:服务器堆栈跟踪:位于
System.ServiceModel.Channels.ServiceChannel.ThrowiffaultUnderstanding(消息
回复,MessageFault故障,字符串操作,MessageVersion版本,
故障转换器(故障转换器)位于
System.ServiceModel.Channel.ServiceChannel.HandlerReply(ProxyOperationRuntime
操作,ProxyRpc&rpc)在
System.ServiceModel.Channel.ServiceChannel.Call(字符串操作,
布尔单向,ProxyOperationRuntime操作,对象[]ins,
对象[]输出,TimeSpan超时)位于
System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage
方法调用,ProxyOperationRuntime操作)在
System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage
消息)异常在[0]重试:在
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
reqMsg,IMessage retMsg)在
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&
msgData,Int32类型)在xxxxxxxx在xxxxxxxx在
c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET
Files\root\24b8742a\5648f098\App_WebReferences.b9kkgfpv.0.cs:第811行
xxxxxxxx\tools.cs中的xxxxxxxx处:xxxxxxxx()处的第547行
xxxxxxxx.cs:xxxxxxxx.Page_加载处的第48行(对象发送方,事件参数e)
在xxxxxxxx.aspx.cs中:第41行
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,对象
o、 位于System.Web.UI.Control.LoadRecursive()的
System.Web.UI.Page.ProcessRequestMain(布尔值
IncludeStages前同步点,布尔值IncludeStages后同步点)
我不明白为什么,因为WCF管理WCF条目的每个实例之间的并发性
编辑:
这是在LogFile类中添加互斥的代码
private readonly Mutex mutex = new Mutex();
[...]
mutex.WaitOne();
StreamWriter logFile = new StreamWriter(path + name, true, System.Text.Encoding.GetEncoding("iso-8859-1"));
logFile.Write(ToString());
logFile.Close();
mutex.ReleaseMutex();
首先,您应该在try finally块中包装互斥锁的使用
private readonly Mutex mutex = new Mutex();
[...]
try
{
mutex.WaitOne();
StreamWriter logFile = new StreamWriter(path + name, true, System.Text.Encoding.GetEncoding("iso-8859-1"));
logFile.Write(ToString());
logFile.Close();
}
catch(Exception e)
{
// trace your exception somewhere useful here!
throw;
}
finally
{
mutex.ReleaseMutex();
}
您还错误地使用了互斥,请看下面一个非常好的示例:
我尝试使用静态互斥来避免这种情况,但问题仍然存在。我将它放在创建StreamWriter之前,并在关闭操作之后释放它。您用于WCF并发的配置是什么?默认情况下都是这样。所以是“每次调用”。向我们展示如何释放互斥锁的代码。由于您正在返回filestream,因此在释放互斥锁后,文件句柄可能仍处于打开状态。我编辑了原始的postOk,我会尝试,但恐怕无法工作,因为WCF中的每个线程都是独立的。我的问题是我不知道一个线程的互斥是否可以从另一个线程看到。这就是问题,请阅读我答案的最后一句,链接指向的正是你想要的答案!现在看来它还管用。如果没有例外,我今晚或周一接受答案:)无论如何,谢谢你的帮助