Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#

Basic WCF Call + Callback example

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
19 Mar 2012CPOL2 min read 38K   3.7K   10  
A basic WCF call + callback example in C# using WSDualHttpBinding.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

namespace ClientApp
{
    public partial class Form1 : Form
    {
        bool isSubscribed = false;
        Callback callback;
        ServiceReference1.CalculatorClient client;

        public Form1()
        {
            InitializeComponent();
            callback = new Callback(this);
            client = new ClientApp.ServiceReference1.CalculatorClient(new InstanceContext(callback));

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.richTextBox1.Text += "\nCalling WCF service add method... Add(" + this.textBox1.Text + "," + this.textBox2.Text + ") = " + client.Add(Convert.ToDouble(this.textBox1.Text), Convert.ToDouble(this.textBox2.Text)).ToString();
            }
            catch (Exception ex)
            {
                this.richTextBox1.Text = ex.ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (client.Subscribe())
                {
                    this.richTextBox1.Text += "\nClient subscribed";
                    isSubscribed = true;
                }
                else
                    this.richTextBox1.Text += "\nCould not subscribe client";
            }
            catch (Exception ex)
            {
                this.richTextBox1.Text += "\n"+ex.ToString();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if(isSubscribed)
                MessageBox.Show("Client unsubscribed..."+ client.UnSubscribe());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not unsubscribe client");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                this.richTextBox1.Text += "\nCalling WCF service subtract method... Subtract("+this.textBox1.Text+","+ this.textBox2.Text+") = " + client.Subtract(Convert.ToDouble(this.textBox1.Text), Convert.ToDouble(this.textBox2.Text)).ToString();
            }
            catch (Exception ex)
            {
                this.richTextBox1.Text = ex.ToString();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                this.richTextBox1.Text += "\nCalling WCF service multiply method... Multiply(" + this.textBox1.Text + "," + this.textBox2.Text + ") = " + client.Multiply(Convert.ToDouble(this.textBox1.Text), Convert.ToDouble(this.textBox2.Text)).ToString();
            }
            catch (Exception ex)
            {
                this.richTextBox1.Text = ex.ToString();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                this.richTextBox1.Text += "\nCalling WCF service divide method... Divide(" + this.textBox1.Text + "," + this.textBox2.Text + ") = " + client.Divide(Convert.ToDouble(this.textBox1.Text), Convert.ToDouble(this.textBox2.Text)).ToString();
            }
            catch (Exception ex)
            {
                this.richTextBox1.Text = ex.ToString();
            }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer NexTReT S.L.
Spain Spain
Javier graduated from Rochester Institute of Technology in 2010 and has been working as a software developer since then

Comments and Discussions