none
callback function in WCF client application to obtain the serial com data from WCF server RRS feed

  • Question

  • I have a WCF Server application which communicates with client application . I want to use serialport in WCF server application and get the data in client application with callback function . Please help me with it . I will attach the code below.                            

    ReportService.cs

    using System;
    using System.IO.Ports;
    namespace ReportService
    {

        [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
        public class ReportService : IReportService
        {

            public void ProcessReport()
            {
                int indata;                    
                indata= port1.ReadByte() ;   

                OperationContext.Current.GetCallbackChannel<IReportServiceCallback>().Progress(indata);


            }

        }

    IReportService.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;

    namespace ReportService
    {
        
        [ServiceContract(CallbackContract = typeof(IReportServiceCallback))]
        public interface IReportService
        {
            [OperationContract]
            void ProcessReport();
        }
        public interface IReportServiceCallback
        {       
            [OperationContract]
            void SerialData (int SerialData);
        }
    }

    ServiceHost

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.ServiceModel;
    using System.IO.Ports;

    namespace formHost
    {
        public partial class Form1 : Form
        {
            private ServiceHost host;
            int indat;
            public Form1()
            {
                InitializeComponent();
                host = new ServiceHost(typeof(ReportService.ReportService));
                host.Open();
                btnStart.Enabled = false;
                btnStop.Enabled = true;
                message.Text = "Service Started";


            }

         

            private void richTextBox1_TextChanged(object sender, EventArgs e)
            {

            }

            private void btnStart_Click(object sender, EventArgs e)
            {
                host = new ServiceHost(typeof(ReportService.ReportService));
                host.Open();
                btnStart.Enabled = false;
                btnStop.Enabled = true;
                message.Text = "Service Started";
            }

            private void btnStop_Click(object sender, EventArgs e)
            {
                host.Close();
                btnStart.Enabled = true;
                btnStop.Enabled = false;
                message.Text = "Service Stopped";
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }
        }
    }

    Tuesday, July 21, 2020 10:37 AM

Answers

All replies

  • Start writing the serial communications first. As library and stand-alone application. Depending on the device behind the serial port, this is necessary to understand how serial communication works as well as the device works.

    Then make this library reliable. Which is in some cases harder then someone may expect. Here you need to thoroughly take a look into concurrent access. Cause a web service allows per se concurrent access, thus using a simple port1.ReadByte() will send the data to arbitrary clients.

    Now you need to look into a separated architecture where you have a separate Windows service handling the device over the serial port. And you need to think about IPC between your web service and that Windows service. In many cases I would expect a job (queue) model, where the consumer (web service) places a job into a queue for the producer (Windows service). Cause in the end behind a serial port lies in almost every case a device which can only handle sequential access.

    Tuesday, July 21, 2020 11:05 AM
  • Wednesday, July 22, 2020 5:48 AM
    Moderator