LP on .NET

June 21, 2011

Errors Generating WSDL for WCF Service

Filed under: .NET,C#,Software Development,WCF — Larry Parker @ 2:42 pm

Our team recently started specifying a namespace for our WCF data contracts.  We were already supplying a namespace for our service contracts, but for some reason we never did it for the data contracts.

The service contracts look something like this:

[ServiceContract(Namespace = ServiceConstants.ServiceContractNamespace)]
public interface ICustomerService
{
    [OperationContract]
    CustomerResponse GetCustomer(CustomerRequest request);
...
}

The namespace is defined as a constant in a static class:

public static class ServiceConstants
{
    public const String ServiceContractNamespace = "http://Company/2011/06/Product";
...
} 

So we just applied the same technique to our data contracts:
[DataContract(Namespace = ServiceConstants.ServiceContractNamespace)]
public class CustomerResponse
{
    [DataMember]
    public String LastName;

    [DataMember]
    public String FirstName;
}

But when we tried to generate a service proxy we got the following error (once we set IncludeExceptionDetailInFaults to true):

System.Xml.Schema.XmlSchemaException: The global element ‘http://Company/2011/06/Product:CustomerResponse’ has already been declared.

After much trial and error, we renamed the class to CustomerResp and everything worked fine.  We originally thought there must have been an older assembly causing the trouble, but a Google search turned up this page…  Problem solved!  🙂

Evidently, WCF uses some reserved suffixes (like Response and Request) and this causes problems when the data contract has the same namespace as the service contract.

Fortunately, the workaround was as simple as changing the data contract’s namespace:

[DataContract(Namespace = ServiceConstants.DataContractNamespace)]
public class CustomerResponse
{
...
}

public static class ServiceConstants
{
    public const String ServiceContractNamespace = "http://Company/2011/06/Product";

    public const String DataContractNamespace = "http://Company/2011/06/Product/Data";
...
}

Aside from fixing the problem, it seems like a good idea to separate the namespaces between the service and data contracts.

But this was a tough one (took all morning).

Hope this helps.

Advertisements

1 Comment »

  1. That helped! Thanks a ton!!!

    Comment by Sandy — May 21, 2012 @ 11:08 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: