问题
I am trying to implement a very simple PDX autoserialization in Geode. I've created a domain class of my own with a zero arg constructor:
public class TestPdx
{
public string Test1 { get; set; }
public string Test2 { get; set; }
public string Test3 { get; set; }
public TestPdx() { }
}
Now I want this class to auto serialize. I start a server cache with the following cache.xml where I attempt to register this type for auto PDX:
<?xml version="1.0" encoding="UTF-8"?>
<cache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://geode.apache.org/schema/cache"
xsi:schemaLocation="http://geode.apache.org/schema/cache
http://geode.apache.org/schema/cache/cache-1.0.xsd"
version="1.0">
<cache-server/>
<pdx>
<pdx-serializer>
<class-name>org.apache.geode.pdx.ReflectionBasedAutoSerializer</class-name>
<parameter name="classes"><string>TestPdx</string></parameter>
</pdx-serializer>
</pdx>
<region name="webclient" refid="REPLICATE_PERSISTENT"/>
</cache>
and then run the following code:
static void Main(string[] args)
{
// 1. cache
CacheFactory cacheFactory = CacheFactory.CreateCacheFactory();
Cache cache = cacheFactory
.SetSubscriptionEnabled(true)
.SetPdxReadSerialized(true)
.Create();
Serializable.RegisterPdxSerializer(new ReflectionBasedAutoSerializer());
RegionFactory regionFactory = cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY);
IRegion<string, TestPdx> region = regionFactory.Create<string, TestPdx>("webclient");
// 3. TestPx object
TestPdx t = new TestPdx();
t.Test1 = "test1";
t.Test2 = "test2";
t.Test3 = "test3";
region["1"] = t;
// 4. Get the entries
TestPdx result1 = region["1"];
// 5. Print result
Console.WriteLine(result1.Test1);
Console.WriteLine(result1.Test2);
Console.WriteLine(result1.Test3);
}
This code is crashing at line region["1"] = t;
with error
GFCLI_EXCEPTION:System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at apache.geode.client.SerializationRegistry.GetPDXIdForType(SByte* , SharedPtr<apache::geode::client::Serializable>* )
So I haven't registered the PDX type properly. How do you do that with native client?
THANKS
回答1:
An answer here is to implement IPdxSerializable
in the TestPdx
as follows:
public class TestPdx : IPdxSerializable
{
public string Test1 { get; set; }
public string Test2 { get; set; }
public string Test3 { get; set; }
public int Pid { get; set; }
public void ToData(IPdxWriter writer)
{
writer.WriteString("Test1", Test1);
writer.WriteString("Test2", Test2);
writer.WriteString("Test3", Test3);
writer.WriteInt("Pid", Pid);
writer.MarkIdentityField("Pid");
}
public void FromData(IPdxReader reader)
{
Test1 = reader.readString("Test1");
Test2 = reader.readString("Test2");
Test3 = reader.readString("Test3");
Pid = reader.readInt("Pid");
}
public static IPdxSerializable CreateDeserializable()
{
return new TestPdx();
}
public TestPdx() { }
}
and then register the Pdx type in the Geode, and use a region of type object
or type TestPdx
as follows:
Serializable.RegisterPdxType(TestPdx.CreateDeserializable);
IRegion<string, Object> t = regionFactory.Create<string, Object>("test");
and to write the TestPdx
to the region simply:
TestPdx value = new TestPdx();
value.Test1 = "hello";
value.Test2 = "world";
value.Test3 = "again";
t[key] = value;
and there will be a PdxInstance
in the Geode region so you can run OQL queries on it, etc.
来源:https://stackoverflow.com/questions/45793610/registering-pdx-type-in-type-registry-with-geode-c-sharp-native-client