【问题标题】:WCF End point not found error from self-hosted service自托管服务中未找到 WCF 端点错误
【发布时间】:2014-06-29 22:54:01
【问题描述】:

我在调用 WCF 服务时收到“未找到端点”。它是控制台应用程序中的自托管服务。

这是我的代码:

IService.cs

namespace ClassLibrary
{
[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebGet]
    string GetMessage(string inputMessage);

    [OperationContract]
    [WebInvoke]
    string PostMessage(string inputMessage);
}

}

Service.cs

namespace ClassLibrary
{
public class Service : IService
{
    public string GetMessage(string inputMessage)
    {
        return "En GetMessage llega " + inputMessage;
    }

    public string PostMessage(string inputMessage)
    {
        return "En PostMessage llega " + inputMessage;
    }
}

}

还有控制台应用:

namespace ConsoleHost
{
class Program
{
    static void Main(string[] args)
    {            
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:8000"));
        ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
        ServiceDebugBehavior db = host.Description.Behaviors.Find<ServiceDebugBehavior>();
        db.HttpHelpPageEnabled = false;

        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        host.Description.Behaviors.Add(smb);

        host.Open();
        Console.WriteLine("Service is up and running");
        Console.WriteLine("Press enter to quit ");
        Console.ReadLine();
        host.Close();
    }
}

}

没有配置文件,因为它都在代码中。

以及对服务的调用:

http://localhost:8000/

任何帮助将不胜感激。 谢谢!

【问题讨论】:

  • 分享一些配置也可能会有所帮助:众所周知,WCF 错误依赖于配置。
  • 你也可以分享客户端代码吗?您是否在客户端(WebHttpBinding)上使用相同的端点配置?
  • 您确定您的控制台正在运行?!有时...
  • 你为什么要创建一个ServiceEndpoint ep 而不用它做任何事情?这是一个未使用的变量。

标签: c# wcf endpoint self-hosting endpointnotfoundexception


【解决方案1】:

几件事。使用WebServiceHost 托管的服务不发布元数据,因此不必费心尝试仅使用服务名称获取 WSDL。

因为它的WebGet,你在输入服务名称时默认调用Get方法,所以你应该在URL中提供任何需要的参数。但是,除非您在合同中声明请求的形式,否则这将不起作用。通过如下修改 WebGet 行来做到这一点:

[WebGet(UriTemplate = "{inputMessage}")]

是的,attribute 属性必须匹配 GetMessage() 操作的形参,在本例中为“inputMessage”

在服务运行的情况下,在浏览器中输入以下 URL 以验证服务是否正常工作:

http://localhost:8000/hello

你应该得到类似的东西:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
En GetMessage llega hello</string>

【讨论】:

    【解决方案2】:

    我为你找到了这个WCF REST Self-Hosted 400 Bad Request 实际上,您正在开发 REST 服务,为此需要了解两件事: 1-使用WebServiceHost时不需要添加EndPoint和Behaviors 2- Rest Services 没有暴露 wsdl,因此您不能从 VS 添加服务引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多