【发布时间】:2012-05-04 22:02:20
【问题描述】:
我有一个在 winforms 应用程序中自托管的 WCF 服务。我使用了以下链接:
- How to: Host a WCF Service in a Managed Application
- How to: Publish Metadata for a Service Using Code
当我使用 WCF 测试客户端并尝试添加服务时,我收到以下错误: 添加服务失败。服务元数据可能无法访问。确保您的服务正在运行并公开元数据。
错误详情:
错误:无法从http://localhost:8001/HelloWorld 获取元数据如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在指定地址启用元数据发布。
有关启用元数据发布的帮助,请参阅位于 http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange 错误 URI:http://localhost:8001/HelloWorld 的 MSDN 文档
元数据包含无法解析的引用:'http://localhost:8001/HelloWorld'。
http://localhost:8001/HelloWorld 上没有可以接受消息的端点侦听。这通常是由不正确的地址或 SOAP 操作引起的。有关详细信息,请参阅 InnerException(如果存在)。
无法连接到远程服务器 由于目标机器主动拒绝,无法建立连接 127.0.0.1:8001HTTP GET Error URI: http://localhost:8001/HelloWorld
下载“http://localhost:8001/HelloWorld”时出错。
无法连接到远程服务器
无法建立连接,因为目标机器主动拒绝了它 127.0.0.1:8001
这是我的代码:
public Server()
{
InitializeComponent();
using (host = new ServiceHost(typeof(HelloWorldService), new Uri("http://localhost:8001/HelloWorld")))
{
// Check to see if the service host already has a ServiceMetadataBehavior
ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
// If not, add one
if (smb == null)
smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
//You need to add a metadata exchange (mex) endpoint to your service to get metadata.
host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
//http
host.AddServiceEndpoint(typeof(IHelloWorldService), new WSHttpBinding(), "");
host.Open();
}
}
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string SayHello(string name);
}
public class HelloWorldService : IHelloWorldService
{
public string SayHello(string name)
{
return string.Format("Hello, {0}", name);
}
}
【问题讨论】:
-
@jskiles1 我关闭了 Windows 防火墙。还是不行。
标签: c# .net wcf exception-handling