Служба REST не работает 400 неверный запрос - PullRequest
0 голосов
/ 26 июля 2011

Это мой 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=&quot;Data Source=.\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=True;MultipleActiveResultSets=True&quot;" 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.Кто-нибудь может сказать мне, что происходит?

Заранее спасибо:)

1 Ответ

1 голос
/ 26 июля 2011

Измените привязку с basicHttpBinding на webHttpBinding. Если вы хотите сделать REST, вам нужно использовать webHttpBinding.

...