нет прослушивания конечной точки на локальном хосте - PullRequest
3 голосов
/ 26 марта 2012

Я получаю сообщение об ошибке на клиенте для моей службы wcf, размещенной в моем консольном приложении. Теперь это работает в моем браузере, но не в моих выигрышных формах, которые представляют собой простое текстовое поле и метку кнопки:

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

Код файла клиентского приложения.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>

1 Ответ

1 голос
/ 27 марта 2012

Для доступа к услуге 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

RequestBody: Null

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...