【问题标题】:no endpoint listening at local host没有在 localhost 监听的端点
【发布时间】:2012-03-27 16:18:43
【问题描述】:

我的控制台应用程序托管的 wcf 服务在客户端出现错误。现在这适用于我的浏览器,但不适用于我的 win 表单,它是一个简单的按钮文本框和标签:

    public ServiceReference1.Service1Client testClient = new ServiceReference1.Service1Client();

    private void button1_Click_1(object sender, EventArgs e)
    {
        label1.Text = testClient.GetData(Convert.ToString(textBox1.Text));
    }

我得到的错误是:

http://localhost:8000/hello 没有端点监听 可以接受消息。这通常是由不正确的地址引起的 或 SOAP 操作。有关详细信息,请参阅 InnerException(如果存在)。

主机控制台应用代码:

class Program
{
    static void Main(string[] args)
    {
        WebHttpBinding binding = new WebHttpBinding();
        WebServiceHost host =
        new WebServiceHost(typeof(Service1));
        host.AddServiceEndpoint(typeof(IService1),
        binding,
        "http://localhost:8000/hello");
        host.Open();
        Console.WriteLine("Hello world service");
        Console.WriteLine("Press <RETURN> to end service");
        Console.ReadLine();
    }
}

Client App.cofig 文件代码:

<configuration>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="WebHttpBinding_IService1">
                    <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                        messageVersion="Soap12" writeEncoding="utf-8">
                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    </textMessageEncoding>
                  <httpTransport></httpTransport>
                </binding>

            </customBinding>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>

                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8000/hello" binding="customBinding" bindingConfiguration="WebHttpBinding_IService1"
                contract="ServiceReference1.IService1" name="WebHttpBinding_IService1" />
            <endpoint address="http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
                contract="ServiceReference1.Service1" name="WSHttpBinding_IService1">
                <identity>
                    <dns value="localhost" />
                </identity>

            </endpoint>

        </client>

    </system.serviceModel>
</configuration>

【问题讨论】:

  • 如果您使用 webHttpBinding 公开您的服务,那么它是一个 RESTful 服务,您可以使用 HTTP GET/POST 等动词访问它。托管服务后,尝试从浏览器浏览到 URL localhost:8000/hello,看看您在浏览器中得到的响应是什么。
  • 如果我浏览到 localhost:8000/hello 我得到一个 Endpoint not found。但是,如果我浏览到 localhost:8000/hello/mystring 我会得到我正在寻找的响应。以上应该可以正常工作,客户端应该能够在文本框中写入,然后单击按钮,然后发送和检索信息?
  • 但是它不是抛出这个错误localhost:8000/hello没有可以接受消息的端点监听。这意味着代码label1.Text = testClient.GetData(Convert.ToString(textBox1.Text)); 没有将其添加到/url 的末尾
  • 如果您将其公开为 RESTful 服务,那么您需要将其附加为资源名称,并且使用 SOAP 访问它不是正确的方法。如果你想通过soap访问它,那么定义一个具有BasicHttpBinding的端点。
  • 在您的控制台应用程序中,您使用 WebHttpBinding 而不是在托管服务时使用 BasicHttpBinding

标签: c# wcf rest


【解决方案1】:

为了访问 RESTful 服务,您可以使用以下代码:

private string UseHttpWebApproach<T>(string serviceUrl, string resourceUrl, string method, T requestBody)
        {
            string responseMessage = null;
            var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
            if (request != null)
            {
                request.ContentType = "application/xml";
                request.Method = method;
            }    

            if(method == "POST" && requestBody != null)
            {                    
                byte[] requestBodyBytes = ToByteArrayUsingJsonContractSer(requestBody);                
                request.ContentLength = requestBodyBytes.Length;
                using (Stream postStream = request.GetRequestStream())
                    postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);

            }

            if (request != null)
            {
                var response = request.GetResponse() as HttpWebResponse;
                if(response.StatusCode == HttpStatusCode.OK)
                {
                    Stream responseStream = response.GetResponseStream();
                    if (responseStream != null)
                    {
                        var reader = new StreamReader(responseStream);

                        responseMessage = reader.ReadToEnd();
                    }
                }
                else
                {
                    responseMessage = response.StatusDescription;
                }
            }
            return responseMessage;
        }

ServiceUrl : http://localhost:8000

ResourceUrl : hello/yourstring

方法:GET

请求正文:空

【讨论】:

  • 是的,您可以使用客户端应用程序中的代码来调用 RESTful 服务
猜你喜欢
  • 2010-11-17
  • 1970-01-01
  • 2014-12-05
  • 1970-01-01
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多