Hosting WCF service inside a Windows Forms application

前端 未结 3 1194
北荒
北荒 2020-12-03 03:50

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

相关标签:
3条回答
  • 2020-12-03 04:29

    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.

    0 讨论(0)
  • 2020-12-03 04:50

    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/");
    
    0 讨论(0)
  • 2020-12-03 04:54

    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 讨论(0)
提交回复
热议问题