John Mc
John Mc

Reputation: 2932

Sharing a class library between two WCF services

I have two WCF service applications which pass Data Transfer Objects between each other to perform certain actions. I have moved the DataContracts to a Common project as this seems like the logical thing to do, but I am having problems adding a service reference to one of the WCF service applications. I'm getting 4 similar errors for different parts of the service (contract, service etc):

Custom tool warning: Cannot import wsdl:portType Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter Error: Referenced type 'ServiceLibraryName.Common.Data.LicenceForm, Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' with data contract name 'LicenceForm' in namespace 'http://schemas.datacontract.org/2004/07/ServiceLibraryName.Common.Data' cannot be used since it does not match imported DataContract. Need to exclude this type from referenced types. XPath to Error Source: //wsdl:definitions[@targetNamespace='http://schemas.datacontract.org/2004/07/ServiceLibraryName.Common.Data']/wsdl:portType[@name='ILicenceGeneration'] C:\SourceCode\Release 2 \ServiceLibraryName\Sharepoint\Service References\LicenceGenerationCrm\Reference.svcmap 1 1 Sharepoint

I am certain that the referenced file assembly is the same file on both service and consumer, but I can do nothing to get rid of this message. Reuse assemblies in the Service Reference Configuration is checked.

Using SVCUtil and referencing the assembly also produces the same error.

Any ideas or suggestions would be welcome.

Upvotes: 1

Views: 1590

Answers (2)

John Mc
John Mc

Reputation: 2932

Turns out that this was a problem caused by a piece of code which I added to handle Cdata fields as strings. When I changed this field to a string rather than the CdataWrapper it worked correctly.

Upvotes: 1

Sixto Saez
Sixto Saez

Reputation: 12680

The WCF WSDL generator defaults to using a portion of the C# namespaces of the service classes to generate the XML namespaces referenced in the WSDL. Since you have two services using a common project (that has its own C# namespace), the WSDL generated is likely different for the "common" DataContract classes.

You can control the XML namespace for the DataContract marked classes (and also the OperationContract & ServiceContract attributes) by using the Namespace property. This can be really tedious if you have a lot of DTOs so as an alternative, set the assembly level ContractNamespace attribute to mark all appropriate classes.

Upvotes: 2

Related Questions