传递IList时WCF应用程序崩溃

时间:2011-07-28 16:36:54

标签: wcf nhibernate rest asp.net-3.5

我创建了一个WCF .Net 3.5 Rest服务,它在我通过IList接口时崩溃了。我正在使用NHibernate来填充列表。

这是我的代码:

[OperationContract]
[WebGet(UriTemplate = "EconService/GetAllLanguage", ResponseFormat = WebMessageFormat.Json)]
public IList<Language> GetAllLanguage()
{
   IList<Language> rtnLang = Language.GetAll();
   return rtnLang;
}

这是错误消息:

  

请求错误

     

服务器在处理请求时遇到错误。例外   消息是'无法序列化类型的参数   'System.Collections.Generic.List 1[Econcordia.Language]' (for operation 'GetLanguage', contract 'Service1') because it is not the exact type 'System.Collections.Generic.IList 1 [Econcordia.Language]'   在方法签名中并且不在已知类型集合中。在   为了序列化参数,将类型添加到已知类型   使用ServiceKnownTypeAttribute进行操作的集合。'。看到   服务器日志了解更多详情。异常堆栈跟踪是:

     

at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.ValidateOutputType(Type   type,Type parameterType,IList`1 knownTypes)
  at System.ServiceModel.Dispatcher.SingleBodyParameterDataContractMessageFormatter.GetOutputSerializer(Type type)
  at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.CreateBodyWriter(Object   体)
  在System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.SerializeReply(MessageVersion)   messageVersion,Object []参数,对象结果)
  在System.ServiceModel.Dispatcher.ContentTypeSettingDispatchMessageFormatter.SerializeReply(MessageVersion messageVersion,Object []参数,对象结果)
  在System.ServiceModel.Dispatcher.CompositeDispatchFormatter.SerializeReply(MessageVersion)   messageVersion,Object []参数,对象结果)
  在System.ServiceModel.Dispatcher.DispatchOperationRuntime.SerializeOutputs(MessageRpc&amp;   RPC)
  在System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp;   RPC)
  在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp;   RPC)
  在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp;   RPC)
  在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp;   RPC)
  在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp;   RPC)
  在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp;   RPC)
  在System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您应该返回具体的List<Language>

[OperationContract]
[WebGet(UriTemplate = "EconService/GetAllLanguage", ResponseFormat = WebMessageFormat.Json)]
public List<Language> GetAllLanguage()
{
    IList<Language> rtnLang = Language.GetAll();
    return rtnLang.ToList();
}

希望它有效。