0

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?

Ben
  • 4,486
  • 6
  • 33
  • 48
  • I think this should answer your question:http://stackoverflow.com/questions/1200346/wcf-service-reference-namespace-differs-from-original – dsfgsho Jul 10 '13 at 12:34
  • Hmm... I don't think so. I don't want to change `SomeResponse`'s namespace but the one of `BoundingBox`. And `[DataMember]` does not accept a namespace. – Ben Jul 10 '13 at 12:45
  • Woops, wrong link :). Try this one: http://blogs.msdn.com/b/jannemattila/archive/2008/10/15/web-services-and-namespaces-or-wcf.aspx – dsfgsho Jul 10 '13 at 13:08
  • Didn't help :( Reuse types in all referenced assemblies was already activated. However, when activating 'Reuse types in specified referenced assemblies', I have no option to choose System.Drawing. – Ben Jul 10 '13 at 13:19
  • Argh... my bad! I simply didn't add System.Drawing as a reference to my project! – Ben Jul 10 '13 at 13:26
  • Yes, but remember this will only work for basicHttpBinding. – dsfgsho Jul 10 '13 at 13:29
  • That's ok, it's all I need. – Ben Jul 10 '13 at 13:30

1 Answers1

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.

dsfgsho
  • 2,731
  • 2
  • 22
  • 39
  • Yes, I know. But I thought the framework was clever enough to map it to a known namespace instead of creating a new one. And it actually does, when 'Reuse types in all referenced assemblies' is checked. My problem was, that System.Drawing was not referenced and thus unknown to my client project. If you edit your anser accordingle, I can accept it :) – Ben Jul 10 '13 at 13:34