如何通过wcf传递system.drawing.rectangle?[英] How do I pass a System.Drawing.Rectangle over WCF?

本文是小编为大家收集整理的关于如何通过wcf传递system.drawing.rectangle?的处理方法,想解了如何通过wcf传递system.drawing.rectangle?的问题怎么解决?如何通过wcf传递system.drawing.rectangle?问题的解决办法?如何通过wcf传递system.drawing.rectangle?问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我有使用类的WCF服务

namespace MyService
{
    [DataContract]
    public class SomeResponse
    {
        [DataMember]
        public System.Drawing.Rectangle BoundingBox { get; set; }
    }
}

在客户端,当我尝试访问BoundingBox矩形时,矩形将其名称空间从System.Drawing.Rectangle更改为MyServiceReference.Rectangle. 我希望WCF框架足够聪明,可以意识到system.drawing.Rectangle也存在于客户端,并因此使用正确的名称空间.可以告诉服务这样做吗?

推荐答案

这里的主要问题是WCF使用SOAP发送/接收数据,该数据不是直接连接到.NET,而是一个开放的Web标准.例如. Java客户端也可能会消耗数据,因此对System.Drawing.Rectangle名称空间一无所知.但是,它可以使用Web服务定义语言中提供的矩形,并将其映射到Java矩形结构.

在您的情况下,您可能要将System.Drawing.Rectangle施放并将其转换为MyServiceReference.Rectangle. However, WCF provides built-in support for this by checking the "在所有引用的组件中的重复使用类型"属性中的选项:

当将服务引用添加到项目中时, 该服务是在本地项目中生成的.在许多情况下,这个 当服务使用常见的.NET框架时,创建重复类型 类型或在共享库中定义类型时.

为避免此问题,引用的组件中的类型由 默认.如果您想禁用一个或多个类型共享 组件,您可以在"配置服务参考"对话框中这样做 盒子.

this 本文地址:https://www.itbaoku.cn/post/1671645.html

问题描述

I have a WCF service using a class

namespace MyService
{
    [DataContract]
    public class SomeResponse
    {
        [DataMember]
        public System.Drawing.Rectangle BoundingBox { get; set; }
    }
}

On client side, when I try to access the BoundingBox rectangle, the Rectangle changed its namespace from System.Drawing.Rectangle to MyServiceReference.Rectangle. I would expect the WCF framework to be clever enough to realize that System.Drawing.Rectangle also exists on client side and accordingly use the right namespace. Is it possible to tell the service to do so?

推荐答案

The main problem here is that WCF uses SOAP to send/received data, which is not directly connected to .net but an open web standard. E.g. a java client might also consume the data and hence not know anything about the System.Drawing.Rectangle namespace. It can however use the Rectangle provided in the Web Service Definition Language and map that to a java Rectangle structure.

In your case, you probably want to cast and convert the System.Drawing.Rectangle to MyServiceReference.Rectangle. However, WCF provides built-in support for this by checking the "Reuse types in all referenced assemblies" option in the properties:

When a service reference is added to a project, any types defined in the service are generated in the local project. In many cases, this creates duplicate types when a service uses common .NET Framework types or when types are defined in a shared library.

To avoid this problem, types in referenced assemblies are shared by default. If you want to disable type sharing for one or more assemblies, you can do so in the Configure Service References dialog box.

This link provides an example on how to enable/use this functionality.

Finally, make sure that all the right assemblies are referenced in your client application. If it is not referenced, it obviously cannot be re-used by WCF.

查看更多