tags:

views:

27

answers:

2

Scenario:

  • Web Site project under .NET 3.5
  • Visual Studio 2010
  • WCF Service reference

Problem:
I'm trying to extend a class marked with the DataContract attribute. I though the generated class was declared partial, so that i could easily extend it. I tried declaring a partial class within the same namespace with the same name, but it doesn't seem to recognize what class it's extending. I tried locating the generated code file (Reference.cs) which i thought existed after reading this article inside the reference folder, but it wasn't there. When trying to navigate to the class's definition, i found out it was in a compiled library, and the biggest problem is that it wasn't declared as partial.
Question:
Is this difference related to the fact that i'm using a Web Site and not a Web Project? If so, is there a way that i could make the code generator (which also seems to compile the generated code) to declare class as partial?

A: 

This can indeed be a most frustrating problem to solve - would recommend that you use the WCF Test Client or command line svcutil against the service - you can often get a much more detailed error description.

In my case the issues are invariably related to serialization or namespacing issues of the entity / graph - typically mismatched get / set DataMember, missing KnownType on polymorphic entities, or circular references in the graph.

Partial shouldn't be a problem. Just make sure that any additional properties that you want serialized are marked as DataMember.

If all else fails, would recommend that you run a serialization / deserialization unit test against your entity / entity graph.

nonnb
+1  A: 

Yes there is a way you can declare your DataContract classes as Partial.

For this you'd want to use the DTO pattern. Basically this means defining "shared" Classes in a different assembly, and having both the Service, and the App which consumes the Service, both reference the assembly with your common classes.

So for example your "DTOs" assembly might contain a DTO called "Product". Ok, so you make them Partial, and next you decorate Product, and which ever other Class with the WCF attributes, like DataContract, and DataMember etc.

Now, you reference you DTO assembly with you Service project, and your Web Project.

Now, when you go to your web project and click on "Add Service Reference", click on the "Advanced", and you'll notice you can enable an option to "resuse referenced assemblies". do that and you'll have full control over you DataContracts.

andy
Thanks, trying this now.
scripni
cool, how did it go?
andy
It works but not in my case, one of the reasons that i needed all the business logic separated from the presentation layer was that the business logic runs under .NET 4.0, while the website runs under .NET 3.5. So i can't have a reference that works both for the service layer and for the presentation layer (i think). Thanks for the solution though, it will be useful to know on other projects.
scripni