简体   繁体   中英

Asynchronous data and WCF?

Sorry for keeping bugging you with WCF. :)

So far I had made a small client-server application, where the client can pass data into functions on the server side and receive results.

But is it somehow possible for a client application to "open a long term connection" to the server and receive asynchronous data from it?

Rather then just connect -> induce a function & pass parameters -> get a replay -> disconnect.

Thanks!

client:

using System.ServiceModel;

namespace ser2client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        CalculatorClient client = new CalculatorClient();

        private void button1_Click(object sender, EventArgs e)
        {
            int v1 = Convert.ToInt16(textBox1.Text);
            int v2 = Convert.ToInt16(textBox2.Text);
            int res = 0;

            if ( tb_action.Text == "+" )
            {
                res = client.Add(v1, v2);
            }
            else if ( tb_action.Text == "-" )
            {
                res = client.Subtract(v1, v2);
            }

            label1.Text = "= " + Convert.ToString(res);
        }


    }
}

server:

using System.ServiceModel;
using System.ServiceModel.Description;

namespace server
{


    [ServiceContract(Namespace = "http://server")]
    public interface ICalculator
    {
        [OperationContract]
        int Add(int n1, int n2);
        [OperationContract]
        int Subtract(int n1, int n2);
    }



    public class CalculatorService : ICalculator
    {

        public int Add(int n1, int n2)
        {
            int result = n1 + n2;
            Console.WriteLine( Convert.ToString(n1)+"+"+Convert.ToString(n2)+"="+result);
            Console.WriteLine("");
            return result;
        }

        public int Subtract(int n1, int n2)
        {
            int result = n1 - n2;
            Console.WriteLine(Convert.ToString(n1) + "-" + Convert.ToString(n2) + "=" + result);
            Console.WriteLine("");
            return result;
        }

    }

WCF is mainly designed for passing messages. What you are defining there is not a defined message. Having said that, WCF does allow a similar behaviour in:

  • Using streams where you can keep writing to the stream
  • Creating a duplex communication where server can callback to the client

From what you seem to need, Streaming is the way to go.

You can setup callback channels on a service and periodically callback your connected clients. Have a look at this article.

I think what your asking is about instance management and if you would like your service to make a callback then WCF can handle that. What it is... you specify a callback method in your contract which the WCF service will invoke at anytime you specify.

For all WCF services I think the default is.. you session will stay open until you close the proxy on the client. Unless you set it up as a PerCall session which the service will close before you close your client proxy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM