Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WCF:配置已知类型_Wcf_Wcf Configuration - Fatal编程技术网

WCF:配置已知类型

WCF:配置已知类型,wcf,wcf-configuration,Wcf,Wcf Configuration,我想知道如何在WCF中配置已知类型。例如,我有一个Person类和一个Employee类。Employee类是Person类的子类。这两个类都用[DataContract]属性标记 我不想硬编码类的已知类型,比如在Person类中放置[ServiceKnownType(typeof(Employee))],以便WCF知道Employee是Person的子类 现在,我在主机的App.config中添加了以下XML配置: <?xml version="1.0" encoding="utf-8"

我想知道如何在WCF中配置已知类型。例如,我有一个Person类和一个Employee类。Employee类是Person类的子类。这两个类都用
[DataContract]
属性标记

我不想硬编码类的已知类型,比如在Person类中放置
[ServiceKnownType(typeof(Employee))]
,以便WCF知道Employee是Person的子类

现在,我在主机的App.config中添加了以下XML配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.serialization>
    <dataContractSerializer>
      <declaredTypes>
        <add type="Person, WCFWithNoLibrary, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null">
          <knownType type="Employee, WCFWithNoLibrary, Version=1.0.0.0,Culture=neutral, PublicKeyToken=null" />
        </add>
      </declaredTypes>
    </dataContractSerializer>
  </system.runtime.serialization>
  <system.serviceModel>
    ....... 
  </system.serviceModel>
</configuration>
顺便说一句,我的服务没有使用类库(WCF类库或非WCF类库)。我只是在主项目中对其进行了简单编码


我猜配置文件中一定有问题(请参阅上面的配置文件)。或者我肯定错过了什么。任何帮助都将不胜感激。

我想我现在已经找到了答案

我在上面发布的配置文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.serialization>
    <dataContractSerializer>
      <declaredTypes>
        <add type="Person, WCFWithNoLibrary, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null">
          <knownType type="Employee, WCFWithNoLibrary, Version=1.0.0.0,Culture=neutral, PublicKeyToken=null" />
        </add>
      </declaredTypes>
    </dataContractSerializer>
  </system.runtime.serialization>
  <system.serviceModel>
    ....... 
  </system.serviceModel>
</configuration>

....... 
我刚才添加的是Person类和Employee类的名称空间。而且不需要更长的版本和文化价值观。。。。正确的配置应为:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.serialization>
    <dataContractSerializer>
      <declaredTypes>
        <add type="WCFWithNoLibrary.Person, WCFWithNoLibrary">
          <knownType type="WCFWithNoLibrary.Employee, WCFWithNoLibrary" />
        </add>
      </declaredTypes>
    </dataContractSerializer>
  </system.runtime.serialization>
  <system.serviceModel>
    ....... 
  </system.serviceModel>
</configuration>

....... 

现在它更短,更有意义。但如果使用第三方库,则需要添加版本、区域性和PublicKeyToken。

我知道这在很久以前就得到了回答,但另一个解决方案(可能对未来的程序员来说更明显):

[KnownType(typeof(SubClass))]
public class BaseClass

斯科特

在另一个案例中,我也收到了这个冗长的错误消息。我确实使用了
KnownTypeAttribute
,并成功地将一个使用
WCF.RIA
的应用程序部署到生产环境中。在第二个版本中,我添加了一个新的子类型,并添加了必要的相应的
KnownTypeAttribute
(编译器不接受没有此属性的子类型-非常好)。然而,编译器接受的内容和在我的机器上运行的内容并没有在生产环境中运行。只有在生产过程中,我才发现上面提到的错误。比较现有子类型和新子类型的所有用法发现,我忘记了
WCF.RIA
要求在方法名称中使用子类型的名称,如GetMySubTypes。因此,如果在添加属性后出现此错误,请查看是否是由于
WCF.RIA
s约定造成的。

我怀疑只有在使用第三方库并将其引用到服务时,配置才会起作用。我会尝试一下,如果我找到一些答案,我会回到这个问题。如果子类型位于不同的projectUpvote中,这并不总是可能的。您必须在Client-side.config上执行任何操作吗?您需要在客户端上复制配置,以便它可以接收新类型。
[KnownType(typeof(SubClass))]
public class BaseClass