【问题标题】:WCF REST Service POST returns 404 Not Found on IISWCF REST 服务 POST 返回 404 Not Found on IIS
【发布时间】:2013-08-02 04:22:56
【问题描述】:

我在 Win8 上使用 VS2012 创建了 WCF 服务。如果我通过 VS (localhost:port) 启动服务,我可以执行 GET 和 POST。当我在同一台机器上部署到 IIS 时,只有 GET 有效。 POST 返回 404 Not Found。我已经尝试通过从我的默认网站创建应用程序以及使用 VS Publish 直接部署到 IIS。

发布网址:http://www.server.com/RestService/RestServiceImpl.svc/auth

POST 请求头:以下 PostData.xml 的内容

Web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<system.web>
    <compilation debug="true" targetFramework="4.0" />
    <customErrors mode="Off"/>
</system.web>

<system.serviceModel>
    <services>
      <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
          <!-- Service Endpoints -->
          <!-- Unless fully qualified, address is relative to base address supplied above -->
          <endpoint address="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="RestServiceImplEndpointBehavior">
              <!-- Upon deployment, the following identity element should be removed or replaced to reflect the 
                   identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity automatically. -->
          </endpoint>
       </service>
    </services>

    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceBehaviour">
                <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="RestServiceImplEndpointBehavior">
                <webHttp />
            </behavior>
        </endpointBehaviors>
    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
    </system.serviceModel>

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        <directoryBrowse enabled="true" />
    </system.webServer>

    <system.diagnostics>
        <sources>
            <source name="System.ServiceModel" 
                switchValue="Information, ActivityTracing"
                propagateActivity="true">
                <listeners>
                    <add name="traceListener" 
                        type="System.Diagnostics.XmlWriterTraceListener" 
                        initializeData= "c:\log\Traces.svclog" />
                </listeners>
            </source>
        </sources>
    </system.diagnostics>
</configuration>

iRestServerImpl.cs:

using System.ServiceModel;
using System.ServiceModel.Web;

namespace RestService
{

    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "xml/{id}")]
        string XMLData(string id);

        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/{id}")]
        string JSONData(string id);

        [OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Xml,
            RequestFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "auth")]
        ResponseData Auth(RequestData rData);

    }
}

RestServiceImpl.svc

<%@ ServiceHost Language="C#" Debug="true" Service="RestService.RestServiceImpl" CodeBehind="RestServiceImpl.svc.cs" %>

RestServiceImpl.svc.cs

namespace RestService
{
    public class RestServiceImpl : IRestServiceImpl
    {
        #region IRestServiceImpl Members

        public string XMLData(string id)
        {
            return "You requested product " + id;
        }

        public string JSONData(string id)
        {
            return "You requested product " + id;
        }

        public ResponseData Auth(RequestData rData)
        {
            // Call BLL here
            var data = rData.details.Split('|');
            var response = new ResponseData
                               {
                                   Name = data[0],
                                   Age = data[1],
                                   Exp = data[2],
                                   Technology = data[3]
                               };

            return response;
        }

        #endregion




    }
}

RequestData.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace RestService
{
    [DataContract(Namespace = "http://www.eysnap.com/mPlayer")]
    public class RequestData
    {
        [DataMember]
        public string details { get; set; }
    }
}

ResponseData.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace RestService
{
    [DataContract]
    public class ResponseData
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string Age { get; set; }

        [DataMember]
        public string Exp { get; set; }

        [DataMember]
        public string Technology { get; set; }
    }
}

PostData.xml

<RequestData xmlns="http://www.eysnap.com/mPlayer">
  <details>Ashu|29|7 Years|.NET</details>
</RequestData>

【问题讨论】:

    标签: wcf post iis-8


    【解决方案1】:

    我发现了问题。应用程序的 IIS 请求过滤已将 POST 设置为不允许。

    【讨论】:

    • 你是怎么做到的?你在哪里修的?
    • 不太一样,我只需要添加 POST,因为那里什么都没有。进入下一个错误
    【解决方案2】:

    您的代码适用于 POST 和 GET,虽然我有 VS 2010 和 IIS 7,但我认为这没有问题。我在网站下发布了服务作为应用程序 (POSTDataIssue)。 我使用提琴手首先测试 GET 请求(URL - http://localhost:82/POSTDataIssue/RestServiceImpl.svc/json/5),它给出了预期的结果。

    然后我尝试将以下内容发布到 URL http://localhost:82/POSTDataIssue/RestServiceImpl.svc/auth-

    User-Agent: Fiddler
    Host: localhost:82
    Content-Type: application/xml
    Content-Length: 110
    
    
    <RequestData xmlns="http://www.eysnap.com/mPlayer">
     <details>Ashu|29|7 Years|.NET</details>
    </RequestData>
    
    
    
     Response 
     HTTP/1.1 200 OK
     Content-Length: 218
     Content-Type: application/xml; charset=utf-8
     Server: Microsoft-IIS/7.5
     X-Powered-By: ASP.NET
     Date: Thu, 01 Aug 2013 02:34:58 GMT
    
     <ResponseData xmlns="http://schemas.datacontract.org/2004/07/RestService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Age>29</Age><Exp>7 Years</Exp><Name>Ashu</Name><Technology>.NET</Technology></ResponseData>
    

    【讨论】:

    • 您从 POST 收到的回复是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多