REST 服务无法正常工作 400 错误请求
这是我的 web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="WCFRestExample.HelloWorld">
<host>
<baseAddresses>
<add baseAddress="http://localhost:60503"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="WCFRestExample.IHelloWorld"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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>
</behaviors>
</system.serviceModel>
<!--<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="RequestBlockingRule1" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{URL}" pattern="*.aspx" />
</conditions>
<action type="Redirect" url="http://localhost/WCFRestExample/CustomError" />
</rule>
</rules>
</rewrite>
</system.webServer>-->
<connectionStrings>
<add name="PubsEntities" connectionString="metadata=res://*/PubsModel.csdl|res://*/PubsModel.ssdl|res://*/PubsModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
这是我的实际服务:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
namespace WCFRestExample
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class HelloWorld : IHelloWorld
{
[WebGet(UriTemplate="/GetData", ResponseFormat=WebMessageFormat.Xml)]
public string GetData()
{
return "HIIII";
}
[WebGet(UriTemplate = "/GetPublisherList", ResponseFormat = WebMessageFormat.Xml)]
public List<publisher> GetPublisherList()
{
using (PubsEntities entities = new PubsEntities())
{
return entities.publishers.ToList();
}
}
}
}
这是我的界面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
namespace WCFRestExample
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IHelloWorld
{
[OperationContract]
string GetData();
[OperationContract]
List<publisher> GetPublisherList();
}
}
这是我的 svc 标记:
<%@ ServiceHost Language="C#" Debug="true" Service="WCFRestExample.HelloWorld"
Factory="System.ServiceModel.Activation.WebServiceHostFactory"
CodeBehind="HelloWorld.svc.cs" %>
如果我从 web.config 中删除这一部分,它可以正常工作:
<service name="WCFRestExample.HelloWorld">
<host>
<baseAddresses>
<add baseAddress="http://localhost:60503"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="WCFRestExample.IHelloWorld"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
但是该方法返回 List 仍然不起作用。我正在使用 EF 4.1。谁能告诉我发生了什么事吗?
提前致谢 :)
This is my web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="WCFRestExample.HelloWorld">
<host>
<baseAddresses>
<add baseAddress="http://localhost:60503"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="WCFRestExample.IHelloWorld"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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>
</behaviors>
</system.serviceModel>
<!--<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="RequestBlockingRule1" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{URL}" pattern="*.aspx" />
</conditions>
<action type="Redirect" url="http://localhost/WCFRestExample/CustomError" />
</rule>
</rules>
</rewrite>
</system.webServer>-->
<connectionStrings>
<add name="PubsEntities" connectionString="metadata=res://*/PubsModel.csdl|res://*/PubsModel.ssdl|res://*/PubsModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
This is my actual service:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
namespace WCFRestExample
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class HelloWorld : IHelloWorld
{
[WebGet(UriTemplate="/GetData", ResponseFormat=WebMessageFormat.Xml)]
public string GetData()
{
return "HIIII";
}
[WebGet(UriTemplate = "/GetPublisherList", ResponseFormat = WebMessageFormat.Xml)]
public List<publisher> GetPublisherList()
{
using (PubsEntities entities = new PubsEntities())
{
return entities.publishers.ToList();
}
}
}
}
This is my interface:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
namespace WCFRestExample
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IHelloWorld
{
[OperationContract]
string GetData();
[OperationContract]
List<publisher> GetPublisherList();
}
}
This is my markup of svc:
<%@ ServiceHost Language="C#" Debug="true" Service="WCFRestExample.HelloWorld"
Factory="System.ServiceModel.Activation.WebServiceHostFactory"
CodeBehind="HelloWorld.svc.cs" %>
If I remove this section from web.config, it works fine:
<service name="WCFRestExample.HelloWorld">
<host>
<baseAddresses>
<add baseAddress="http://localhost:60503"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="WCFRestExample.IHelloWorld"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
But the method that returns List still doesn't work. I am using EF 4.1. Can anybody tell me what is happening?
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将绑定从
basicHttpBinding
更改为webHttpBinding
。如果你想做REST,你需要使用webHttpBinding。Change the binding from
basicHttpBinding
towebHttpBinding
. If you want to do REST, you need to use the webHttpBinding.