最近我一直在尝试在 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="Data Source=DataSourceHere;Initial Catalog=DBNameHere;User ID=IDHere;Password=PassHere;Application Name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
数据库托管:Azure,但我一回到家就在本地进行迁移。当前问题:无法使用我创建的 WebService 从 Internet 下载信息。遇到的问题:返回 HTTP 405:不允许的方法。有时取决于我所在的端点返回与实体框架相关的错误。更多信息:尝试使用 .Net Framework 4 托管服务 幸运镜头:有一个端点正在运行(AnosAutos),但其他返回的方法不允许 HTTP 405。我已经进行了三周,决定将其发布在 stackoverflow 中。如果您需要更多信息,请说出来。