简体   繁体   English

无法从中获取元数据 <redact> 。 客户端和服务绑定可能不匹配

[英]Cannot obtain metadata from <redact>. Client and service binding may be mismatched

Áfter some changes in a WCF DataContract , I suddenly couldn't access the service anymore. WCF DataContract中进行了一些更改之后,我突然无法再访问该服务。 Using Microsoft's WCF Test Client to obtain the interface I got the following error message 使用Microsoft的WCF Test Client获取界面,我收到以下错误消息

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. 错误:无法从中获取元数据如果这是您有权访问的Windows(R)Communication Foundation服务,请检查是否已在指定地址启用元数据发布。 For help enabling metadata publishing, please refer to the MSDN documentation bla-bla. 有关启用元数据发布的帮助,请参阅MSDN文档bla-bla。

Metadata contains a reference that cannot be resolved: . 元数据包含无法解析的参考:。 Content Type application/soap+xml; 内容类型application / soap + xml; charset=utf-8 was not supported by service 服务不支持charset = utf-8

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; 远程服务器返回错误:(415)由于内容类型为'application / soap + xml,因此无法处理该消息; charset=utf-8' was not the expected type 'text/xml; charset = utf-8'不是预期的类型'text / xml; charset=utf-8'. 字符集= 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? 您可以在上面的代码中找到错误吗?

The error is in the KnownTypeAttribute . 错误在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))] 如果希望将完整的派生类作为已知类型,则应使用带有Type参数而不是字符串参数的构造函数,因此:[KnownType( typeof (DerivedClass))]

[DataContract]
[KnownType(typeof(DerivedClass))]
public class BaseClass
{

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM