Click here to Skip to main content
11,576,293 members (49,982 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Basic WCF Call + Callback example

, 19 Mar 2012 CPOL 23.2K 3K 10
A basic WCF call + callback example in C# using WSDualHttpBinding.
WCF2.zip
WCF2
ClientApp
WindowsFormsApplication1
ClientApp.suo
WindowsFormsApplication1
bin
Debug
ClientApp.exe
ClientApp.pdb
ClientApp.vshost.exe
obj
Debug
ClientApp.csproj.GenerateResource.Cache
ClientApp.exe
ClientApp.Form1.resources
ClientApp.pdb
ClientApp.Properties.Resources.resources
TempPE
Properties.Resources.Designer.cs.dll
Service References.ServiceReference1.Reference.cs.dll
WindowsFormsApplication1.csproj.GenerateResource.Cache
Properties
Settings.settings
Service References
ServiceReference1
CalculatorService.wsdl
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
WCFServiceNamespace.disco
WCFServiceNamespace.wsdl
Service
Service.suo
Service
bin
Release
InstallUtil.InstallLog
Service.exe
Service.InstallLog
Service.pdb
Service.vshost.exe
Service.vshost.exe.manifest
WCFServiceLog_01.log
obj
Debug
Service.exe
Service.pdb
TempPE
Release
Refactor
Service.exe
Service.pdb
TempPE
Properties
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)

Share

About the Author

javieranton
Software Developer NexTReT S.L.
Spain Spain
Javier attended Rochester Institute of Technology and now works as a Firmware Engineer at NexTReT S.L. and Amper S.A.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 19 Mar 2012
Article Copyright 2012 by javieranton
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid