I am currently developing a C# Windows Form Application that I intend to let it interact with a server. The server will receive posting from a mobile application that I have developed and whenever a posting is received, my Windows Form Application should be notified and give me a notification. In order to do this, I intend to use WCF duplex service for it.
E.g. My mobile application sends an posting over to my server. Once my server reads and receives the new posting, the service should send a message over to my winform app to alert me that a posting is received. And the UI of the winform app should update accordingly to what I want to updated. (e.g. adding new panels)
For my service, this is what I have done so far,
[ServiceContract(SessionMode=SessionMode.Required,
CallbackContract = typeof(IPostingServiceCallBack))]
public interface IPostingService
{
}
public interface IPostingServiceCallBack
{
[OperationContract(IsOneWay = true)]
void NotifyAboutPosting(Posting posting);
}
[DataContract]
public class Posting
{
[DataMember]
public int alertId { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public string Message { get; set; }
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class PostingService : IPostingService
{
public void NotifyAboutPosting(Posting posting)
{
}
}
all this is done in a seperate project file apart from the winform app as I need to host the service on the server itself. I have 2 interface as I understand that there would be 2 channels, 1 for the connection and 1 for the callback.
My question is, do I need to program anything for my IPostingService interface? As I need only the service to reply me when theres new posting and I do not think I need to invoke anything from the winform app.
Secondly, how do I program my callback channel to achieve the result that I want above?
Thanks!
if there is a need for the codes in the main method, it is as following :
Uri baseAddress = new Uri("http://localhost:8888/PostingService");
ServiceHost selfHost = new ServiceHost(typeof(PostingService), baseAddress);
try
{
selfHost.AddServiceEndpoint(typeof(IPostingService), new WSHttpBinding(), "Posting");
Managed to achieve the desired scenario using the publish subscribe framework for wcf.