简体   繁体   English

WCF服务引发“未预期类型[…]。 即使已经使用XmlInclude,也要使用XmlInclude […]”

[英]WCF service throws “The type […] was not expected. Use the XmlInclude […]” even if XmlInclude is already being used

I have a WCF service (started from code) that uses an object not defined in the service definition. 我有一个WCF服务(从代码开始),该服务使用服务定义中未定义的对象。 Because of that I have to use the [XmlInclude] attribute to make WCF understand how to serialize it. 因此,我必须使用[XmlInclude]属性使WCF理解如何对其进行序列化。 For some reason this doesn't work and WCF still complains (I found the exception using tracing) that I have to use [XmlInclude] for the type already defined. 由于某种原因,这不起作用,WCF仍然抱怨(我使用跟踪发现了异常)我必须对已定义的类型使用[XmlInclude]

What am I missing here? 我在这里想念什么?

Code to start WCF service 启动WCF服务的代码

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();

Service implementation 服务实施

[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());
    }
}

Service definition / interface 服务定义/接口

[ServiceContract]
public interface IMyService {

    [OperationContract, XmlSerializerFormat(Style = OperationFormatStyle.Document)]
    MyReturnObject Test();
}

Return types 退货类型

The MyReturnObject class contains a generic object that can contain whatever I want. MyReturnObject类包含一个通用对象,该对象可以包含我想要的任何对象。 For this example I include a InnerObject type as defined above, and the type definitions look like so. 在此示例中,我包括上面定义的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
}

Complete exception 完全例外

System.InvalidOperationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.InvalidOperationException,mscorlib,版本= 4.0.0.0,文化=中性,PublicKeyToken = b77a5c561934e089

The type InnerObject was not expected. 不应使用InnerObject类型。 Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. 使用XmlInclude或SoapInclude属性可以指定静态未知的类型。

您应该在wcf服务上使用[ServiceKnownType]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “不希望使用RoleProxy类型。 使用XmlInclude或SoapInclude属性可以指定静态未知的类型。” NHibernate - “The type RoleProxy was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.” NHibernate xxx类型不是预期的。 使用XmlInclude或SoapInclude属性来指定静态未知的类型 - The type xxx was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically Xml序列化异常:不需要类型UserQuery + SpecificContentItem。 使用XmlInclude或SoapInclude - Xml serialization Exception : The type UserQuery+SpecificContentItem was not expected. Use the XmlInclude or SoapInclude 类型xxxx不期望使用xmlinclude或soapinclude - type xxxx not expected use xmlinclude or soapinclude XmlSerializer抛出类型的异常,之后我使用XMLInclude? - XmlSerializer throwing exception for type, After I used XMLInclude? 调用Web服务时出现问题-使用XmlInclude或SoapInclude属性 - Issue calling a web service - Use the XmlInclude or SoapInclude attribute 如何使用XmlInclude序列化IEnumerable - How to use XmlInclude to serialize IEnumerable 如何使用XmlInclude动态指定类型? - How to use XmlInclude to specify types dynamically? XmlInclude所有派生类似于WCF中的Knowtypes解决方案 - XmlInclude All Derived Similarly To Knowtypes Solution in WCF 没有[XMLInclude]或Type []构造函数的抽象类的序列化 - Serialization of an abstract class without [XMLInclude] or Type[] Constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM