Hosting a WCF Service Library using Windows Forms

by Jagadish Pulakhandam on 5/10/2011 1:50:18 PM
Rated 0 from 0 votes
Brief: Demonstrates on developing a WCF Service Library from the scratch and hosting the same using Windows Forms.
Posted to: Windows Communication Foundation - Hosting
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Demonstrates the following (see attached video):

  • Create a very basic WCF Service Library from the scratch (using Visual Studio 2010)
  • Configure WCF Service using BasicHttpBinding using "WCF Service Configuration Editor"
  • Develop a Windows Forms application which hosts WCF Service developed in previous step
  • Test the service using "WCF Test Client"
Sample screen shot:



Source Code:

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05.using System.ServiceModel;
06. 
07.namespace SampleCalcSvc
08.{
09.    [ServiceContract]
10.    public interface ICalcService
11.    {
12.        [OperationContract]
13.        int GetSum(int a, int b);
14.    }
15.}
01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05. 
06.namespace SampleCalcSvc
07.{
08.    public class CalcService : ICalcService
09.    {
10.        public int GetSum(int a, int b)
11.        {
12.            return (a + b);
13.        }
14.    }
15.}
01.using System;
02.using System.Collections.Generic;
03.using System.ComponentModel;
04.using System.Data;
05.using System.Drawing;
06.using System.Linq;
07.using System.Text;
08.using System.Windows.Forms;
09. 
10.using System.ServiceModel;
11. 
12.namespace SampleSvcWinHost
13.{
14.    public partial class Form1 : Form
15.    {
16.        public Form1()
17.        {
18.            InitializeComponent();
19.        }
20. 
21.        private ServiceHost oSvc;
22.        private void btnStart_Click(object sender, EventArgs e)
23.        {
24.            oSvc = new ServiceHost(typeof(SampleCalcSvc.CalcService) );
25.            oSvc.Open();
26.            this.lblMsg.Text = "Started..";
27.        }
28. 
29.        private void btnStop_Click(object sender, EventArgs e)
30.        {
31.            oSvc.Close();
32.            this.lblMsg.Text = "Stopped..";
33.        }
34.    }
35.}
01.<?xml version="1.0" encoding="utf-8" ?>
02.<configuration>
03.    <system.serviceModel>
04.        <behaviors>
05.            <serviceBehaviors>
06.                <behavior name="SampleSvcBahavior">
07.                    <serviceDebug includeExceptionDetailInFaults="true" />
08.                    <serviceMetadata httpGetEnabled="true" />
09.                </behavior>
10.            </serviceBehaviors>
11.        </behaviors>
12.        <services>
13.            <service behaviorConfiguration="SampleSvcBahavior" name="SampleCalcSvc.CalcService">
14.                <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
15.                    name="SampleSvcBasicHttpEndPoint" contract="SampleCalcSvc.ICalcService" />
16.                <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
17.                    name="SampleSvcMexHttpEndPoint" contract="IMetadataExchange" />
18.                <host>
19.                    <baseAddresses>
20.                        <add baseAddress="http://localhost:8080/SampleSvc" />
21.                    </baseAddresses>
22.                </host>
23.            </service>
24.        </services>
25.    </system.serviceModel>
26.</configuration>


Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following

Video/Screen Recording (may not have audio narration/annotations)
You need to Login or Join for FREE to download the following