gpt4 book ai didi

c# - 在 WCF 中使用 DataContract 传递对象

转载 作者:太空宇宙 更新时间:2023-11-03 20:18:48 25 4
gpt4 key购买 nike

[编辑] 我现在已经使用客户端中的构造函数和调用代码以及 OnDeserializing() 和 OnDeserialized() 方法编辑了 D。

我有一个 WCF 服务(通过命名管道)和一个客户端。我需要传递一个对象(最好是该对象的引用)作为我的 OperationContract 的参数。

[DataContract]
public class D
{
[DataMember]
public int Id;

[DataMember]
public string StrId;

//...


public D(int id, string strid)
{
Id = id;
StrId = strid;
//...
}

[OnDeserialized]
void OnDeserialized(StreamingContext strmCtx)
{
} // breakpoint here (1)

[OnDeserializing]
void OnDeserializing(StreamingContext strmCtx)
{
} // breakpoint here (2)

}

这是服务契约(Contract):

[ServiceContract]
public interface ICalc
{
[OperationContract]
int Calculate(string date, int count);

// d is input of this method, and count and array are outputs.
[OperationContract]
int getArray(ref int count, ref int[] array, D d);
}

这是调用 getArray 的我的客户端代码:

proxy.getArray(ref myCount, ref myIntArray, new D(source))

我也试过这个:

D d = new D(source)
proxy.getArray(ref myCount, ref myIntArray, d)

显然这并没有改变任何东西,在这两种情况下,当我在服务代码(getArray 方法的代码)中收到 d 时,它的所有字段都是空的。这是为什么?有什么我想念的吗?

我知道(使用启用跟踪并查看传输层的消息)字段的传输层值正在网络上正确传输。我还向对象添加了 OnDeserialized() 和 OnDeserializing() 方法,以便我可以在那里放置一个断点,在断点 (1) 和 (2) 处,所有字段都为空?!!事实上,对象 setter 根本没有被调用!!

我的想法已经用完了....

最佳答案

WCF 是面向数据的(序列化 xml)而不是面向对象的。那是行不通的!

您的服务操作:

[OperationContract]
int getArray(ref int count, ref int[] array, D d);

将返回一个 int 数据值。如果你想获得 int 值和数组值,我建议你为它创建一个 [DataContract],包含这两个值。通过这种方式,它们将作为数据传递给客户端。

使用 (ref int[]) 调用服务操作不会产生任何影响。

更新一些代码:抱歉,但我无法发现您的错误。这是您可以比较的小例子(有效)。如果您仍然无法修复错误,我建议您发布完整的代码和配置。

using System.Runtime.Serialization;
using System.ServiceModel;

namespace WcfService2
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(InputData value);
}

[DataContract]
public class InputData
{
[DataMember]
public int[] Array { get; set; }
[DataMember]
public D SomeStuff { get; set; }
}

[DataContract]
public class D
{
[DataMember]
public int Id { get; set; }
}
}

using System;

namespace WcfService2
{
public class Service1 : IService1
{
public string GetData(InputData data)
{
if (data.Array == null || data.SomeStuff == null)
throw new NullReferenceException();
return "OK!";
}
}
}

using ConsoleApplication9.ServiceReference;
using System;

namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
var proxy = new Service1Client();
var request = new InputData
{
Array = new int[] {1, 2, 3},
SomeStuff = new D {Id = 42}
};
Console.WriteLine(proxy.GetData(request));
Console.ReadKey();
}
}
}

关于c# - 在 WCF 中使用 DataContract 传递对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14956377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com