CommunicationException:尝试序列化参数时出错

发布于 2024-11-26 17:43:53 字数 2416 浏览 4 评论 0原文

我有一个 Web 服务,其方法采用抽象基本类型 Specification 的参数作为参数。我意识到 WCF 不知道如何在不指定已知类型的情况下对其进行反/序列化。但是当我尝试指定它们时,我仍然得到这个异常:

尝试序列化参数 http://myproject.org:specation 时出错。 InnerException 消息为“键入‘MyProject.DomainModel.WebSnapshot.WebSnapshotSpecification’,数据协定名称为‘WebSnapshotSpecification:http://schemas.datacontract.org/2004/07/MyProject.DomainModel.WebSnapshot’”不是预期的。将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。

如果有人能看到我错过了什么,我将非常感激:) 这些类如下所示:

WebSnapshotRepositoryService

[ServiceContract(Namespace = "http://myproject.org")]
[ServiceKnownType(typeof(Specification<WebSnapshot>))]
[ServiceKnownType(typeof(WebSnapshotSpecification))]
public class WebSnapshotRepositoryService
{
    [OperationContract]
    public IEnumerable<WebSnapshot> Get(Specification<WebSnapshot> specification)
    {
        // Code here.
    }
}

WebSnapshotSpecification

public class WebSnapshotSpecification : Specification<WebSnapshot>
{
    public override Expression<Func<WebSnapshot, bool>> IsSatisfiedByExpression()
    {
        return (t) => true;
    }
}

这是 WebSnapshot 的规范:

[DataContract(IsReference = true, Name = "WebSnapshot", Namespace = "MyProject.DomainModel.WebSnapshot")]
public class WebSnapshot
{
    [DataMember(Order = 1)]
    public virtual string Name { get; set; }
    [DataMember(Order = 2)]
    public virtual string PictureFilePath { get; set; }
    [DataMember(Order = 3)]
    public virtual int PictureHeight { get; set; }
    [DataMember(Order = 4)]
    public virtual int PictureWidth { get; set; }
}

最后,这里是 Specification; :

public abstract class Specification<TDomainModel>
{
    public virtual bool IsSatisfiedBy(TDomainModel domainObject)
    {
        return IsSatisfiedByExpression().Compile().Invoke(domainObject);
    }

    public virtual TDomainModel AssembleObject()
    {
        return Activator.CreateInstance<TDomainModel>();
    }

    public abstract Expression<Func<TDomainModel, bool>> IsSatisfiedByExpression();
}

I have a webservice with a method that takes a parameter of abstract base type Specification<TDomainModel> as parameter. I realize that WCF don't know how to de-/serialize this without specifying known types. But when I try to specify them, I still get this exception:

There was an error while trying to serialize parameter http://myproject.org:specification. The InnerException message was 'Type 'MyProject.DomainModel.WebSnapshot.WebSnapshotSpecification' with data contract name 'WebSnapshotSpecification:http://schemas.datacontract.org/2004/07/MyProject.DomainModel.WebSnapshot' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

If anyone can see where I have missed something, it would be very much appreciated :)
The classes looks as follows:

WebSnapshotRepositoryService:

[ServiceContract(Namespace = "http://myproject.org")]
[ServiceKnownType(typeof(Specification<WebSnapshot>))]
[ServiceKnownType(typeof(WebSnapshotSpecification))]
public class WebSnapshotRepositoryService
{
    [OperationContract]
    public IEnumerable<WebSnapshot> Get(Specification<WebSnapshot> specification)
    {
        // Code here.
    }
}

and WebSnapshotSpecification:

public class WebSnapshotSpecification : Specification<WebSnapshot>
{
    public override Expression<Func<WebSnapshot, bool>> IsSatisfiedByExpression()
    {
        return (t) => true;
    }
}

which is a specification of WebSnapshot:

[DataContract(IsReference = true, Name = "WebSnapshot", Namespace = "MyProject.DomainModel.WebSnapshot")]
public class WebSnapshot
{
    [DataMember(Order = 1)]
    public virtual string Name { get; set; }
    [DataMember(Order = 2)]
    public virtual string PictureFilePath { get; set; }
    [DataMember(Order = 3)]
    public virtual int PictureHeight { get; set; }
    [DataMember(Order = 4)]
    public virtual int PictureWidth { get; set; }
}

finally, here is Specification<TDomainModel>:

public abstract class Specification<TDomainModel>
{
    public virtual bool IsSatisfiedBy(TDomainModel domainObject)
    {
        return IsSatisfiedByExpression().Compile().Invoke(domainObject);
    }

    public virtual TDomainModel AssembleObject()
    {
        return Activator.CreateInstance<TDomainModel>();
    }

    public abstract Expression<Func<TDomainModel, bool>> IsSatisfiedByExpression();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说谎友 2024-12-03 17:43:53

您似乎正在尝试将 WebSnapshotSpecification 类实例发送到服务,并期望 WCF 序列化/反序列化它包含的 IsSatisfiedByExpression 方法。 WCF 不支持这种情况。如果 WebSnapshotSpecification 类具有某些属性,那么这些属性将被序列化/反序列化,但方法永远不是 WCF 隐式或显式 DataContract 定义的一部分。

It seems like you are trying to send a WebSnapshotSpecification class instance to the service and are expecting WCF to serialize/deserialize the IsSatisfiedByExpression method it contains. That scenario is not supported in WCF. If the WebSnapshotSpecification class had some properties then those will be serialized/deserialized but methods are never part of a WCF implicit or explicit DataContract definition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文