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.

June 9, 2011

WCF Tracing can also be used on the Client

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

I helped track down an issue at a customer today where we couldn’t determine if one of the client machines was making any WCF service calls to the server.

So we used the WCF Configuration Editor Tool (SvcConfigEditor.exe) and the WCF Service Trace Viewer Tool (SvcTraceViewer.exe) on the server to check for incoming messages.

This helped us get to the bottom of the problem, but it would have been better if we could have directly done a trace on the client machine to monitor the outgoing messages.

I had used Fiddler for something like this many years ago, but before I threw that into the mix I figured I would check if the WCF tools could be used on the client side.

And they can!  🙂

You simply need to run SvcConfigEditor.exe on the client application’s app.config file and set it up for message logging and / or tracing.  Now after you restart your client app you can run SvcTraceViewer.exe and see the WCF service calls being made from it.

Hope this helps!

Blog at WordPress.com.