在 Windows 窗体应用程序中托管 WCF 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5231867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hosting WCF service inside a Windows Forms application
提问by Ahmy
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).
我需要在 Windows 窗体应用程序中托管 WCF 服务,并从 Windows 服务调用 WCF 服务,该服务会将数据发送到 WCF 服务,该服务将在 Windows 窗体应用程序(桌面应用程序)中显示它。
How can I implement this? I need code that is working correctly and tried before.
我该如何实施?我需要可以正常工作并尝试过的代码。
采纳答案by Keeper
This code should be enough to get you started:
这段代码应该足以让你开始:
Form1.cs
表格1.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.
请注意,当我向项目添加 WCF 服务时,Visual Studio 已生成 App.config。
回答by user3096335
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
{
}
}
回答by Nima M
Use the below code to host the WCF service in a Windows Forms application:
使用以下代码在 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/");