0

Áfter some changes in a WCF DataContract, I suddenly couldn't access the service anymore. Using Microsoft's WCF Test Client to obtain the interface I got the following error message

Error: Cannot obtain Metadata from If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation bla-bla.

Metadata contains a reference that cannot be resolved: . Content Type application/soap+xml; charset=utf-8 was not supported by service

The client and service bindings may be mismatched. The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.

A question with the exact same error message was asked and answered here.

In that question the cause was an error in the configuration file. Because my configuration file worked yesterday, I was pretty sure the config file is not the cause of the problem.

[DataContract]
[KnownType(nameof(DerivedClass))]
public class BaseClass
{
    [DataMember]
    public int Id {get; set;}
    [DataMember]
    public string Name {get; set;}
}

[DataContract]
public class DerivedClass : BaseClass
{
     public decimal Value {get; set;}
}

It took me several hours to find the source of the problem. I thought it wise to share the knowledge, in case someone uses the same error message as a search string.

Quiz: The code compiles, the service runs, but the metadata can't be obtained. Can you find the error in the code above?

Community
  • 1
  • 1
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116

1 Answers1

0

The error is in the KnownTypeAttribute. If you want a complete derived class as a known type, you should use the constructor with Type parameter instead of string parameter, so: [KnownType(typeof(DerivedClass))]

[DataContract]
[KnownType(typeof(DerivedClass))]
public class BaseClass
{
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116