在 Windows 窗体应用程序中托管 WCF 服务 [英] Hosting WCF service inside a Windows Forms application

查看:28
本文介绍了在 Windows 窗体应用程序中托管 WCF 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Windows 窗体应用程序中托管 WCF 服务,并从 Windows 服务调用 WCF 服务,该服务将向 WCF 服务发送数据,WCF 服务将在 Windows 窗体应用程序(桌面应用程序)中显示数据.

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.

推荐答案

这段代码应该足以让你开始:

This code should be enough to get you started:

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>

请注意,当我向项目添加 WCF 服务时,Visual Studio 已生成 App.config.

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

这篇关于在 Windows 窗体应用程序中托管 WCF 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆