繁体   English   中英

WCF服务序列化:解串器不知道任何映射到该名称的类型

[英]WCF Service Serialization: The deserializer has no knowledge of any type that maps to this name

我正在尝试构建WCF服务,但是遇到了阻塞问题。 我一直在Google搜索,但是没有取得任何进展。 希望我在这里能有更多的运气。

假设我有一个这样定义的工作类别:

[DataContract]
public class Job : IJob
{
    public Job(...)
    {
    }

    [DataMember]

    public string Example
    {
        get { return m_example; }
        set { m_example = value; }
    }
}

现在,我要做的就是这样

public void DoSomething()
{
    ExampleServiceProxy.ExampleClient proxy = new ExampleServiceProxy.ExampleClient();
    proxy.DoSomething(job);
}

在我的Reference.cs内部,添加了一些ServiceKnownTypeAttribute,如下所示:

...
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(Job))]
void DoSomething(object job);

我的服务代码如下:

[ServiceContract]
public interface IExample
{
    [OperationContract]
    void DoSomething(IJob);
}

public class Example : IExample
{
    public void DoSomething(IJob job)
    {
        ...
    }
}

我是否需要将其他ServiceKnownTypeAttributes放在某个地方? 我是否需要在服务端重新实现该对象?

attribute on the Service Contract interface. 您必须将属性放在Service Contract接口上。

[ServiceContract] 
public interface IExample 
{ 
    [OperationContract] 
    [ServiceKnownType(typeof(Job))]
    void DoSomething(IJob); 
} 

public class Example : IExample 
{ 
    public void DoSomething(IJob job) 
    { 
        ... 
    } 
} 

您使用IJob接口有什么原因吗? 我从未见过以这种方式实现WCF。

每当我实现WCF时,我总是使用DataContract,即Job。 我这样做是因为DataContract是两方之间的合同,并且没有任何行为。 由于服务确实存在行为,因此使用接口是一个好主意。

另外,还需要在服务端定义DataContract。 因此,如果要将Job移到Service库,请删除IJob,删除Client中现有的引用,然后重新生成它,它将可以正常工作。

服务代码如下:

[ServiceContract]
public interface IExample
{
    [OperationContract]
    void DoSomething(Job);
}

[DataContract]
public class Job 
{
    public Job(...)
    {
    }

    [DataMember]

    public string Example
    {
        get { return m_example; }
        set { m_example = value; }
    }
}

public class Example : IExample
{
    public void DoSomething(Job job)
    {
        ...
    }
}

暂无
暂无

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

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