问题与传递的DataContract对象的列表 [英] Problem with a list of DataContract objects being passed

查看:84
本文介绍了问题与传递的DataContract对象的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与数据库进行通信的服务器上具有WCF服务,并且在与WCF服务进行通信的客户端计算机上具有Windows服务.这是我第一次开发WCF服务,因此有了一些学习经验.我所坚持的一个领域是DataContract对象.

WCF简介:
客户端发送2个参数以检查数据库中的消息.查询数据库,并为每个条目创建一个Message对象.这些对象被放入列表.列表<>然后将其发送回客户端计算机.

客户代码:

I have a WCF service on a server that communicates with the database, and a Windows service that is put onto client machines that communicates with the WCF service. This is the first time I have developed a WCF service so it has been a bit of a learning experience. One area that I am stuck on is the DataContract objects.

Intro to the WCF:
The client sends in 2 parameters to check the database for messages. The database is queried, and for each entry, a Message object is created. These objects are put into a List<>. The List<> is then to be sent back to the client machine.

Client code:

class PostOffice
    {
        private static serviceMReference.IserviceM reference = new IserviceMClient();
        private delegate List<serviceMReference.Message> MessagesDelegateToServer(string userName, int sessionID);
        
        public static void MessagesWaiting()
        {
            if (Properties.Settings.Default.userName != "noUser" && Properties.Settings.Default.userName != null)
            {
                // Async call to the messaging centre

                List<serviceMReference.Message> newMessages = new List<serviceMReference.Message>();

                MessagesDelegateToServer deliveryService = new MessagesDelegateToServer(reference.MessagingCentre);
                IAsyncResult messageIAR = deliveryService.BeginInvoke(Properties.Settings.Default.userName,
                    int.Parse(Properties.Settings.Default.sessionID), null, null);
                newMessages = deliveryService.EndInvoke(messageIAR);

                // Work with the returned list.
            }
        }
    }



服务器端
IserviceM.cs代码:



Server Side
IserviceM.cs code:

[ServiceContract]
public interface IserviceM
{
    // Other code erased for ease of reading

    [OperationContract]
    List<Objects> MessagingCentre(string userName, int sessionID);

}

[DataContract(Name = "Message")]
    public class Objects
    {
        [DataMember(Name = "MUID")]
        internal int MUID;

        [DataMember(Name = "username")]
        internal string username;

        [DataMember(Name = "sessionid")]
        internal int sessionid;

        [DataMember(Name = "category")]
        internal string category;

        [DataMember(Name = "message")]
        internal string message;

        public Objects(int newMUID, string newUsername, int newSessionid, string newCategory, string newMessage)
        {
            MUID = newMUID;
            username = newUsername;
            sessionid = newSessionid;
            category = newCategory;
            message = newMessage;
        }
    }



serviceM.svc.cs代码:



serviceM.svc.cs code:

public class serviceM : IserviceM
    {
        private delegate List<Objects> LetsGoPostal(string userName, int sessionID);
        
        public List<Objects> MessagingCentre(string userName, int sessionID)
        {
            LetsGoPostal letsGoPostal = new LetsGoPostal(DatabaseDAL.PostalServiceSelectAll);
            IAsyncResult onTheirBLEEP = letsGoPostal.BeginInvoke(userName, sessionID, null, null);
            List<Objects> ReturnOfTheMessages = letsGoPostal.EndInvoke(onTheirBLEEP);

            return ReturnOfTheMessages;
        }
	}



DatabaseDAL代码:



DatabaseDAL code:

public static List<Objects> PostalServiceSelectAll(string userName, int sessionID)
        {
            // Get the user ID
            int userID = 0;
            try
            {
                userID = CheckUserPresence(userName);
            }
            catch (Exception e)
            {
                WriteToLog.WriteString("DatabaseDAL.PostalServiceSelectAll: Error retrieving userID. Error: " + e.Message, " USERNAME: " + userName);
            }
            
            // Get state ID from database
            SqlCommand cmd = new SqlCommand();
            SqlDataReader reader = null;
            DataTable queryTable = new DataTable();
            cmd.CommandText = "dbo.GetAllMessagesForUser";
            cmd.Parameters.AddWithValue("@UserID", userID);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = DatabaseConnection;
            List<Objects> ListOfMessages = new List<Objects>();
            
            try
            {
                if (cmd.Connection.State != ConnectionState.Open)
                {
                    cmd.Connection = new SqlConnection(DatabaseConnectionString);
                    cmd.Connection.Open();
                }
                reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Objects currentMessage = new Objects(
                            int.Parse(reader["MUID"].ToString()),
                            userName, sessionID, reader["Category"].ToString(),
                            reader["Message"].ToString()
                            );
                        ListOfMessages.Add(currentMessage);
                    }
                }
            }
            catch (Exception e)
            {
                // Write an error to the event log
                WriteToLog.WriteString("DatabaseDAL.PostalService: Error with tables. " + e.Message, EventLogEntryType.Error);
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                {
                    reader.Close();
                }
                reader.Dispose();
                if (cmd.Connection.State == System.Data.ConnectionState.Open)
                {
                    cmd.Connection.Close();
                }
                cmd.Connection.Dispose();
                cmd.Dispose();
            }
            return ListOfMessages;
        }



我得到的错误在此行的客户端代码中:



The error that I am getting is in the client code on this line:

MessagesDelegateToServer deliveryService = new MessagesDelegateToServer(reference.MessagingCentre);



错误:



Error:

'MessagingWindowsService.serviceMReference.Message[] MessagingWindowsService.serviceMReference.IserviceM.MessagingCentre(string, int)' has the wrong return type



我已经将MSDN页面用于DataContracts,并在这里以及其他站点上阅读了许多有关WCF和服务之间通信的文章.如果有人对我如何解决此错误或使沟通更加顺利有任何提示或建议,我将不知所措.请记住,这是我第一次编写一个以这种方式进行交流的项目,所以我做的事情可能与我应该做的完全不同.

预先感谢您的帮助!



I have used the MSDN pages for the DataContracts and have read many articles on here as well as other sites regarding WCF and communications between services. If anyone has any tips or suggestions as to how I can fix this error or make the communication work better, I''m all ears. Please keep in mind that this is my first time writing a project that communicates this way, so I may be doing it completely different than I should be.

Thanks in advance for any help!

推荐答案

我自己已经解决了这个问题...我能够接受Message []并使用它代替List< ; message>.
I have solved this problem myself... I was able to accept the Message[] and use it in place of the List<message>.


这篇关于问题与传递的DataContract对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆