发布在IIS 7中返回Json的Restful Service

时间:2012-04-19 04:54:29

标签: wcf iis rest iis-7 wcf-rest

最近我一直试图在IIS7中发布Restful服务但是没有运气。发布时,它无法识别我在web.config中指定的端点。 IIS 7现在是本地主机,一旦我启动它,我将把它移动到我的客户端服务器。以下是界面:

[ServiceContract]
public interface ICOService
{
    /// <summary>
    /// Devuelve la lista de los anos de los autos (lol)
    /// </summary>
    /// <returns></returns>
    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "/AnosAutos", ResponseFormat = WebMessageFormat.Json, 
        RequestFormat = WebMessageFormat.Json)]
    List<AnosAutos> GetAnosAutos();

    /// <summary>
    /// Returns the result of Autos Search
    /// </summary>
    /// <returns></returns>
    [OperationContract(Name="GetAutosAll")]
    [WebInvoke(Method = "POST", UriTemplate = "/Autos", ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
    List<Autos> GetAutos(string allYear, string marca, string categoria, string lowerPrice, string upperPrice);

}

以下是服务实施:

    public class COService : ICOService
{
    private COEntities context = new COEntities();

    public List<AnosAutos> GetAnosAutos()
    {
        List<AnosAutos> list = new List<AnosAutos>();
        list = ConvertFromEntityToObject(list);
        return list;
    }

    /// <summary>
    /// Convert entity WP_AnosAutos to class AnosAutos
    /// </summary>
    /// <param name="list">Contains list of type AnosAutos</param>
    /// <returns>Returns the converted list</returns>
    private List<AnosAutos> ConvertFromEntityToObject(List<AnosAutos> list)
    {
        List<WP_AnosAutos> temp = context.WP_AnosAutos.ToList();
        foreach (WP_AnosAutos wp in temp)
        {
            AnosAutos aa = new AnosAutos();
            //aa.ID = wp.ID;
            aa.AutoYear = wp.AutoYear;
            list.Add(aa);
        }
        return list;
    }

    /// <summary>
    /// Returns all Autos without being filtered by year.
    /// </summary>
    /// <param name="allYear"></param>
    /// <param name="marca"></param>
    /// <param name="categoria"></param>
    /// <param name="lowerPrice"></param>
    /// <param name="upperPrice"></param>
    /// <returns></returns>
    public List<Autos> GetAutos(string allYear, string marca, string categoria, string lowerPrice, string upperPrice)
    {
        List<Autos> list = new List<Autos>();
        list = ConvertFromEntityToObject(list, allYear, marca, categoria, lowerPrice, upperPrice);
        return list;
    }

    /// <summary>
    /// Convert entity WP_Autos to class Autos
    /// </summary>
    /// <param name="list">Contains list of type Autos</param>
    /// <returns>Returns the converted list</returns>
    private List<Autos> ConvertFromEntityToObject(List<Autos> list,
        string year, string marca, string categoria, string lowerPrice, string upperPrice)
    {
        List<WP_Autos> temp = context.GetFilteredAutos(Convert.ToInt32(year), null, null, 
            marca, categoria, Convert.ToInt32(lowerPrice), Convert.ToInt32(upperPrice)).ToList();
        foreach (WP_Autos wp in temp)
        {
            Autos a = new Autos();
            a.ID = wp.ID;
            a.SocioID = wp.SocioID;
            a.SocioLogo = wp.SocioLogo;
            a.Marca = wp.Marca;
            a.Modelo = wp.Modelo;
            a.Descripcion = wp.Descripcion;
            a.Foto = wp.Foto;
            a.Nombre = wp.Nombre;
            a.PersonaContacto = wp.PersonaContacto;
            a.Email = wp.Email;
            a.Tel = wp.Tel;
            a.Tel2 = wp.Tel2;
            a.Categoria = wp.Categoria;
            a.Municipio = wp.Municipio;
            a.Millaje = wp.Millaje;
            a.Precio = wp.Precio;
            a.Comentario_Precio = wp.Comentario_Precio;
            a.Ano = wp.Ano;
            list.Add(a);
        }
        return list;
    }

下面是Web.Config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="COWebService">
        <endpoint name="AnosAutos" address="/AnosAutos" binding="webHttpBinding" contract="COWebService.ICOService.GetAnosAutos" behaviorConfiguration="JsonBehavior">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint name="AutosAll" address="/Autos" binding="webHttpBinding" contract="COWebService.ICOService.GetAutosAll" behaviorConfiguration="JsonBehavior">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
      </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>
      <endpointBehaviors>
        <behavior name="JsonBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <connectionStrings>
        <remove name="LocalSqlServer" />
    <add name="COEntities" connectionString="metadata=res://*/COModel.csdl|res://*/COModel.ssdl|res://*/COModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=DataSourceHere;Initial Catalog=DBNameHere;User ID=IDHere;Password=PassHere;Application Name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

数据库托管:Azure但很快就在我回到家的地方很快就会进行迁移。 当前问题:无法使用我创建的WebService从Internet下载信息。 遇到问题:返回HTTP 405:不允许使用方法。有时取决于我所在的位置端点返回与实体框架相关的错误。 更多信息:尝试使用.Net Framework 4托管服务 Lucky Shots:运行一个端点(AnosAutos)但其他返回的方法不允许HTTP 405。 我已经有三个星期了,决定将它发布在stackoverflow中。如果您需要更多信息,请说出来。

1 个答案:

答案 0 :(得分:-1)

你应该用HTTP POST调用GetAutos()