简体   繁体   English

自托管服务找不到WCF端点错误

[英]WCF End point not found error from self-hosted service

i´m getting an "End point not found" when calling a WCF service. 调用WCF服务时,出现“找不到端点”。 It is a Self-hosted service in a console app. 它是控制台应用程序中的自托管服务。

Here is my code: 这是我的代码:

IService.cs IService.cs

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

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

} }

Service.cs 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;
    }
}

} }

And the console app: 和控制台应用程序:

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();
    }
}

} }

There is no config file because it is all in the code. 没有配置文件,因为它全部在代码中。

And the call to the service: 并致电该服务:

http://localhost:8000/

Any help would be much appreciated. 任何帮助将非常感激。 Thanks! 谢谢!

Couple of things. 几件事。 Services hosted with WebServiceHost do not publish metadata, so don't bother trying to get a WSDL with just the service name. 使用WebServiceHost托管的服务不会发布元数据,因此不必费心尝试仅使用服务名称来获取WSDL。

Because its WebGet, you are calling the Get method by default when you enter the service name, so you should supply any needed parameters in the URL. 因为它是WebGet,所以在输入服务名称时默认情况下正在调用Get方法,因此您应该在URL中提供所有需要的参数。 However, this won't work until you declare the form of the request in the contract. 但是,直到您在合同中声明了请求的形式,这才起作用。 Do this by modifying the the WebGet line as follows: 通过如下修改WebGet行来执行此操作:

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

Yes, the attribute property must match the formal parameter of the GetMessage() operation, in this case "inputMessage" 是的,attribute属性必须与GetMessage()操作的形式参数匹配,在这种情况下为“ inputMessage”

With the service running, enter the following URL into a browser to verify the service works: 在服务运行的情况下,在浏览器中输入以下URL以验证该服务是否有效:

http://localhost:8000/hello

You should get something like: 您应该得到类似以下内容:

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

我为您找到了WCF REST自托管400错误请求有效地开发了REST服务,并且有两件事要知道:1-使用WebServiceHost时不需要添加EndPoint和Behavior 2- Rest Services没有公开wsdl,例如,您不能从VS添加服务防护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM