How do I pass a System.Drawing.Rectangle over WCF?

£可爱£侵袭症+ 提交于 2019-12-12 04:58:23

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17570678/how-do-i-pass-a-system-drawing-rectangle-over-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!