views:

34

answers:

2

Here is specifically what I am seeking to accomplish:

Using .Net 3.5, I have a Windows Service, within which I have a WCF Service running. I have a client, which requests a file from the WCF Service (Net.Tcp binding), which is then copied by the WCF service to a specified location, where it can then be edited/modified by the client. The WCF Service is operating in InstanceContextMode.PerSession mode.

While the file is being edited, I wish to lock the file on the machine where the WCF Service is hosted. Once the file is returned, I wish to then unlock the file again. I do not believe that I can lock the file within the WCF Service as the service session ends once the file has been copied and I assume that the lock on the file would not be retained at that point.

One solution (which I am not excited about), is to create a thread within the Windows service, which can maintain a List<> of files and lock/unlock these as required. Of course access to this list must be synchronized for multi-threaded access. This solution would require constant polling of lists to detect new files being added and files getting removed.

A preferred approach in my mind would be to trigger an event from within the WCF Service whenever a lock/unlock is required. As opposed to constant polling. The execution of the lock/unlock task would however need to occur on a different thread (e.g. a thread running within the context of the Windows service).

We do this with Windows Forms, where from a background thread we can invoke a method to be executed on the UI messaging thread. Is there a way to "invoke" a method on another thread (not the UI messaging thread)?

Any suggestions on how to best approach this?

A: 

MS Office does this with a separate file with a name similar to the target file name. You could do similarly as long as you have full access to the destination, which it sounds like you do. I'd really try to avoid any actually locking of anything.

You'll need a checkin/checkout process and a way to recoever from failed checkins and failed checkouts and non-returned checkouts.

No Refunds No Returns
+1  A: 

Did you think about having a Lock/Unlock service that deals with this? This way you could call the locking service from WCF when required and let the other service deal with lock issue. This service would also then deal with orphan check-ins, etc.

Wagner Silveira