从Wcf服务连接到dbml

时间:2012-05-25 09:30:56

标签: c# android wcf linq-to-sql

我有与Wcf Services通信的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存在问题,但是当我使用一种非服务方法的方法来获取Chatroom列表时,它没问题。在服务中呼叫时,我无法理解有什么问题。请帮忙

1 个答案:

答案 0 :(得分: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 };

}