开发者

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

I'm trying to build out a WCF service but I've run into a blocking issue. I've been Googling around but I haven't been able to make any progress. Hopefully I'll have more luck here.

Let's say I have a job class defined as such:

[DataContract]
public class Job : IJob
{
    public Jo开发者_Go百科b(...)
    {
    }

    [DataMember]

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

Now, what I do is something like this

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

Inside of my Reference.cs I've added some ServiceKnownTypeAttribute as follows:

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

My service code is as follows:

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

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

Do I need to put further ServiceKnownTypeAttributes somewhere? Do I need to reimplement the object on the service side?


You have to put the ServiceKnownType attribute on the Service Contract interface.

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

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


Is there any reason you are using the interface IJob? I have never seen WCF implemented in that way.

When ever I implement WCF I always use the DataContract, ie Job. I do that because the DataContract is the contract between the two parties, and has no behavior. Where as the Service does have behavior so using an interface is a good idea.

Also the DataContract needs to be defined on the Service side. So if you were to move Job to the Service library, remove IJob, delete the existing reference in the Client and regenerate it, it will work fine.

Here is the service code:

[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)
    {
        ...
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜