繁体   English   中英

在WPF应用程序中托管WCF服务

[英]Hosting WCF Service in WPF Application

我正在尝试在WPF应用程序中托管WCF服务,但无法这样做。

这是我实现的代码:

ServiceHost host = null;
using (host = new ServiceHost(typeof(WcfJsonTransferRestService.apiService)))
            {
                host.AddServiceEndpoint(typeof(WcfJsonTransferRestService.IApiService), new WebHttpBinding(), "http://localhost:3300/api/endpoint1");
                host.AddServiceEndpoint(typeof(WcfJsonTransferRestService.IApiService), new WebHttpBinding(), "http://localhost:3300/api/endpoint2");
                host.AddServiceEndpoint(typeof(WcfJsonTransferRestService.IApiService), new WebHttpBinding(), "http://localhost:3300/api/endpoint3");

host.Open();

一切看起来都不错,并且运行正常,但是该服务无法启动。

有人知道我的问题可能是什么吗?

谢谢

最可能的问题是,您已经在using语句中包装了ServiceHost的创建和打开。 一旦using语句完成(并且从您发布的代码中尚不清楚它的位置),将关闭ServiceHost实例。

换句话说,如果在host.Open();之后host.Open();关闭usinghost.Open(); 像这样:

using (host = new ServiceHost(typeof(WcfJsonTransferRestService.apiService)))
{
    host.AddServiceEndpoint(typeof(WcfJsonTransferRestService.IApiService), new WebHttpBinding(), "http://localhost:3300/api/endpoint1");
    host.AddServiceEndpoint(typeof(WcfJsonTransferRestService.IApiService), new WebHttpBinding(), "http://localhost:3300/api/endpoint2");
    host.AddServiceEndpoint(typeof(WcfJsonTransferRestService.IApiService), new WebHttpBinding(), "http://localhost:3300/api/endpoint3");

    host.Open();
}

主机将关闭,您的应用程序将无法接收请求。 通常,您将需要在应用程序启动时(或可能在给定事件上)打开主机,然后在应用程序退出时将其关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM