WCF + WebGet => Http错误请求400 [英] WCF + WebGet => Http Bad request 400

查看:80
本文介绍了WCF + WebGet => Http错误请求400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题困扰了几个小时,我需要有人指出我的错误.

VS2010项目配置为在Visual Studio Development Server的2722端口上运行.

当我尝试浏览此URL时:
http://localhost:2722/Web/EmployeeService.svc/GetEmployee/1

我收到以下错误:
Http 400错误请求.

并在WCF日志文件中:
邮件正文为空,因此无法读取.

感谢您的宝贵时间!


下面是文件:

----------------------------
界面:
Web/IEmployee.cs
----------------------------

I am stuck since severals hours on that problem and i need someone to point me out my mistake.

The VS2010 project is configured to run on the 2722 port with the Visual Studio Development Server.

When i try to browse this url :
http://localhost:2722/Web/EmployeeService.svc/GetEmployee/1

I get the following error :
Http 400 bad request.

And in the WCF log file :
The body of the message cannot be read because it is empty.

Thanks for your time!


Below, the files :

----------------------------
Interface :
Web/IEmployee.cs
----------------------------

namespace WcfServices.Web
{
    [ServiceContract]
    interface IEmployee
    {
        [WebGet(UriTemplate = "GetEmployee/{id=1}")]
        [OperationContract]
        Employee GetEmployee(String id);
    }
}


----------------------------
实现方式:
Web/EmployeeService.cs
----------------------------


----------------------------
The implementation :
Web/EmployeeService.cs
----------------------------

namespace WcfServices.Web
{
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
    public class EmployeeService : IEmployee
    {
        private IList<Employee>_list = new List<Employee>();

        EmployeeService()
        {
            _list.Add(new Employee() { Id = 1, FirstName = "Joe", LastName = "Dalton"});
            _list.Add(new Employee() { Id = 2, FirstName = "Luke", LastName = "Lucky" });
            _list.Add(new Employee() { Id = 3, FirstName = "Ma", LastName = "Dalton" });
            _list.Add(new Employee() { Id = 4, FirstName = "Rantanplan", LastName = "Unknow" });
        }

        Employee IEmployee.GetEmployee(String id)
        {
            int __id;
            if (int.TryParse(id, out __id))
            {
                return _list[__id];    
            }
            return null;
        }
    }
}



--------------------------------------
数据结构:
数据/员工.cs
--------------------------------------



--------------------------------------
The data structure :
Data/Employee.cs
--------------------------------------

namespace WcfServices.Data
{
    [DataContract]
    public class Employee
    {
        [DataMember]
        public int Id { get; set; }
        [DataMember]
        public string LastName { get; set; }
        [DataMember]
        public string FirstName { get; set; }
    }
}


--------------------------------------
web.config
--------------------------------------


--------------------------------------
The web.config
--------------------------------------

 <system.serviceModel>

    <behaviors>
      <serviceBehaviors>

        <behavior name="RESTServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>

      </serviceBehaviors>

      <endpointBehaviors>

        <behavior name="RESTEndPointBehavior">
          <webHttp helpEnabled="true" />
        </behavior>

      </endpointBehaviors>

    </behaviors>

    <services>
      <service name="WCFServices.Web.EmployeeService">
        <endpoint address=""

                  binding="webHttpBinding"

                  behaviorConfiguration="RESTEndPointBehavior"

                  contract="WCFServices.Web.IEmployee">
        </endpoint>
      </service>
    </services>    
    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    
  </system.serviceModel>
  
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

推荐答案

选中此
IIS返回WCF服务文件的400个错误请求" [
Check this
IIS returns 400 "bad request" for WCF service file[^]


这篇关于WCF + WebGet =&gt; Http错误请求400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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