- PR -

「 System.Collections.IEnumerable をシリアル化できません」のエラー

1
投稿者投稿内容
Access
ぬし
会議室デビュー日: 2002/04/08
投稿数: 829
投稿日時: 2009-02-24 14:36
お世話さまです。

Webサービスを作成したのですが、
「 System.Collections.IEnumerable をシリアル化できません」
のエラーになります。

このエラーを回避する方法を教えてください。

ちなみに、PageMethodだと正常に動作します。

コード:
<%@ WebService Language="C#" Class="WebService" %>

using System;
using System.Collections;
using System.Linq;
using System.Xml.Linq;
using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService  : System.Web.Services.WebService {

  public const string _feedURI = "http://....";

  [WebMethod]
  public IEnumerable GetItems(int Count)
  {
    XDocument feedXML = XDocument.Load(_feedURI);

    var feeds =
      from feed in feedXML.Descendants("item")
      select new
      {
        Date = DateTime.Parse(feed.Element("pubDate").Value).ToShortDateString(),
        Title = feed.Element("title").Value,
        Link = feed.Element("link").Value,
        Description = feed.Element("description").Value,
      };

    return feeds.Take(Count);
  }
   
}

masa
大ベテラン
会議室デビュー日: 2004/10/28
投稿数: 161
投稿日時: 2009-02-24 17:31
エラーメッセージの通りです。

WEBメソッドの引数や戻り値は一旦シリアライズされて送受信されます。
IEnumerable はシリアライズできません。

戻り値をアイテムの配列にしてみてください。
当然、アイテムの型自体がシリアライズ可能である必要もあります。

public アイテムの型[] GetItems(int Count) {

Access
ぬし
会議室デビュー日: 2002/04/08
投稿数: 829
投稿日時: 2009-02-24 22:09
>WEBメソッドの引数や戻り値は一旦シリアライズされて送受信されます。
>IEnumerable はシリアライズできません。
そうなんですか。

でも、*.aspxファイルに記述したPageMethodは正常に動作しています。

public static IEnumerable GetFeedItems(int Count)

の「static」が影響しますか。


コード:
<%@ Page Language="C#" %>

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Xml.Linq" %>
<%@ Import Namespace="System.Web.Services" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  public const string _feedURI = "...";

  [WebMethod]
  public static IEnumerable GetFeedItems(int Count)
  {
    XDocument feedXML = XDocument.Load(_feedURI);

    var feeds =
      from feed in feedXML.Descendants("item")
      select new
      {
        Date = DateTime.Parse(feed.Element("pubDate").Value).ToShortDateString(),
        Title = feed.Element("title").Value,
        Link = feed.Element("link").Value,
        Description = feed.Element("description").Value,
      };

    return feeds.Take(Count);
  }

  
</script>

King
ぬし
会議室デビュー日: 2008/06/20
投稿数: 284
投稿日時: 2009-02-24 23:13
戻り値をシリアライズして送受信する必要のないメソッドだからです。
static は関係ありません。

WEB アプリケーションの関数は WEB サーバで実行して戻り値を WEB サーバ上で受け取ります。
WEB サービスの関数は WEB サーバで実行して戻り値を SOAP で送受信し呼び出し元で受け取ります。

google で「WEBサービス シリアライズ」を検索キーワードにして検索してみて下さい。
べる
ぬし
会議室デビュー日: 2003/09/20
投稿数: 1093
投稿日時: 2009-02-24 23:24
引用:
Webサービスを作成したのですが、
「 System.Collections.IEnumerable をシリアル化できません」
のエラーになります。

これの確認方法はどうやってます?当方では、.asmxを直接呼んでSOAPで受信しようとしても
確かに同様のエラーになりますが、サービスブリッジのクライアントから呼び出した場合は
正常に動作しているようです。

つまり
引用:
でも、*.aspxファイルに記述したPageMethodは正常に動作しています。

これと同じ条件で確認してみてくださいということです。
Access
ぬし
会議室デビュー日: 2002/04/08
投稿数: 829
投稿日時: 2009-02-25 00:13
引用:

WEB アプリケーションの関数は WEB サーバで実行して戻り値を WEB サーバ上で受け取ります。 WEB サービスの関数は WEB サーバで実行して戻り値を SOAP で送受信し呼び出し元で受け取ります。


そうなんですか。

どうもWebサービスは苦手なんですよね。
少し本でも読んで勉強してみます。

Kingさんありがとうございました。

引用:

これの確認方法はどうやってます?当方では、.asmxを直接呼んでSOAPで受信しようと>しても確かに同様のエラーになりますが、サービスブリッジのクライアントから呼び出した場合は正常に動作しているようです。



こちらでも正常に動作しました。

どうもテストしたときは、
[System.Web.Script.Services.ScriptService]
がコメントになっていたようです。

お騒がせしました。

べるさんありがとうございました。

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//[System.Web.Script.Services.ScriptService] ←コメントになってました!
public class WebService : System.Web.Services.WebService
{


[ メッセージ編集済み 編集者: Access 編集日時 2009-02-25 00:21 ]
デューン
大ベテラン
会議室デビュー日: 2004/04/21
投稿数: 174
お住まい・勤務地: Tokyo
投稿日時: 2009-02-25 01:30
ちなみに

コード:

[WebMethod]
public List<FeedItem> GetItems(int Count)
{
XDocument feedXML = XDocument.Load(_feedURI);

var feeds =
from feed in feedXML.Descendants("item")
select new FeedItem
{
Date = DateTime.Parse(feed.Element("pubDate").Value).ToShortDateString(),
Title = feed.Element("title").Value,
Link = feed.Element("link").Value,
Description = feed.Element("description").Value,
};

return feeds.Take(Count).ToList();
}


[Serializable]
public class FeedItem
{
public string Date;
public string Title;
public string Link;
public string Description;
}



と、Listにしても一応回避できるかと。


# すみません、masaさんの回答を見落としていました。

[ メッセージ編集済み 編集者: デューン 編集日時 2009-02-25 01:37 ]
1

スキルアップ/キャリアアップ(JOB@IT)