WCF Casting导致序列化错误 [英] WCF Casting results in serialization error

查看:83
本文介绍了WCF Casting导致序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试构建一个实现以下界面的WCF服务。

Hi everyone,

I'm trying to build a WCF service that implements the following interface.

[ServiceContract]
public interface IWebService
{
	[OperationContract]
	void DoWork(BaseClass argument);
}





我还有以下课程:





I also have the following classes:

public class BaseClass
{
    private string[] elements;
}

public class SuperClass : BaseClass
{
    public string Name
    {
        get{return elements[0];}
        set{elements[0]=value;}
    }
}





但是当我使用以下客户端使用WCF服务时,它会导致错误:



But when I consume the WCF service with the following client, it results in an error:

static void Main()
{
    WebserviceClient _client = new WebserviceClient();
    _client.Open();
    
    SuperClass argument = new SuperClass();
    argument.Name = "Joe";
    
    _client.DoWork((BaseClass)argument);
}







There was an error while trying to serialize parameter http://tempuri.org/:argument. The InnerException message was 'Type 'SuperClass' with data contract name 'SuperClass:http://schemas.datacontract.org/2004/07/SharedTypes' is not expected. Consider using a DataContractResolver or 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.'.  Please see InnerException for more details.





我知道我可以将类型'SuperClass'添加到知识类型列表中。但这不是我的意图。我想围绕这个baseClass构建很多包装类,这些类对我/其他用户来说很容易使用。但是这些包装类只不过是BaseClasses。



上面的示例代码是我真实项目的简化版本。



有谁知道我要做些什么让这个工作?



亲切的问候,



I know I could add the type 'SuperClass' to the list of knowntypes. But that is not my intention. I want to build a lot of wrapper classes around this baseClass that are easy to use for me/other users. But these wrapper classes are nothing more than BaseClasses.

The above example code is a simplified version of my real project.

Does anyone know what I have to do to make this work?

Kind regards,

推荐答案


这是一个非常着名的问题。

SOA与OOP不同,具有新思维。

我们,OOP开发人员需要改变我们的方式当我们在SOA中设计我们的解决方案时思考。



简而言之,问题发生是因为您签署了一份承诺发送对象BaseClass的合同,但它被发现了你发送了一个你应该遵守的合同中未知或意外的另一种类型的对象。



要解决这个问题,请添加属性KnownType 在DataContract的定义之上或使用ServiceKnownType..



这是解决方案,请在线查找更多..我希望这给了你一个线索。
Hi This is a very known issue.
SOA is different than OOP with new mindset.
We, OOP developers need to change our way of thinking when we design our solution in SOA.

In short, the problem happens because you signed a contract that you commit to send an object BaseClass, but it was found out that you sent an object of another type that was unknown or unexpected by the very contract that you should adhere to.

To solve this problem, add the attribute "KnownType" above the definition of the DataContract or use "ServiceKnownType"..

This is the solution, Please find more online.. I hope that gives you a clue.


这篇关于WCF Casting导致序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆