没有端点在本地主机上监听

3

我在使用控制台应用程序托管的WCF服务时在客户端遇到了错误。虽然在浏览器中可以正常工作,但在我的WinForms中却不能。我的WinForms只包含一个简单的按钮,文本框和标签。

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

客户端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 http://localhost:8000/hello,查看浏览器中获得的响应。 - Rajesh
如果我浏览到localhost:8000/hello,我会得到一个“Endpoint not found”的错误。但是,如果我浏览到localhost:8000/hello/mystring,我会得到我想要的响应。上述内容应该可以正常工作,客户端应该能够在文本框中编写信息,并在单击按钮后发送和检索信息? - Kirsty White
然而,如果它不是这样,它会抛出以下错误:没有端点在http://localhost:8000/hello处侦听可以接受消息的内容。这意味着代码`label1.Text = testClient.GetData(Convert.ToString(textBox1.Text));没有将其添加到/url`的末尾。 - Kirsty White
如果您将其公开为RESTful服务,则需要将其附加为资源名称,并且使用SOAP访问它不是正确的方法。如果您想通过SOAP访问它,请定义一个具有BasicHttpBinding的终结点。 - Rajesh
在您的控制台应用程序中,当托管服务时,请使用BasicHttpBinding而不是WebHttpBinding。 - Rajesh
显示剩余2条评论
1个回答

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

服务地址: http://localhost:8000

资源地址: hello/yourstring

请求方式: GET

请求体: 空


是的,您可以在客户端应用程序中使用该代码来调用RESTful服务。 - Rajesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接