I want to write a RESTful Webservice with WCF which is able to reply in JSON and XML. I have a XML schema from which I generated my classes by using xsd.exe
. Everthing works fine as long as I request XML, but it fails if I want JSON as response.
The System.ServiceModel.Dispatcher.MultiplexingDispatchMessageFormatter
throws a System.Collections.Generic.KeyNotFoundException
. The problem is, what I found out so far, that xsd.exe
does not generate DataContract
and DataMember
Attributes. Is there any solution to this where I do not need to use the SvcUtil.exe
, because therefore I would need to change my schema..
That is the code where it fails, JsonDispatchMessageFormatter
is of the type MultiplexingDispatchMessageFormatter
. (Which is the default type anyway)
var headers = requestProperty.Headers[HttpRequestHeader.Accept] ?? requestProperty.Headers[HttpRequestHeader.ContentType];
if (headers != null && headers.Contains("application/json"))
{
return this.JsonDispatchMessageFormatter.SerializeReply(messageVersion, parameters, result);
}
Generated code:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="...")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="...", IsNullable=false)]
public partial class Incident {
private long incidentIdField;
/// <remarks/>
public long IncidentId {
get {
return this.incidentIdField;
}
set {
this.incidentIdField = value;
}
}
}