【发布时间】:2014-08-19 20:56:17
【问题描述】:
在我的 MVC 3 应用程序中,我需要每 5 分钟调用一次动作控制器。 在这个动作控制器中,我将读取并解析一个 XML 文件。 所以当我有很多客户端同时调用 action 方法时,我会得到 IO 异常。我怎样才能避免这个问题并使我的动作调用线程安全。 我的控制器操作如下所示:
public PartialViewResult _warningsView(string containerId)
{
Debug.WriteLine("_warningsView");
var myXslTrans = new XslCompiledTransform();
myXslTrans.Load(@"E:/web/data/xml/alert.xslt");
myXslTrans.Transform(@"E:/.../alert.xml", @"E:/.../TransAlert.xml");
XmlSerializer serializer = new XmlSerializer(typeof(warnings));
using (FileStream fileStream = new FileStream(@"E:/.../TransAlert.xml", FileMode.Open))
{
warnings result = (warnings)serializer.Deserialize(fileStream);
return new Ext.Net.MVC.PartialViewResult
{
RenderMode = RenderMode.AddTo,
ContainerId = containerId,
Model = result.warningList,
WrapByScriptTag = false
};
}
}
我忘了提到错误:
[IOException: The process cannot access the file 'E:\web\data\xml\TransAlert.xml' because it is being used by another process.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +10527069
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1305
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +60
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync) +54
System.Xml.XmlWriterSettings.CreateWriter(String outputFileName) +155
System.Xml.XmlWriter.Create(String outputFileName, XmlWriterSettings settings) +23
System.Xml.Xsl.XslCompiledTransform.Transform(String inputUri, String resultsFile) +91
WIS_3_0.Controllers.HomeController._warningsView(String containerId) in C:\Users\Mohamed\Documents\Visual Studio 2010\Projects\wis30\WIS_3_0\Controllers\HomeController.cs:92
lambda_method(Closure , ControllerBase , Object[] ) +180
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +214
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +253
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +21
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +253
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +21
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +324
System.Web.Mvc.Controller.ExecuteCore() +106
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +91
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +34
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +19
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +48
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514928
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
【问题讨论】:
-
您得到的确切异常是什么?请发布
ex.ToString()的结果。 -
您究竟从哪里得到 IO 异常?它说了什么?
-
这个文件会随着时间而改变吗?
-
你为什么要转换成文件?使用适用于流的重载之一并使用
MemoryStream -
@JohnSaunders 异常已添加到帖子中。谢谢
标签: c# asp.net-mvc multithreading