Coherence With .Net

In one of my recent
projects I was implementing Enterprise level caching using Coherence.

As you might be aware its written in Java and expects all the objects
(that needs to be cached in it) in a Portable Object Format. This is to allow
interoperability and platform independence. If the client is Java, the POF isn't mandatory, however if
its .Net it is mandatory.

Things weren't too good to start with. I personally do not favor
any platform for getting certain things done, but the fact of the matter is
that Microsoft excels in product support. If you can find issues with any
product, you are most likely to find remedies over the support site. With
Coherence, and Oracle I am afraid things aren't the way they should. They need
some effort here.

The issues I faced while implementing the solution are:

- Although my .Net class was derived from IPortableObject, I
wasn't able to serialize int!!! There was no issue with strings. The error
message was as generic as it can ever get - Java.Io.IoException: Unable to
convert type -20 to a binary type. If you do a search you will not find
anything helpful.

- If I tried to use one more class, it failed with the message:
Unable to load type 1002. Please note that its suggested in the documents that
custom user type IDs should be greater that 1000. So, my first type Employee
was 1001, and the second type Contractor was 1002. It failed miserably, and
like earlier found nothing over the Web.

So, I decided to try something else, knowing the fact that it
needs objects in binary format, I used the following:

- Decorated the class in question with [Serializable] attribute

    [Serializable]

  public class
Contractor : IPerson,
IPortableObject

- Created the object and initialized it with values

   IPerson
contractor = new Contractor();

  contractor.Name = "ABC";

  contractor.Address = "Karol Bagh";

  contractor.DoB = DateTime.Now;

- Initialized the cache

    cache
= CacheFactory.GetCache("dist-session-cache");

- Created a binary formatter, a memory stream, serialized the object,
and then inserted the array of bytes in binary format in the Coherence cache.

  BinaryFormatter
binFormat = new BinaryFormatter();

  MemoryStream memStream
= new MemoryStream();

  binFormat.Serialize(memStream, contractor);

  byte[]
array = memStream.ToArray();

  cache.Insert(1, array);

- To deserilaize, I did the following:

  byte[]
cachedObject = cache[1] as byte[];

  MemoryStream memStreamFromCache = new MemoryStream(cachedObject);

  IPerson p = (IPerson)binFormat.Deserialize(memStreamFromCache);

I am sure this is going to be helpful for all those using Coherence
with .Net

See you soon with Splunk.