【发布时间】:2015-02-01 19:23:55
【问题描述】:
我正在使用此示例创建一个自托管 WCF 应用程序 (.NET 4.5): https://msdn.microsoft.com/en-us/library/ms731758%28v=vs.110%29.aspx
我已经成功使用它创建了一个控制台应用程序。
但我现在正尝试在全新的 WPF 应用程序中使用相同的代码。
我唯一改变的是删除:
host.Close();
所以应用程序在连接打开的情况下运行。
但是当我使用 WCF 测试客户端测试 WPF 应用程序时,它返回错误:
Error: Cannot obtain Metadata from http://localhost:8080/hello If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:8080/hello Metadata contains a reference that cannot be resolved: 'http://localhost:8080/hello'. There was no endpoint listening at http://localhost:8080/hello that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:8080HTTP GET Error URI: http://localhost:8080/hello There was an error downloading 'http://localhost:8080/hello'. Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:8080
如果我设置断点:
host.Open();
它已运行,所以应该运行该服务好吗?
我的 WPF 应用程序与导致此问题的控制台应用程序有什么区别。在这两种情况下,Visual Studio 都在管理员下运行。
主要代码:
using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
{
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.Open();
//Console.WriteLine("The service is ready at {0}", baseAddress);
//Console.WriteLine("Press <Enter> to stop the service.");
//Console.ReadLine();
// Close the ServiceHost.
//host.Close();
}
【问题讨论】:
-
我不同意。问题更多是在问为什么控制台应用程序和 WPF 应用程序之间存在差异
-
当您说您所做的唯一事情是删除
host.Close()呼叫时,我假设您指的是 MSDN 链接中的第 8 步。但是,鉴于您现在将服务托管在 WPF 应用程序中,我也假设您已从示例中删除了Console.ReadLine()调用,这是阻止 that 退出using块并处理ServiceHost实例的代码。您能否包含用于从 WPF 应用程序启动ServiceHost而不是链接到 MSDN 的代码? -
已编辑,是因为它存在using语句吗?如果是这样,请将其放在答案中,我会将其标记为已回答。非常感谢!
-
啊,我删除了 using 语句,它起作用了。我真傻。我对使用语句相当陌生。