2012-03-25 40 views
3

我的控制檯應用程序託管的我的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(); 
    } 
} 

客戶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> 
+0

如果您正在使用的WebHttpBinding那麼它的RESTful服務暴露你的服務,你可以使用HTTP GET訪問/ POST等。動詞嘗試在託管服務後從瀏覽器瀏覽到URL http:// localhost:8000/hello,看看您在瀏覽器中得到的回覆是什麼。 – Rajesh 2012-03-26 08:22:38

+0

如果我瀏覽到localhost:8000/hello,我找不到端點。但是,如果我瀏覽到localhost:8000/hello/mystring,我收到了我正在尋找的響應。以上應該可以正常工作,客戶端應該能夠在文本框中編寫,然後在按鈕上單擊信息然後發送和檢索? – 2012-03-26 08:45:12

+0

然而,它不會引發這個錯誤沒有端點監聽http:// localhost:8000/hello可以接受消息。這意味着代碼'label1.Text = testClient.GetData(Convert.ToString(textBox1.Text));'沒有將它添加到'/ url'的末尾 – 2012-03-26 08:48:51

回答

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

RequestBody:空

+0

這是客戶端代碼嗎?從我的Windows窗體? – 2012-04-04 14:52:30

+0

是的,您可以使用客戶端應用程序中的代碼來調用RESTful服務 – Rajesh 2012-04-04 16:26:24

相關問題