Получение ошибки 404 из WCF Web API (операция Put) - PullRequest
0 голосов
/ 16 ноября 2011

Я создал svc для отдыха, используя WCF Web API. Мои GET-запросы работают нормально, но я не могу заставить PUT-запросы работать. Я продолжаю получать 404 ошибки, как будто ресурса нет, но он есть.

Код сервера:

    <WebInvoke(UriTemplate:="WebMaint", Method:="Put")>
Public Function WebMaintPut(ByVal entries As HttpRequestMessage(Of IList(Of WebMaint))) As HttpResponseMessage(Of WebMaint)
    Dim response As New HttpResponseMessage(Of WebMaint)(HttpStatusCode.NoContent)
    Using scope = New TransactionScope()
        entries.Content.ReadAs(Of IList(Of WebMaint)).AsParallel().ForAll(Sub(entry)
                                                                              _repos.Update(entry)
                                                                          End Sub)
        scope.Complete()
    End Using

    Return response
End Function

Хост-код:

Public Sub New()
    ObjectFactory.Initialize(Sub(x)
                                 x.For(Of IWebMaintRepository)().Use(Of WebMaintRepository).Ctor(Of String).Is(ConfigurationManager.ConnectionStrings("Dev").ConnectionString)
                             End Sub)
    _host = New HttpServiceHost(ObjectFactory.GetInstance(Of WebMaintResource), "http://localhost:8000/REST/")
    _host.Open()
End Sub

Код клиента:

Private Sub btnSave_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnSave.Click
    Using client = New HttpClient()
        Dim Content = New ObjectContent(Of IList(Of WebMaint))(_WebMaint)
       Dim response = client.Put(New Uri("http://localhost:8000/REST/WebMaint/"), Content)
        Debug.Print(response.StatusCode.ToString())
    End Using
End Sub

Запрос PUT:

PUT http://localhost:8000/REST/WebMaint/ HTTP/1.1
Host: localhost:8000
Content-Length: 592
Expect: 100-continue

<?xml version="1.0" encoding="utf-8"?><ArrayOfWebMaint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><WebMaint><WebMaintenanceID>1</WebMaintenanceID><Maintenance>true</Maintenance><WebSystemId>1</WebSystemId><WebMsgId>4</WebMsgId></WebMaint><WebMaint><WebMaintenanceID>2</WebMaintenanceID><Maintenance>true</Maintenance><WebSystemId>2</WebSystemId><WebMsgId>3</WebMsgId></WebMaint><WebMaint><WebMaintenanceID>3</WebMaintenanceID><Maintenance>true</Maintenance><WebSystemId>3</WebSystemId><WebMsgId>2</WebMsgId></WebMaint></ArrayOfWebMaint>

PUT Response:

HTTP/1.1 404 Not Found
Content-Length: 1565
Content-Type: text/html; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Wed, 16 Nov 2011 19:31:43 GMT

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Service</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Service</p>
      <p>Endpoint not found.</p>
    </div>
  </body>
</html>

Ответы [ 2 ]

2 голосов
/ 17 ноября 2011

Я не знаю, но вы пробовали также PUT вместо Put в атрибуте WebInvoke?(Звучит неловко, но это предварительный просмотр, поэтому это возможно)

Вы пытались использовать List вместо IList?

Другая возможность - вам может потребоваться установить Content-Type в вашем запросе наtext / xml явно.

Если вы размещаете на IIS (Express), вам нужно включить PUT и DELETE , так как они по умолчанию отключены.

0 голосов
/ 17 ноября 2011

Я нашел проблему. Проблема была в следующей строке:

<WebInvoke(UriTemplate:="WebMaint", Method:="Put")>

Верьте или нет, параметр "method" является чувствительным к регистру . Меняя его на:
<WebInvoke(UriTemplate:="WebMaint", Method:="PUT")> исправил проблему. Я думаю, что должно быть лучшее решение, чем просто получить ошибку http 404.

...