如何在 WCF 客户端中使用用 DataContractAttribute 修饰的类中的方法?

发布于 2024-09-11 07:53:28 字数 962 浏览 0 评论 0原文

我正在编写一个 .net WCF 服务。我已经编写了一些类,我想将它们返回到 WCF 服务的调用代码;因此,我用 DataContract 属性来装饰它们。

假设我的 WCF 服务名为 FooService。它包含一个名为 FooMethod 的方法,该方法返回一个 FooData 类型的对象(它用 DataContract 属性修饰。假设 FooData< /code> 包含一个数字列表和一个名为 FooAverage 的方法,该方法返回数字的平均值。

在 Visual Studio 中,我创建了一个新应用程序来使用该服务,并添加了一个新的“服务引用”。 " 到我的 WCF 服务,并为其指定命名空间 myWcfService。在我的客户端代码中,我实例化一个代理类并获取到该服务的连接。要从以下位置获取 FooData 对象我的代理,方法调用是 myWcfService.FooMethod() 它返回一个 myWcfService.FooData 类型的对象,该类型是由 FooService 确定的。 。

现在我有了 myWcfService.FooData 类型的对象,如何将此数据作为服务代码中的原始类型获取到 FooData 类型的对象中 这样我就可以调用 FooData.FooAverage() 了?

编辑:我完全意识到数据以 XML 格式通过管道传输,并且用 DataContract 装饰的类中的方法的逻辑不会被返回服务;它不可序列化。我要问的是:如果我可以引用包含服务中使用的 DataContract 类的类,是否有一种直接的方法将数据反序列化到其序列化的类中?

我愿意接受当前的.net框架不可能的答案。

I'm writing a .net WCF service. I've written a few classes that I want to return to the calling code from the WCF service; hence, I decorate them with the DataContract attribute.

Let's suppose that my WCF service is called FooService. It contains a method called FooMethod which returns an object of type FooData (which is decorated with the DataContract attribute. Suppose that FooData contains a list of numbers and a method called FooAverage which returns the average value of the numbers.

In Visual Studio, I create a new application to consume the service. I add a new "Service Reference" to my WCF Service and give it the namespace myWcfService. In my client code, I instantiate a proxy class and get a connection to the service. To get the FooData object from my proxy, the method call is myWcfService.FooMethod(). It returns an object of type myWcfService.FooData, which is a type determined from the FooService metadata.

Now that I have my object of type myWcfService.FooData, how can I get this data into an object of type FooData as the original type from the service code so that I can call FooData.FooAverage()?

Edit: I am entirely aware of the fact that data comes down the pipe in XML format and that the logic of the methods in a class decorated with DataContract does not get returned by the service; it isn't serializable. What I'm asking is: if I can reference the class that contains the DataContract class used in the service, is there a straightforward way to de-serialize the data into the class that it was serialized from?

I am willing to accept the answer that it is not possible with the current .net framework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终止放荡 2024-09-18 07:53:28

如果类型包含在项目中,WCF 将允许重复使用类型。如果您的数据类型位于 Common 项目中的某个位置,那么这些类型将被重新序列化(只要它们具有默认构造函数并被标记为序列化)。

但这只是你问题的一半。如果您希望数据类型代理到服务器方法调用实现,那么您可能需要远程处理(http://msdn.microsoft.com/en-us/library/ms973857.aspx)并使用代理对象。

WCF will allow types to be re-used if they are contained in the project. If your data types are in a Common project somewhere then those types are re-serialized (so long as they have a default constructor and are marked for serialization).

That's only half your question though. If you want your datatype to proxy to the server the method call implementation then you might want Remoting (http://msdn.microsoft.com/en-us/library/ms973857.aspx) and use a Proxy object.

百思不得你姐 2024-09-18 07:53:28

你不能。

您需要将两件事完全分开:

  • 您拥有定义方法的服务契约(用[OperationContract]属性装饰)< /p>

  • 这些方法可能需要您使用定义的类类型[DataContract] 属性

类型。 WCF 的本质是从客户端到服务器的参数和类型是序列化为 XML 表示形式 - 因此您可以以数据契约的形式通过网络发送的所有数据都是原始数据 - 无行为 - 您无法通过 DataContract 类发送并调用一个关于它的方法。

在正常设置中,客户端数据类甚至与服务器端数据类的类型不同 - 它们只是在 XML 序列化格式中看起来相同。它们共享的只是 XML 序列化——序列化的数据元素,仅此而已。 WCF 不是某种远程过程调用方法 - 它在客户端和服务器之间发送纯数据消息(这是一件好事!(tm))。

如果您需要能够调用某个方法,请将该方法添加到您的服务契约中,作为使用 [OperationContract] 属性修饰的方法。

You cannot.

You need to keep two things completely separate:

  • you have service contracts which define the methods (decorated with [OperationContract] attributes)

  • those methods might need class types, which you define using [DataContract] attributes

The nature of WCF is that the parameters and types going from client to server are serialized into an XML representation - so all you can send across the wire in the form of data contracts is raw data - no behavior - you cannot send across a DataContract class and call a method on it.

In a normal setup, your client-side data classes are not even the same type as they are on the server side - they just look the same in their XML serialized format. All they share is their XML serialization - the data elements that get serialized, nothing more. WCF is not some remote-procedure call method - it sends data-only messages between client and server (and that's a good thing! (tm) ).

If you need to be able to call a method, add that method to your service contract as a method decorated with the [OperationContract] attribute.

多像笑话 2024-09-18 07:53:28

您可以使用分部类定义扩展 myWcfService.FooData 并在客户端中定义 FooAverage(),如果原始 FooAverage() 没有任何禁止这样做的依赖项:

partial class FooData
{
  public double FooAverage() { return Math.Average(Numbers); }
}

但请记住 FooData 是一个数据传输对象,因此没有任何私有状态,这意味着您可以将所有数据放入非成员函数中:

double average = Math.Average(foodata.Numbers);

You can extend myWcfService.FooData with a partial class definition and define FooAverage() in the client, if the original FooAverage() doesn't have any dependencies that prohibit this:

partial class FooData
{
  public double FooAverage() { return Math.Average(Numbers); }
}

But remember that FooData is a data transfer object, and as such does not have any private state, which means that you can put all the data into non-member functions anyway:

double average = Math.Average(foodata.Numbers);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文