Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF应用程序中托管WCF服务_C#_Wpf_Wcf_Wcf Rest - Fatal编程技术网

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

C# 在WPF应用程序中托管WCF服务,c#,wpf,wcf,wcf-rest,C#,Wpf,Wcf,Wcf Rest,我试图在WPF应用程序中托管WCF服务,但我无法这样做 以下是我实现的代码: ServiceHost host = null; using (host = new ServiceHost(typeof(WcfJsonTransferRestService.apiService))) { host.AddServiceEndpoint(typeof(WcfJsonTransferRestService.IApiService), new We

我试图在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();
一切正常,运行正常,但服务未启动

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


谢谢

最有可能的问题是,您已经使用语句将
ServiceHost
的创建和打开包装在
中。一旦
using
语句完成(您发布的代码不清楚它在哪里),将关闭
ServiceHost
实例

换句话说,如果在
host.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();
}

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

最可能的问题是,您已使用
语句将
ServiceHost
的创建和打开包装在
中。一旦
using
语句完成(您发布的代码不清楚它在哪里),将关闭
ServiceHost
实例

换句话说,如果在
host.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();
}

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

您是否尝试在调试器中单步执行代码?是否抛出任何错误?事件查看器中是否有任何内容?是的,所有内容都创建得很好,但是当我尝试连接(使用高级REST客户端)时,我得到一个错误,好像服务没有从您发布的代码中运行,看起来您正在托管SOAP服务-REST与SOAP不同。您是否尝试过使用
WebHttpBinding
而不是
WSHttpBinding
?我以前确实使用过basicHttpBinding,我尝试了两种不同的方法,最终我得到了WSHttpBinding…但你是对的,我本应该使用WebHttpBinding…但仍然没有乐趣。我编辑了我的问题,将WebHttpBinding包括在内。你尝试过在调试器中单步执行代码吗?是否抛出任何错误?事件查看器中是否有任何内容?是的,所有内容都创建得很好,但是当我尝试连接(使用高级REST客户端)时,我得到一个错误,好像服务没有从您发布的代码中运行,看起来您正在托管SOAP服务-REST与SOAP不同。您是否尝试过使用
WebHttpBinding
而不是
WSHttpBinding
?我以前确实使用过basicHttpBinding,我尝试了两种不同的方法,最终我得到了WSHttpBinding…但你是对的,我应该使用WebHttpBinding…但仍然没有乐趣。我编辑了我的问题,将WebHttpBinding包括在内。谢谢Tim!我不知道我怎么会错过它!我认为有几个示例具有使用功能,可以复制到peoples代码中。谢谢Tim!我不知道我怎么会错过它!我认为有几个示例具有using-in,它被复制到了peoples代码中。