问题.NET的Web服务写入单个文件 [英] Issue writing to single file in Web service in .NET

查看:95
本文介绍了问题.NET的Web服务写入单个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创造了.NET 2.0 Web服务,C#。我需要记录一些信息,以每当一个不同的方法由Web服务客户端调用的文件。

I have created a webservice in .net 2.0, C#. I need to log some information to a file whenever different methods are called by the web service clients.

当一个用户进程写入文件,另一个进程试图写它的问题就来了。我收到以下错误:

The problem comes when one user process is writing to a file and another process tries to write to it. I get the following error:

,因为它正由另一个进程的进程无法访问该文件。

The process cannot access the file because it is being used by another process.

这是我曾尝试在C#中实施和失败的解决方案如下。

The solutions that I have tried to implement in C# and failed are as below.


  1. 实现单例类包含code写入到文件中。

  2. 用于lock语句包住code写入到文件中。

  3. 我也曾尝试使用开源记录log4net的,但它也并不是一个完美的解决方案。

  4. 我知道记录到系统事件记录器,但我没有这样的选择。

我想知道是否存在一个完美的,完整的解决方案,以这样的问题?

I want to know if there exists a perfect and complete solution to such a problem?

推荐答案

锁定可能是失败,因为您的Web服务是由多个工作进程中运行。
你可以保护一个命名的互斥体,这是跨进程的共享访问,不像你使用锁(someobject){...}获得锁

The locking is probably failing because your webservice is being run by more than one worker process. You could protect the access with a named mutex, which is shared across processes, unlike the locks you get by using lock(someobject) {...}:

Mutex lock = new Mutex("mymutex", false);

lock.WaitOne();

// access file

lock.ReleaseMutex();

这篇关于问题.NET的Web服务写入单个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆