9

I need to host a WCF service inside a Windows Forms application and call the WCF service from a Windows service that will send data to the WCF service which will show it in the Windows Forms application (desktop application).

How can I implement this? I need code that is working correctly and tried before.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahmy
  • 5,420
  • 8
  • 39
  • 50
  • Could be of use: *[Hosting a WCF Service Library using Windows Forms](http://www.dotnetcodecentral.com/Post/43/wcf-hosting/host-wcf-service-using-windows-forms)* – Peter Mortensen Jul 24 '15 at 14:45

3 Answers3

5

This code should be enough to get you started:

Form1.cs

namespace TestWinform
{
    public partial class Form1 : Form
    {
        private ServiceHost Host;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Host = new ServiceHost(typeof(MyWcfService));
            Host.Open();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Host.Close();
        }
    }
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="TestWinform.MyWcfServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="TestWinform.MyWcfServiceBehavior"
                name="TestWinform.MyWcfService">
                <endpoint address="" binding="wsHttpBinding" contract="TestWinform.IMyWcfService">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8080/MyWcfService/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

Note that the App.config has been generated by Visual Studio when I added a WCF Service to my project.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Keeper
  • 3,516
  • 1
  • 18
  • 29
  • I have tried your code and when i type the address in a browser it doesn't open and when i try to add a service reference in a website for this WCF service it fails! did you try this code? and did you try to call WCF service ? – Ahmy Mar 08 '11 at 12:25
  • I have added a new item (WCF service) to the windows application and it creates 2 class files and add configuration for them in the app.config so is there any needed configuration or missing steps? – Ahmy Mar 08 '11 at 12:27
  • I tried this code using vs2008 and it works. I created a new Windows Form Application then I added the Wcf Service. I tested it using WcfTestClient which is located in "Microsoft Visual Studio 9.0\Common7\IDE". You can launch it passing the url as the first parameter: WcfTestClient.exe http://localhost:8080/MyWcfService/ – Keeper Mar 08 '11 at 13:23
  • When i added in the properties of the application the following command it worked successfully(/client:"WcfTestClient.exe") at the debug tab when get the project properties..... i have tried it before reading your comment but i accepted your answer because of you care to answer me. – Ahmy Mar 08 '11 at 14:07
  • Great example. helped me a lot to understand the logic behind especially with the app.config. thanks – smoothumut Apr 15 '17 at 07:09
2

I recomend you to also use a Thread:

    private void Form1_Load(object sender, EventArgs e)
    {
        Task.Factory.StartNew(() =>
        {
            host = new ServiceHost(typeof(AssinadorWcf.AssinadorDigitalLecom));
            host.Open();
        });
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        try
        {
            host.Close();
        }
        catch
        {
        }
    }
0

Use the below code to host the WCF service in a Windows Forms application:

using System.ServiceModel.Channels;
ServiceHost host = new ServiceHost(typeof(MyNamespace.OrderService));
BindingElementCollection bec = new BindingElementCollection();
SymmetricSecurityBindingElement ssbe = new
SymmetricSecurityBindingElement();
ssbe.LocalServiceSettings.InactivityTimeout = new TimeSpan(0, 10, 0);
bec.Add(ssbe);
bec.Add(new TextMessageEncodingBindingElement());
bec.Add(new HttpsTransportBindingElement());
CustomBinding customBinding = new CustomBinding(bec);
host.AddServiceEndpoint(typeof(MyNamespace.IOrderService),customBinding,"http://localhost:8000/O
rderService/");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nima M
  • 718
  • 10
  • 18
  • 1
    Where to write this code in the form load ? and am i need any other configuration in the app.config ? i have added a service with its interface inside the windows application – Ahmy Mar 08 '11 at 11:56