Tag Archives: .NET

Windows Service and Remote Desktop Mutex Sharing

Yesterday I ran into some weirdness while trying to implement System.Threading.Mutex synchronization between one of my Windows services and a colleague’s application.  The synchronization worked great in the debugger, but when running on a test system, the apps stomped all over each other.

Both had code somewhat like this:

  System.Threading.Mutex mutex = new System.Threading.Mutex(false, "SHARED_MUTEX");
  //some prep work

  //get the mutex for the shared resource
  mutex.WaitOne();

  //work with the shared resource

  //done, release the mutex
  mutex.ReleaseMutex();

Wha?!

I double checked the system mutex name, that was fine.  Didn’t seem like it should be a permissions issue.  The note in the documentation about Terminal Services kept nagging at me.  We weren’t running Terminal Services on the virtual test machine, but we were using Remote Desktop (Terminal Services lite, if you will).

I started by prefacing the mutex from my colleague’s app with “Global\”.  All of a sudden, the apps were working fine on the test system.  But I hadn’t changed my windows service app’s mutex name to look at “Global\”!

  System.Threading.Mutex mutex = new System.Threading.Mutex(false, "Global\\SHARED_MUTEX");
  //some prep work

  //get the mutex for the shared resource
  mutex.WaitOne();

  //work with the shared resource

  //done, release the mutex
  mutex.ReleaseMutex();

It turns out that because my portion was a windows service, it’s execution context changed from being part of the Terminal Services/Remote Desktop session, to being global.  The other app was still in the Terminal Services/Remote Desktop session context because it was an interactive application.  In order for it to lock on the same mutex, it had to explicitly look for the mutex in the Global namespace rather than the Local session namespace.

Makes sense, I guess, but still a little weird. Glad I didn’t spend all day tracking it down either…

How To: WCF Serialization Using Both XmlSerializerFormat and DataContractSerializer

On one of my projects, we’ve recently decided to switch from a SOAP-based interface for our SDK to a REST-based interface.  One potential problem: we can’t break the old SOAP interface.

Ideally, we’d be able to use the same underlying business objects for both access paths, but we were unsure how well WCF’s DataContractSerializer and XMLSerializerFormat would play together.  Our existing SOAP interface made heavy usage of the DataContractSerializer; I know we could have used that for the REST interfaces as well, but we needed to support XML attributes to align and integrate with other internal projects.

It turns out that you can mock up the business objects with attributes for both DataContractSerializer and XmlSerializerFormat, but the interfaces that expose these objects must be different.  So you can’t have, say, an IDataService that functions for both a DataContract interface and an Xml interface.  The attributes annotating the interface methods must be distinct, otherwise the DataContractSerializer will usurp the XmlSerializerFormat.


[ServiceContract]
[DataContractFormat]
public interface IDataService
{
  [OperationContract]
  List<MyObject> GetMyObjects();
}

[ServiceContract]
[XmlSerializerFormat]
public interface IDataService2
{
  [WebGet(UriTemplate="")]
  List<MyObject> GetMyObjects();
}

[DataContract]
[XmlRoot("myObject")]
public class MyObject
{
  [DataElement]
  [XmlAttribute("name")]
  public String Name { get; set; }
}

This may be not be ideal because now we have two interfaces. So, an alternative is to change to a single interface and pushing the attribute annotations to the implementations. That looks more like this:


public interface IDataService
{
  List<MyObject> GetMyObjects();
}

[ServiceContract]
[XmlSerializerFormat]
public class RestDataService : IDataService
{
  [WebGet(UriTemplate("")]
  public List<MyObject> GetMyObjects() 
  {
    return new List<MyObject>();
  }
}
 
[ServiceContract]
[DataContractFormat]
public class SoapDataService : IDataService
{
  [OperationContract]
  public List<MyObject> GetMyObjects()
  {
    return new List<MyObject>();
  }
}