7

在将数据传回客户端时,我如何告诉 WCF 服务使用什么 KnownTypes?

我知道我可以使用该[ServiceKnownType]属性,这使得服务调用从 WCF 测试服务器运行良好,但是它仍然从客户端失败。我在这里错过了什么吗?

[OperationContract]
[ServiceKnownType(typeof(SubClassA))]
[ServiceKnownType(typeof(SubClassB))]
BaseClassZ GetObject();

来自客户端的错误消息是:

{“元素‘http://schemas.datacontract.org/2004/07/BaseClassZ’包含映射到名称‘http://schemas.datacontract.org/2004/07/SubClassA’的类型的数据。反序列化器不知道映射到此名称的任何类型。考虑使用 DataContractResolver 或将与“SubClassA”对应的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将其添加到已知类型列表中传递给 DataContractSerializer。"}

使用 DataContractSerializer 和 KnownTypes 列表对 WCF 服务器上的对象进行序列化/反序列化工作正常。

更新:如果我将 KnownType 属性添加到基类,我似乎可以让客户端正确读取对象,但如果可能的话,我仍在寻找解决方法,因为基类用于很多项目而我没有我不想在添加新项目时修改基类上的 KnownType 属性。

[DataContract]
[KnownType(typeof(SubClassA))]
[KnownType(typeof(SubClassB))]
public class BaseClassZ 
{
    ...
}
4

3 回答 3

10

为避免阻止您的服务代码,请将已知类型放入服务的 web.config 中:

<system.runtime.serialization>
    <dataContractSerializer>
        <declaredTypes>
            <add type="SomeNs.BaseClassZ, SomeAssembly">
                <knownType type="SomeNs.SubClassA, SomeAssembly" />
                <knownType type="SomeNs.SubClassB, SomeAssembly" />
            </add>
        </declaredTypes>
    </dataContractSerializer>
</system.runtime.serialization>

如果您想通过代码执行此操作,则需要在服务接口而不是操作方法上使用此属性,但我更喜欢声明性方法:

[ServiceContract]
[ServiceKnownType(typeof(SubClassA))]
[ServiceKnownType(typeof(SubClassB))]
public interface IFoo
{
    [OperationContract]
    BaseClassZ GetObject();
}

更新:

我已经建立了一个示例项目来说明如何使用 web.config 来配置已知类型,这是我的首选方法。另一个示例项目演示了第二种方法。


更新 2:

使用 Silverlight 应用程序客户端查看更新后的代码后,我们注意到以下定义:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IService1")]
public interface IService1 {

    [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/IService1/GetMany", ReplyAction="http://tempuri.org/IService1/GetManyResponse")]
    System.IAsyncResult BeginGetMany(System.AsyncCallback callback, object asyncState);

    System.Collections.ObjectModel.ObservableCollection<MyCommonLib.BaseClassZ> EndGetMany(System.IAsyncResult result);

    [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/IService1/GetSingle", ReplyAction="http://tempuri.org/IService1/GetSingleResponse")]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(MyCommonLib.SubClassA))]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(MyCommonLib.SubClassB))]
    System.IAsyncResult BeginGetSingle(System.AsyncCallback callback, object asyncState);

    MyCommonLib.BaseClassZ EndGetSingle(System.IAsyncResult result);
}

注意该BeginGetSingle方法如何包含已知的类型属性,而该BeginGetMany方法不包含。事实上,这些属性应该放在服务定义中,以便类看起来像这样。

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IService1")]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(MyCommonLib.SubClassA))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(MyCommonLib.SubClassB))]
public interface IService1
{
    [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/IService1/GetMany", ReplyAction="http://tempuri.org/IService1/GetManyResponse")]
    System.IAsyncResult BeginGetMany(System.AsyncCallback callback, object asyncState);

    System.Collections.ObjectModel.ObservableCollection<MyCommonLib.BaseClassZ> EndGetMany(System.IAsyncResult result);

    [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/IService1/GetSingle", ReplyAction="http://tempuri.org/IService1/GetSingleResponse")]
    System.IAsyncResult BeginGetSingle(System.AsyncCallback callback, object asyncState);

    MyCommonLib.BaseClassZ EndGetSingle(System.IAsyncResult result);
}

由于这是一个自动生成的类,因此SLsvcUtil.exe中可能存在错误,并且svcutil.exe它表现出相同的行为。将已知的类型属性放在正确的位置可以解决问题。问题是这个类是由工具自动生成的,如果你尝试从 WSDL 重新生成它,它会再次搞砸。

因此,如果您具有以下服务定义,则似乎:

[ServiceContract]
[ServiceKnownType(typeof(SubClassA))]
[ServiceKnownType(typeof(SubClassB))]
public interface IService1
{
    [OperationContract]
    BaseClassZ[] GetMany();

    [OperationContract]
    BaseClassZ GetSingle();
}

并且这里使用的 3 个数据契约在导入服务定义时在客户端和服务器之间共享,返回集合的方法在生成的客户端代理中没有得到正确的已知类型属性。也许这是设计使然。

于 2010-10-02T12:54:39.347 回答
1

我今天花了几个小时,据我所知,这是完全相同的问题。我的解决方案是使用 IDesign 的 ServiceModelEx 库中的 AddGenericResolver 方法。

注意:需要 .NET 4.0,因为它使用DataContractResolver

您可以在IDesign 下载页面上找到它。

在我的情况下,我所要做的就是添加以下代码行:

Client.AddGenericResolver( typeof ( K2Source ) );

我希望这可以帮助其他人节省几个小时!

您可以在 Juval Lowy 所著的《Programming WCF Services: Mastering WCF and the Azure AppFabric Service Bus》一书中找到更多信息

于 2011-09-21T06:30:42.570 回答
0

还有另一种方法可以做到这一点。您无需使用“添加服务引用”来编写代理类。它最初需要更多编码,但为您提供了更加稳定和强大的解决方案。我们发现,从长远来看,这可以节省我们的时间。

见:http ://www.dnrtv.com/default.aspx?showNum=122

注意:这仅适用于您同时控制服务器和客户端的情况。

于 2010-10-02T12:51:01.320 回答