I am trying to connect to hazelcast cache server by using the sample code from this link
But I am getting the below error - Exception has been thrown by the target of an invocation.
I have verified that the hazelcast cache server is running (in local system). It is running on 5701 port and the same is mentioned C# code.
Below is the code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Hazelcast.Config;
using Hazelcast.Client;
using Hazelcast.Core;
using Hazelcast.IO;
using Hazelcast.IO.Serialization;
namespace Hazelcast.Client.Example
{
public class ExampleWithCustomSerializer
{
static void Main(string[] args)
{
var clientConfig = new ClientConfig();
clientConfig.GetNetworkConfig().AddAddress("127.0.0.1:5701");
var sc = new SerializerConfig()
.SetImplementation(new CustomSerializer())
.SetTypeClass(typeof(Person));
//Custom Serialization setup up for Person Class
clientConfig.GetSerializationConfig().AddSerializerConfig(sc);
IHazelcastInstance client = HazelcastClient.NewHazelcastClient(clientConfig);
//All cluster operations that you can do with ordinary HazelcastInstance
IMap<string, Person> mapCustomers = client.GetMap<string, Person>("persons");
mapCustomers.Put("1", new Person("Joe", "Smith"));
mapCustomers.Put("2", new Person("Ali", "Selam"));
mapCustomers.Put("3", new Person("Avi", "Noyan"));
ICollection<Person> persons = mapCustomers.Values();
foreach (var person in persons)
{
Console.WriteLine(person.ToString());
}
HazelcastClient.ShutdownAll();
Console.ReadKey();
}
}
class CustomSerializer : IStreamSerializer<Person>
{
public int GetTypeId()
{
return 10;
}
public void Destroy()
{
//NOOP
}
public void Write(IObjectDataOutput output, Person t)
{
byte[] array;
var bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, t);
array = ms.ToArray();
}
output.WriteInt(array.Length);
output.Write(array);
}
public Person Read(IObjectDataInput input)
{
var bf = new BinaryFormatter();
var len = input.ReadInt();
var buffer = new byte[len];
input.ReadFully(buffer);
Person result = null;
using (var ms = new MemoryStream(buffer))
{
result = (Person)bf.Deserialize(ms);
}
return result;
}
}
[Serializable]
public class Person
{
private string name;
private string surname;
public Person(string name, string surname)
{
this.name = name;
this.surname = surname;
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Surname
{
get { return surname; }
set { surname = value; }
}
public override string ToString()
{
return "Person{ name:" + name + ", surname:"+surname + " }";
}
}
}
Can anyone help me to resolve this issue, thanks in advance.
I tried this example with 3.8.1 and it works:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.