我正在实现一个医疗数据的分布式处理系统。我有多个客户端和服务器。客户端拥有数据,并向服务器发送请求进行处理。
我必须将两个值传输到服务器并取回一个列表。服务器实现定义为:
[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*/}
}
网格类定义为:
[Serializable]
public class Grid
{
public Point3D[] p = new Point3D[8];
public Int32[] val = new Int32[8];
}
Triangle 类为
[Serializable]
public class Triangle
{
public Point3D[] p = new Point3D[3];
}
我创建了客户端和服务器端实现,等值线值传递良好,但网格未正确传递。
服务器使用以下代码创建 WCF 服务:
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();
}
调用服务器获取结果的代码部分是:
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);
它从不返回三角形列表(始终返回 null)。
此代码在转换为分布式之前经过测试并且工作正常。
我正在使用 WCF,我尝试将网格和三角形转换为字符串值并传递它们,它可以工作,但速度非常慢。该算法本身需要大量时间,因此不需要额外的处理时间。有什么想法吗?
最佳答案
您应该通过将 [DataContract]
属性添加到要序列化的类来使用 DataContractSerializer。然后将 [DataMember]
属性添加到该类的每个成员。所以你会得到这样的东西:
[DataContract]
public class Grid
{
[DataMember]
public Point3D[] p = new Point3D[8];
[DataMember]
public Int32[] val = new Int32[8];
}
更多信息可以在 Microsoft 网站 here 上找到。 .
关于c# - 在客户端和服务器之间传递对象 C# WCF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20195063/