Click here to Skip to main content
15,392,679 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
C#
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.
            }
        }
    }


Server Side
IserviceM.cs code:
C#
[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 code:
C#
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 code:
C#
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:
C#
MessagesDelegateToServer deliveryService = new MessagesDelegateToServer(reference.MessagingCentre);


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


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!
Posted
Updated 3-Jul-12 8:30am
v2

1 solution

I have solved this problem myself... I was able to accept the Message[] and use it in place of the List<message>.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900