2012-08-05 127 views
1

我們在設立每這些指導原則一個RESTful WCF服務的過程:
http://www.codeproject.com/Articles/255684/Create-and-Consume-RESTFul-Service-in-NET-Framewor未經授權的HTTP請求消費簡單WCF服務

的說明都很好,我們已經成功地得到了我們的測試版本運行和顯示XML結果在瀏覽器中(在我們的例子中爲Person記錄)。

我們遇到的一個問題是作者在瀏覽器中顯示XML之外並沒有太多的幫助。

你會認爲一旦結果以XML形式呈現給你,它會非常容易地將一個可以將數據反序列化到你的Person類的客戶端。

其實並不那麼容易。很多人對我們的結合點和終點有些撓頭......不知道我們是否已經正確掌握了。無論如何,一段時間後,發現其中一半是有道理的代碼示例,並撞倒這件事:

Dim persons As Persons 
Using host As New WebServiceHost(GetType(MyTestService), New Uri("http://MyServer:8443/TestService/")) 
    host.AddServiceEndpoint(GetType(IMyTestService), New BasicHttpBinding(), "") 
    host.Open() 

    Using cf As New ChannelFactory(Of IMyTestService)(New BasicHttpBinding(), "http://MyServer:8443/TestService/") 
     cf.Endpoint.Behaviors.Add(New WebHttpBehavior()) 
     Dim channel As IMyTestService = cf.CreateChannel() 


     persons = channel.GetPersons 
    End Using 

    MsgBox(persons.Count) 

End Using 

唯一的問題是它的失敗就行了persons = channel.GetPersons與此錯誤:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM'.

現在,當服務器非常樂意通過IE或Chrome提供數據時,發現身份驗證錯誤令我感到很奇怪。

然後我們試圖改變WebHttpBindingBasicHttpBinding更希望好於預期,因爲我們真的不知道什麼這些方法真的這樣做在表面之下,但產生一個錯誤:

The endpoint at 'http://MyServer:8443/TestService/' does not have a Binding with the None MessageVersion. 'System.ServiceModel.Description.WebHttpBehavior' is only intended for use with WebHttpBinding or similar bindings.

回答

1

嗯...你可見的代碼示例將使用basicHttpBinding這是一個SOAP約束力,無關與REST是一個WCF服務的服務器邊......這是絕對爲客戶端的WCF不代碼REST服務!

假設你的WCF REST服務啓動和運行,你的客戶也會是相當簡單的,就像這樣:

// create a new web request to query your service 
WebRequest request = WebRequest.Create("http://MyServer:8443/TestService/"); 

// get the response 
WebResponse response = request.GetResponse(); 

// examine some properties of the response 
long length = response.ContentLength; 
string contentType = response.ContentType; 

// fetch the XML (or JSON) contents of the response 
string content = new StreamReader(response.GetResponseStream()).ReadToEnd(); 
+0

我知道了起來,現在正在運行。謝謝你的時間。您100K +代表用戶是StackOverflow的奧運金牌得主。我仍然沒有得到綁定和端點的東西,但也許我不需要。 – hawbsl 2012-08-06 08:23:19

+1

我_did_得到401未經授權的請求的情況,但計算出使用其他StackOverflow答案。再次感謝。 – hawbsl 2012-08-06 08:24:14