C# 如何使简单的WCF服务在WPF应用程序中工作?
我一直在学习教程,并尝试使用WCF托管一个简单的REST服务器。基本上,我创建了WCF接口和类文件,如教程中所述:C# 如何使简单的WCF服务在WPF应用程序中工作?,c#,.net,wpf,rest,wcf,C#,.net,Wpf,Rest,Wcf,我一直在学习教程,并尝试使用WCF托管一个简单的REST服务器。基本上,我创建了WCF接口和类文件,如教程中所述: [ServiceContract] public interface IService { [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "/GET/{msg}/{msg2}")] string GetRequest(string msg, string msg2); [O
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GET/{msg}/{msg2}")]
string GetRequest(string msg, string msg2);
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "/PUT/{msg}")]
void PutRequest(string msg, Stream contents);
}
混凝土等级:
class Service : IService
{
static string Message = "Hello";
public string GetRequest(string msg, string msg2)
{
return Message + msg + msg2;
}
public void PutRequest(string msg, Stream contents)
{
Service.Message = msg + msg + msg;
string input = new StreamReader(contents).ReadToEnd();
Console.WriteLine("In service, input = {0}", input);
}
}
这两个WCF服务类在我创建的控制台应用程序中工作。下面是“Main”的样子当我向控制台应用程序提交GET请求时,我得到了一个200确定值:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (var host = new ServiceHost(typeof(Service)))
{
var ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), new Uri("http://1.10.100.126:8899/MyService"));
ep.EndpointBehaviors.Add(new WebHttpBehavior());
host.Open();
Console.WriteLine("Service is running");
Console.ReadLine();
host.Close();
}
}
}
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
using (var host = new ServiceHost(typeof(Service)))
{
var ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), new Uri("http://1.10.100.126:8899/MyService"));
ep.EndpointBehaviors.Add(new WebHttpBehavior());
host.Open();
Console.WriteLine("Service is running");
Console.ReadLine();
host.Close();
}
}
}
}
}
但是,当我想在WPF应用程序中使用这两个类时,它们就不再工作了。下面是WPF应用程序的主窗口类当我向WPF应用程序提交GET请求时,我收到错误502 BAD GATEWAY:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (var host = new ServiceHost(typeof(Service)))
{
var ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), new Uri("http://1.10.100.126:8899/MyService"));
ep.EndpointBehaviors.Add(new WebHttpBehavior());
host.Open();
Console.WriteLine("Service is running");
Console.ReadLine();
host.Close();
}
}
}
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
using (var host = new ServiceHost(typeof(Service)))
{
var ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), new Uri("http://1.10.100.126:8899/MyService"));
ep.EndpointBehaviors.Add(new WebHttpBehavior());
host.Open();
Console.WriteLine("Service is running");
Console.ReadLine();
host.Close();
}
}
}
}
如何使这两个WCF类与一个简单的空WPF应用程序项目一起工作为什么这两个WCF类与一个空的控制台应用程序项目一起工作,而不是与一个空的WPF应用程序项目一起工作?为您提供一个关于如何在WPF应用程序中正确托管WCF服务的完整、彻底的答案确实有点过于宽泛,但这里有一些提示 您的WPF尝试有几个主要问题:
Console.ReadLine()
来阻止线程,而在GUI应用程序中没有Console
侦听-因此Console.ReadLine()
只是立即返回(但如果您确实阻止了线程,您将回到问题1)这里肯定有可以改进的地方-启动和管理服务的生命周期,在WPF中使用命令模型,使用TPL或BackgroundWorker在不同的线程中运行服务,更充分地利用MVVM模式-但这只是一个开始。
Console.ReadLine
在这种情况下不会导致线程暂停。因此,主机
将在打开后立即关闭。您可能应该移动主机。关闭某个“退出”按钮。您需要将host
变量设置为成员变量,这样才能工作。我已尝试从WPF应用程序中完全删除Console.Readline()和host.Close()行。不过我还是得到了502分,我不认为主持人在开幕后就关门是原因。可能主机没有打开?您可以演示如何执行GET
请求吗?您还需要使用语法删除<代码>使用
将处理您的主机
对象。另外,我认为您应该将host
作为一个实例变量。@Alisson我使用Fiddler测试我的GET请求,因此基本上是对“”的GET请求,这是针对WPF和控制台的