【问题标题】:Hosting WCF service inside a Windows Forms application在 Windows 窗体应用程序中托管 WCF 服务
【发布时间】:2011-03-08 11:32:33
【问题描述】:

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

我该如何实现呢?我需要能够正常工作并尝试过的代码。

【问题讨论】:

标签: wcf windows-services windows


【解决方案1】:

这段代码应该足以让您入门:

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>

请注意,当我将 WCF 服务添加到我的项目时,Visual Studio 已经生成了 App.config。

【讨论】:

  • 我已经尝试了您的代码,当我在浏览器中键入地址时,它无法打开,当我尝试在网站中为此 WCF 服务添加服务引用时,它失败了!你试过这个代码吗?你有没有尝试调用 WCF 服务?
  • 我在 windows 应用程序中添加了一个新项目(WCF 服务),它创建了 2 个类文件并在 app.config 中为它们添加配置,那么是否有任何需要的配置或缺少的步骤?跨度>
  • 我用 vs2008 试过这段代码,它可以工作。我创建了一个新的 Windows 窗体应用程序,然后添加了 Wcf 服务。我使用位于“Microsoft Visual Studio 9.0\Common7\IDE”中的 WcfTestClient 对其进行了测试。您可以通过将 url 作为第一个参数来启动它:WcfTestClient.exe localhost:8080/MyWcfService
  • 当我在应用程序的属性中添加以下命令时,它在调试选项卡中成功运行(/client:"WcfTestClient.exe"),当获取项目属性时.....我试过了在阅读您的评论之前,我接受了您的回答,因为您愿意回答我。
  • 很好的例子。帮助我理解了背后的逻辑,尤其是 app.config。谢谢
【解决方案2】:

我建议你也使用线程:

    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
        {
        }
    }

【讨论】:

    【解决方案3】:

    使用以下代码在 Windows 窗体应用程序中托管 WCF 服务:

    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/");
    

    【讨论】:

    • 在表单加载的哪里写这段代码?我需要 app.config 中的任何其他配置吗?我在 Windows 应用程序中添加了一个带有其界面的服务
    猜你喜欢
    • 1970-01-01
    • 2021-02-14
    • 2011-10-05
    • 1970-01-01
    • 2019-03-02
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多