这是我的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。谁能告诉我发生了什么?
提前致谢:)
答案 0 :(得分:1)
将绑定从basicHttpBinding
更改为webHttpBinding
。如果要执行REST,则需要使用webHttpBinding。