本文介绍了System.ServiceModel.FaultException-进程无法访问文件“ xxx”,因为它正在被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务WCF,它在过程结束时记录请求:

  public void xxxxx(string sXmlIn,out字符串sXmlOut)
{
[...]
//日志
log.PrintDatas = bPrintDatas;
log.sXmlIn = sXmlIn;
log.sXmlOut = sXmlOut;
log.error =错误;
log.toFile();
}

这是我的日志类:

 公共类LogFile 
{
public String sXmlIn;
public String sXmlOut;
public Error错误;

私人布尔值bPrintDatas;
public bool PrintDatas
{
set {bPrintDatas = value; }
}

私人布尔值initWs;
public bool InitWs
{
get {return bInitWs; }
set {bInitWs = value; }
}

私有字符串sMethodName;
公共字符串MethodName
{
get {return sMethodName; }
set {sMethodName = value; }
}

私人布尔bCallWs;
公共布尔CallWs
{
get {return bCallWs; }
set {bCallWs = value; }
}

私人DateTime dtDebutSession;
私人DateTime dtEndSession;

私有DateTime dtDebutWS;
私有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 =新Uri(Path.GetDirectoryName(Assembly.GetAssembly(typeof(xxxxx))。CodeBase));
字符串路径= pathUri.AbsolutePath + / logs /;
path = System.Web.HttpUtility.UrlDecode(path);
System.IO.Directory.CreateDirectory(path);

字符串名称= DateTime.Now.ToString( yyyyMMdd)+ .txt;

//创建一个StreamWriter并打开文件
StreamWriter logFile = new StreamWriter(path + name,true,System.Text.Encoding.GetEncoding( iso-8859-1)) ;

logFile.Write(ToString());
logFile.Close();
}

覆盖
public String ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(会话开始: + dtDebutSession);
sb.AppendLine( Method: + sMethodName);
sb.AppendLine( Init WS: + bInitWs);
sb.AppendLine(正在呼叫WS: + bCallWs);
sb.AppendLine( Callins WS Duration: +(dtEndWS-dtDebutWS).TotalSeconds);
sb.AppendLine( Duration: +(dtEndSession-dtDebutSession).TotalSeconds);
sb.AppendLine(会话结束: + 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();
}

公共无效toXML()
{
}
}

问题是,有时(我无法重做),出现异常:

I don't understand why because WCF manage concurrency between each instance of WCF entry.

EDIT :

This is the code with the Mutex added in LogFile class

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();
解决方案

At first you should wrap your mutex usage in a try finally block

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();
}

You are also using the Mutex wrong, have a look at the very good example here:

What is a good pattern for using a Global Mutex in C#?

这篇关于System.ServiceModel.FaultException-进程无法访问文件“ xxx”,因为它正在被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:50