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)
{
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);
}
}
}
Server Side
IserviceM.cs code:
[ServiceContract]
public interface IserviceM
{
[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:
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:
public static List<Objects> PostalServiceSelectAll(string userName, int sessionID)
{
int userID = 0;
try
{
userID = CheckUserPresence(userName);
}
catch (Exception e)
{
WriteToLog.WriteString("DatabaseDAL.PostalServiceSelectAll: Error retrieving userID. Error: " + e.Message, " USERNAME: " + userName);
}
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)
{
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
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!