2012-08-05 82 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