0

我有与 Wcf 服务通信的 Web 应用程序和 android 应用程序。我的一项服务是 Chat.svc

 [ServiceContract(Namespace = "http://webchat.com")]
public interface IChat
{
    [OperationContract]
    [WebInvoke(Method = "POST",
       ResponseFormat = WebMessageFormat.Json,
       RequestFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.Wrapped,
       UriTemplate = "Start")]
    StartChatResult StartChat(StartChatEntity sce);
}

和 Chat.svc.cs

  public StartChatResult StartChat(StartChatEntity sce)
    {
        //doing something else

        List<tblChatRoom> list = ChatManager.GetChatRoomList();

        return new StartChatResult() { IsSuccess = true, ChatRooms = list };

    }

这个方法来自我的 ChatManager 类

public static List<tblChatRoom> GetChatRoomList()
    {
        SessionDBDataContext db = new SessionDBDataContext();
        return db.tblChatRooms.ToList();
    }

当我从 Android 端调用 StartChat 方法时,总是有“错误请求”响应。当我对此行发表评论时

List<tblChatRoom> list = ChatManager.GetChatRoomList();

我有“好的”,没问题。这条线有问题。SessionDBDataContext 类也是

[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="SessionDB")]
public partial class SessionDBDataContext : System.Data.Linq.DataContext
{

    private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();

    public SessionDBDataContext() : 
            base(global::System.Configuration.ConfigurationManager.ConnectionStrings["SessionDBConnectionString"].ConnectionString, mappingSource)
    {
        OnCreated();
    }

    public SessionDBDataContext(string connection) : 
            base(connection, mappingSource)
    {
        OnCreated();
    }

    public SessionDBDataContext(System.Data.IDbConnection connection) : 
            base(connection, mappingSource)
    {
        OnCreated();
    }

    public SessionDBDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
            base(connection, mappingSource)
    {
        OnCreated();
    }

    public SessionDBDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
            base(connection, mappingSource)
    {
        OnCreated();
    }

    public System.Data.Linq.Table<tblChatRoom> tblChatRooms
    {
        get
        {
            return this.GetTable<tblChatRoom>();
        }
    }

    public System.Data.Linq.Table<tblTalker> tblTalkers
    {
        get
        {
            return this.GetTable<tblTalker>();
        }
    }

    public System.Data.Linq.Table<tblSession> tblSessions
    {
        get
        {
            return this.GetTable<tblSession>();
        }
    }

    public System.Data.Linq.Table<tblMessagePool> tblMessagePools
    {
        get
        {
            return this.GetTable<tblMessagePool>();
        }
    }
}

我认为 SessionDB.dbml 有问题,但是当我使用不是服务方法的方法来拥有聊天室列表时,没关系。打电话给服务时我不明白出了什么问题。请帮忙

4

1 回答 1

1

测试这段代码:创建一个类如tblChatRoom,例如:

public class ChatRoom
{
    public string username;
    public string firstname;
    public string lastname;

    public ChatRoom(){}

    public ChatRoom(string username, string firstname, string lastname)
    {
         this.username = username;
         this.lastname = lastname;
         this.firstname = firstname;
    }
}

public StartChatResult StartChat(StartChatEntity sce)
{
    //doing something else

    List<ChatRoom> list =
         (from q in ChatManager.GetChatRoomList()
          select new ChatRoom(q.username, q.firstname, q.lastname)).ToList();

    return new StartChatResult() { IsSuccess = true, ChatRooms = list };

}
于 2012-07-26T21:33:36.647 回答