WCF REST服务POST返回IIS上找不到404 [英] WCF REST Service POST returns 404 Not Found on IIS

查看:82
本文介绍了WCF REST服务POST返回IIS上找不到404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a WCF service created with VS2012 on Win8. If I start the service via VS (localhost:port) I'm able to do GET's and POST's. When I deploy to IIS on the same machine only GET works. The POST return 404 Not Found. I've tried deploying directly to IIS by creating an application off of my Default Web Site as well as using VS Publish.

POST URL: http://www.server.com/RestService/RestServiceImpl.svc /auth

POST URL: http://www.server.com/RestService/RestServiceImpl.svc/auth

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

POST Request Header: contents of PostData.xml below

Web.config:

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:

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

RestServiceImpl.svc

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

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

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

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

PostData.xml

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

推荐答案

我发现了问题.该应用程序的IIS请求筛选将POST设置为不允许.

I found the problem. IIS Request Filtering for the application had POST set to not allowed.

这篇关于WCF REST服务POST返回IIS上找不到404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆