Reputation: 162
I am implementing a distributed processing system for medical data. I have multiple clients and servers. Clients have data and they send requests to server for processing.
I have to transfer two values to server and get one list back. The server implementation is defined as:
[ServiceContract]
public interface ServerInterface
{
[OperationContract]
List<Triangle> ServerFunction(Grid g, double isolevel);
}
public class ServerImpl : ServerInterface
{
public List<Triangle> ServerFunction(Grid g, double isolevel)
{/*Implementation*/}
}
the grid class is defined as:
[Serializable]
public class Grid
{
public Point3D[] p = new Point3D[8];
public Int32[] val = new Int32[8];
}
And the Triangle class as
[Serializable]
public class Triangle
{
public Point3D[] p = new Point3D[3];
}
I created client and server side implementation and isolevel value passes fine but grid doesnot get passed properly.
The server creates WCF service using this code:
Uri baseAddress = new Uri("http://localhost:6525/ServerObject");
using (ServiceHost host = new ServiceHost(typeof(ServerImpl), baseAddress))
{
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.Open();
Console.ReadKey();
}
the portion of code calling the server for results is :
var myBinding = new BasicHttpBinding();
var myEndPoint = new EndpointAddress("http://localhost:6525/ServerObject");
var myChannelFactory=new ChannelFactory<ServerInterface>(myBinding,myEndPoint);
ServerInterface si=null;
si = myChannelFactory.CreateChannel();
List<Triangle> triList=si.ServerFunction(Grid g,double isoValue);
It never returns the list of triangles(always returns null).
This code was tested before converting to Distributed and was working properly.
I am using WCF and I tried converting the Grid and triangles to string values and passing them, it works but is very slow. The algorithm itself takes good amount of time so extra processing time is not desirable. Any ideas?
Upvotes: 2
Views: 2786
Reputation: 563
You should use the DataContractSerializer by adding the [DataContract]
attribute to the classes you want to serialize. Then add the [DataMember]
attribute to each of the classe's members. So you'd have something like this:
[DataContract]
public class Grid
{
[DataMember]
public Point3D[] p = new Point3D[8];
[DataMember]
public Int32[] val = new Int32[8];
}
More info can be found on Microsoft's site here.
Upvotes: 1
Reputation: 954
I took your code and implemented the server code below:
var v = new List<Triangle>();
var t1 = new Triangle();
t1.p = new Point3D[3];
t1.p[0] = new Point3D(1,2,3);
t1.p[1] = new Point3D(2,2,3);
t1.p[2] = new Point3D(3,2,3);
v.Add(t1);
return v;
and the client received the triangle coordinates nicely. Have you verified that your server is actually populating the array correctly? below is my simple client code. To create the proxy I just started the server and from the client project I added a service reference to
http://localhost:6525/ServerObject?wsdl
-
var c = new ServerInterfaceClient();
var v=c.ServerFunction(new Grid(), 34.3);
foreach (var triangle in v)
{
Console.WriteLine("X:"+ triangle.p[0]._x);
Console.WriteLine("y:"+ triangle.p[0]._y);
Console.WriteLine("z:"+ triangle.p[0]._z);
}
Sorry I couln't be of more help, but I would add some loggin to both server and client side to try to see where the objects go missing. There shouldn't be anything wrong with your wcf setup.
Upvotes: 1