Partager via


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.

Comments

  • Anonymous
    July 08, 2008
    PingBack from http://blog.a-foton.ru/2008/07/coherence-with-net/

  • Anonymous
    July 09, 2008
    This is actually a non-optimal implementation. You are completely removing the ability to do sever side processing on the grid, which is what Coherence is good at. The recommended way is to create a simple object in Java that implements the POF serialization . That way you have correct java representation of the .NET objects stored in the grid. Timur

  • Anonymous
    July 09, 2008
    Hello Timur, Thanks for the comment. Do I need to create a POF class in Java for the corresponding class in .Net? How does it work?

  • Anonymous
    April 22, 2009
    Check out my latest Linq to Coherence provider. One of the projects I am working on my spare time is trying to get .Net development with Coherence without any requirement for Java code. Look at these: http://blog.tfanshteyn.com/search/label/Coherence as well as please check out the code on Google Code http://code.google.com/p/linqtocoherence/

  • Anonymous
    January 28, 2011
    The comment has been removed