我已经开发了一个WCF webservice,目前只有一种方法,该方法应该从我的postgresql数据库中获取所有“Electeur”数据。我应该在之后调用这个web服务,在我的jQuery移动应用程序中使用它并将数据显示在数据表中。 http://mikesknowledgebase.azurewebsites.net/pages/Services/WebServices-Page1.htmWCF webservice- GetElecteurs方法返回空白页
当我建立web服务似乎一切都很好,但是当我通过导航到浏览器中查看它:http://localhost:20913/Service1.svc/GetElecteurs,我得到一个空白页,我不知道什么是同时开发的web服务我试着按照本教程错误或者我错过了什么? 我不知道我应该如何在我的jQuery移动客户端应用程序中将这些提取的数据显示为数据表。任何帮助深表感谢!提前致谢。
PS:我使用PostgreSQL,这就是为什么我不能在我的代码使用LINQ,而不是我在我的文件夹中的bin文件夹复制Npgsql.dll并添加它作为一个参考,然后我用命令Npgsql的连接到我的数据库并获取数据..
这里是我的代码:
Service1.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Npgsql;
using System.Data;
using System.Configuration;
namespace WcfService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
// Add more operations here and mark them with [OperationContract]
// The connexion
NpgsqlConnection cnx = new NpgsqlConnection("Server=localhost;User Id=postgres;Password=*****;Database=electionscasa;");
public List<Electeurs> GetElecteurs()
{
List<Electeurs> ElecteursInscrits = new List<Electeurs>();
{
//Openning the connexion string
cnx.Open();
//Connecting to the database and fetching data : List of all the voters
NpgsqlCommand cmd = new NpgsqlCommand("SELECT cinelecteur, nomelecteur, prenomelecteur, adresseelecteur, professionelecteur FROM electeur", cnx);
cmd.ExecuteNonQuery();
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
DataTable dt = new DataTable();
//Populating the datatable
da.Fill(dt);
cnx.Close();
}
return ElecteursInscrits;
}
}
}
IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetElecteurs")]
List<Electeurs> GetElecteurs();
}
[DataContract]
public class Electeurs
{
string IdElecteur;
string CinElecteur;
string NomElecteur;
string PrenomElecteur;
string AdresseElecteur;
string ProfessionElecteur;
[DataMember]
public string IdElect
{
get { return IdElecteur; }
set { IdElecteur = value; }
}
[DataMember]
public string CinElect
{
get { return CinElecteur; }
set { CinElecteur = value; }
}
[DataMember]
public string NomElect
{
get { return NomElecteur; }
set { NomElecteur = value; }
}
[DataMember]
public string PrenomElect
{
get { return PrenomElecteur; }
set { PrenomElecteur = value; }
}
[DataMember]
public string AdresseElect
{
get { return AdresseElecteur; }
set { AdresseElecteur = value; }
}
[DataMember]
public string ProfessionElect
{
get { return ProfessionElecteur; }
set { ProfessionElecteur = value; }
}
}
}
的web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<!-- We need to add the following <services> tag inside the <serviceModel> -->
<services>
<service name="JSONWebService.Service1">
<endpoint address="../Service1.svc"
binding="webHttpBinding"
contract="JSONWebService.IService1"
behaviorConfiguration="webBehaviour" />
</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="false"/>
</behavior>
</serviceBehaviors>
<!-- //In the <behaviors> tag, we need to add the following <endpointBehaviors> tag-->
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<!-- If we're going to be hosting the web services on a particular domain, but accessing the services from some JavaScript on a different domain,
then we need to make a further change to the web.config file by adding these following lines
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
</customHeaders>
</httpProtocol>
-->
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
试图离开地址空白您的端点这样的地址=“” – Coder1409
@ Coder1409感谢乌拉圭回合的答复。我试过你的建议,但仍然得到相同的空白页:(!有没有错的方法或许? – GeoSal
是否有替代LINQ for postgresql? – GeoSal