Hi.
I'm having trouble getting a FileInfo object through to the client. First of all, if I add a operations contract on the server that returns a FileInfo object, it all works like a charm - The FileInfo object is received fine on the client-side.
But in my setup, the operations contract returns a custom class that COULD hold some FileInfo object inside.
Here's my setup:
[
DataContract] public class TypeDef{
[
DataMember(IsRequired = true)] private string description;[
DataMember(IsRequired = true)] private string name;[
DataMember(IsRequired = true)] private object data; public TypeDef(string name, string description, object data){
this.Name = name; this.Description = description; this.Data = data;}
public string Description{
get { return description; } set { description = value; }}
public string Name{
get { return name; } set { name = value; }}
public object Data{
get { return data; } set { data = value; }}
}
[
DataContract] public class IOObject{
[
DataMember(IsRequired = true)] private string name;[
DataMember(IsRequired = true)] public List<TypeDef> Types; public IOObject(string name){
this.name = name;Types =
new List<TypeDef>();}
public string Name{
get{
return name;}
}
}
So basically, the IOObject holds a list of TypeDef objects that have a datafield which is of type Object.
The service contract:
[
OperationContract] IOObject[] GetIOObjectForTrigger(int id);
When I generate the client proxy with svcutil and consume the service I get the following error in Microsoft Service Trace Viewer (great tool btw!) (And assuming that a IOObject actually holds a FileInfo object ofcourse):
"There was an error while trying to serialize parameter http://tempuri.org/:GetIOObjectForTriggerResult. The InnerException message was 'Type 'System.IO.FileInfo' with data contract name 'FileInfo:http://schemas.datacontract.org/2004/07/System.IO' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details."
Well this sounded pretty straight forward. I added the [KnownType(typeof(System.IO.FileInfo))] to both the IOObject and TypeDef class and ran svcutil again.
The client is now able to create a proxy and call GetIOObjectForTriggerResult, but the value returned is not of the System.IO.FileInfo Type. Instead its a wierd type that is generated in the clientproxy from svcutil.exe. Here's the autogenerated code:
[System.Xml.Serialization.XmlIncludeAttribute(
typeof(FileInfo))][System.CodeDom.Compiler.GeneratedCodeAttribute(
"svcutil", "3.0.4506.30")][System.
SerializableAttribute()][System.Diagnostics.
DebuggerStepThroughAttribute()][System.ComponentModel.DesignerCategoryAttribute(
"code")][System.Xml.Serialization.XmlTypeAttribute(Namespace=
"http://schemas.datacontract.org/2004/07/System.IO")]public
partial class FileSystemInfo{
private System.Xml.XmlElement[] anyField; private System.Xml.XmlQualifiedName factoryTypeField; /// <remarks/>[System.Xml.Serialization.XmlAnyElementAttribute(Order=0)]
public System.Xml.XmlElement[] Any{
get{
return this.anyField;}
set{
this.anyField = value;}
}
/// <remarks/>[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified, Namespace=
"http://schemas.microsoft.com/2003/10/Serialization/")] public System.Xml.XmlQualifiedName FactoryType{
get{
return this.factoryTypeField;}
set{
this.factoryTypeField = value;}
}
}
///
<remarks/>[System.CodeDom.Compiler.GeneratedCodeAttribute(
"svcutil", "3.0.4506.30")][System.
SerializableAttribute()][System.Diagnostics.
DebuggerStepThroughAttribute()][System.ComponentModel.DesignerCategoryAttribute(
"code")][System.Xml.Serialization.XmlTypeAttribute(Namespace=
"http://schemas.datacontract.org/2004/07/System.IO")]public
partial class FileInfo : FileSystemInfo{
}
This Type doesnt hold any of the information I need from the server.Since I was able to send the FileInfo object alone (without including it in some custom class) it should be able to get my setup to work, right
How do I get the client to use a System.IO.FileInfo object and not the one from the svcutil generated proxy
I suspect that I'm using the KnownType attribute wrong, but so far i've been unable to figure it out myself.
Hope some of you can help me out.