"global" singleton

  • Thread starter Thread starter Pawel Janik
  • Start date Start date
P

Pawel Janik

Hello.

I'd like to create utility, that writes something to a file, but this
utility shoud be easy to access from several different applications.
Because this utility have to open a file, it shoud do this only once (there
is one class instance, but every application has a acces to it). What
technology use here? How to do this?

Pawel Janik
 
Pawel,

Personally, I would run an out-of-process serviced component that you
only allow one instance of (by setting it to run in a pool, the max and min
size of the pool being one). However, you can also use remoting to have one
instance on a machine, and then access the remote object which accesses the
file.

I think that you might get better results though if you allow multiple
instances of the class to be created, and place the appropriate locks on the
file. Your class isn't the only thing that can potentially open the file,
so placing locks on the file level would be a better way to go, IMO.

Hope this helps.
 
Hi,

You could use a web service for this. or otherwise create a dedicated
server, this can be for example a window service.

To can communicate with this service either using remoting, a TCP
connection or any other medium.

Cheers,
 
Helo.

Thanks for all answers!
Personally, I would run an out-of-process serviced component that you
only allow one instance of (by setting it to run in a pool, the max and min
size of the pool being one). However, you can also use remoting to have one
instance on a machine, and then access the remote object which accesses the
file.
Thanks, servised component shoud be OK....remoting? isn't it to slow while
communicating on localhost?
I think that you might get better results though if you allow multiple
instances of the class to be created, and place the appropriate locks on the
file. Your class isn't the only thing that can potentially open the file,
so placing locks on the file level would be a better way to go, IMO.
Ok, it seems to be a good solution...but what this sample doesn't work (and
does not throw any exception?)

using System;
using System.IO;
using System.Text;

class Test
{
public static void Main()
{
string path = "MyTest.txt";

Byte[] info1 = new UTF8Encoding(true).GetBytes("This is some text in
the file. 1\r\n");
Byte[] info2 = new UTF8Encoding(true).GetBytes("This is some text in
the file. 2\r\n");

FileStream fs1 = File.Open(path, FileMode.Append, FileAccess.Write,
FileShare.Write);
fs1.Write(info1, 0, info1.Length);
Console.WriteLine("zapisalem");
FileStream fs2 = File.Open(path, FileMode.Append, FileAccess.Write,
FileShare.Write);
fs1.Write(info1, 0, info1.Length);
Console.WriteLine("zapisalem");
fs2.Write(info2, 0, info2.Length);
Console.WriteLine("zapisalem");
fs2.Write(info2, 0, info2.Length);
Console.WriteLine("zapisalem");
fs2.Close();
fs1.Close();
}
}

Pawel Janik
 
Back
Top