当前位置: 代码迷 >> Web Service >> WCF 引用自定义类库中的类解决思路
  详细解决方案

WCF 引用自定义类库中的类解决思路

热度:157   发布时间:2016-05-02 02:21:34.0
WCF 引用自定义类库中的类
    我使用 WCF和silverlight 进行通信,自己写的方法,如果返回的是基础类型(int,string)等,是没问题的;在IService里边定义的类,返回也是没问题的。
    由于之前写过一个类库,里边有自定义的抽象类和接口,想在WCF里边返回这些接口。于是在WCF中添加了类库的引用,并写了返回自定义类库中的接口的方法,于是问题来了。
    调试的时候报错:
    failed to invoke the service. possible causes:the service is offline or inaccessible; the client-side configuration does not match the proxy;the existing proxy is invalid . refer to the stack trace  for more detail.you can try to recover by starting a new proxy,restoring to default configuration, or refreshing the service.
    刚接触wcf,我wcf直接创建的服务,没经过配置,但是本机可以调用。考虑可能是自定义的类 前边没有[DataContract]的原因,但是问题来了,难道要我在类库中对每个类添加[DataContract]?或者在IService中再定义一遍类?
    请大家帮下忙,如果重新再定义一遍类库中的类,感觉会出问题,毕竟名字重复了。一般这种情况应该如何处理?多谢大家了!
------解决方案--------------------
如果使用抽象类或者interface作为WCF 公开方法的参数,必须用 [KnowTypeAttribute] 申明抽象类,或者接口的实现类。
  相关解决方案