Sharing common class in wcf project

1k Views Asked by At
class MyCommonClass
{
//properties
}

This class should be accessible in

  1. service project
  2. wcf-client project
  3. the other for which they are references. In this common project I can't generate servicereferences.

I think, I could don't generate MyCommonClass in ServiceReferences but how to mark class to be nonserializable? In properties there's IgnoreDataMemberAttribute. I tried also reuse MyCommonClass type located in common project, but it is still generated

UPDATE

In different words: if some type is used in ServiceOperation it's automatically generated into wsdl document. How to disable it? (I don't want it on wcf-client side)

1

There are 1 best solutions below

3
On

It's not clear whether the common class needs to part of a WCF service contract so it can be made available to any client through the WSDL document or if you are trying make the common class accessible to both the WCF service and the WCF client as a standard .NET class.

The only reason to share a standard .NET class between the service and the client is to provide access to shared logic. Otherwise, just mark that class with either DataContract or Serializable to let the service expose the class to the client thru WSDL. Just remember that one of the four tenets of SOA is to share types (WSDL) not classes (.NET assemblies).

Based on the updated question, you can achieve better control over what WCF serializes into soap XML by forcing WCF to use the XmlSerializer instead of the default DataContractSerializer. Also, you may want to decouple the domain model objects from what objects are exposed by the WCF service as discussed in this SO question and answers. This excellent blog post explains the differences between the two serializers and how to force WCF to use the XmlSerializer.