我在winforms应用程序中有一个自托管的WCF服务。我使用了以下链接:
当我使用并尝试添加服务时,我会得到以下错误:添加服务失败。服务元数据可能无法访问。确保您的服务正在运行并公开元数据。
错误详细信息:
错误:如果这是您可以访问的Windows (R)通信基础服务,则无法从http://localhost:8001/HelloWorld获取元数据,请检查是否已启用指定地址的元数据发布。 有关启用元数据发布的帮助,请参阅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:8001HTTPGET错误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);
}
}发布于 2012-05-07 20:26:09
我不知道为什么,但是将sure (){}切换到try{}..catch{}允许此代码正常工作。WCF可以成功地添加服务,我可以通过:http://localhost:8001/HelloWorld浏览到正在运行的服务
发布于 2012-05-05 01:25:15
从根本上看,这一问题的根源在于:
无法连接到远程服务器,无法连接,因为目标计算机主动拒绝了127.0.0.1:8001
我会检查您的事件日志,看看host.Open()调用是否失败(可能是由于防火墙或其他原因),或者使用调试器遍历它,然后使用telnet查看它是否真的在监听8001。
发布于 2012-05-17 11:51:12
以下是它的解决方案--尝试一下--它将起作用
Uri baseAddress = new Uri("http://localhost:8001/HelloWorld");
// Create the ServiceHost.
using (serviceHost = new ServiceHost(typeof(HelloWorldService), baseAddress))
{
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
serviceHost.Description.Behaviors.Add(smb);
// Open the ServiceHost to start listening for messages. Since
// no endpoints are explicitly configured, the runtime will create
// one endpoint per base address for each service contract implemented
// by the service.
serviceHost.AddServiceEndpoint(typeof(IHelloWorldService), new WSHttpBinding(), "");
serviceHost.Open();
}https://stackoverflow.com/questions/10456687
复制相似问题