我有一个WCF服务(从代码开始),它使用未在服务定义中定义的对象。因此,我必须使用[XmlInclude]
属性使WCF了解如何序列化它。
由于某种原因,这不起作用,WCF仍然抱怨(我发现使用跟踪的异常),我必须使用[XmlInclude]
作为已定义的类型。
我在这里缺少什么?
ServiceHost host = new ServiceHost(typeof(MyService), "http://localhost/myservice");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy12;
host.Description.Behaviors.Add(smb);
host.Open();
[WebService(Namespace = "http://services.mysite.com/MyService")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class MyService : WebService, IMyService {
[WebMethod]
[XmlInclude(typeof(InnerObject))]
public MyReturnObject Test() {
return new MyReturnObject(new InnerObject());
}
}
[ServiceContract]
public interface IMyService {
[OperationContract, XmlSerializerFormat(Style = OperationFormatStyle.Document)]
MyReturnObject Test();
}
MyReturnObject
类包含一个通用对象,可以包含我想要的任何内容。对于此示例,我包含如上定义的InnerObject
类型,类型定义如下所示。
[KnownType(typeof(InnerObject))]
public class MyReturnObject {
public object Content { get; set; }
public MyReturnObject(object content) {
Content = content;
}
}
public class InnerObject {
public int Foo;
public string Bar;
// And some other properties
}
System.InvalidOperationException,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089
不期望InnerObject类型。使用XmlInclude或SoapInclude属性指定静态未知的类型。
答案 0 :(得分:1)
您应该在wcf服务上使用[ServiceKnownType]