1

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

这是我的代码:

IService.cs

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

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

}

服务.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/

任何帮助将非常感激。谢谢!

4

2 回答 2

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>
于 2014-05-12T15:11:15.343 回答
0

我为你找到这个WCF REST 自托管 400 错误请求 实际上你正在开发一个 REST 服务,为此有两件事要知道:1-使用 WebServiceHost 时不需要添加端点和行为 2-休息服务没有暴露 wsdl,因此您不能从 VS 添加服务引用。

于 2014-05-12T14:26:30.520 回答