WCF Web服务 - 获取Electeurs方法返回空白页面

时间:2015-04-20 10:13:45

标签: jquery web-services wcf postgresql datatable

到目前为止,我已经开发了一个只有一种方法的WCF Web服务,该方法应该从我的postgresql数据库中获取所有“Electeur”数据。我之后应该调用这个web服务,在我的jQuery移动应用程序中使用它并在数据表中显示数据。我在开发webservice时试图学习本教程:http://mikesknowledgebase.azurewebsites.net/pages/Services/WebServices-Page1.htm

当我构建web服务时,一切似乎都很好,但是当我在浏览器中通过导航到http://localhost:20913/Service1.svc/GetElecteurs查看它时,我得到一个空白页面,我不知道什么是错的或者我错过了什么? 我不知道我应该如何在我的jQuery移动客户端应用程序中显示这个获取的数据作为数据表。任何帮助深表感谢!提前谢谢。

  

PS: 我正在使用Postgresql,这就是为什么我无法在我的代码中使用LINQ,而是将Npgsql.dll复制到我的bin文件夹中文件夹并添加它作为参考,然后我使用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>

1 个答案:

答案 0 :(得分:1)

我设法通过编辑web.config文件来解决问题(服务名称不正确,特别是命名空间,所以我更改了它)并且我还对我的GetElecteurs方法添加了一些更改。

以下是代码:

[WebMethod]
    public List<Electeurs> GetElecteurs()
    {
        List<Electeurs> ElecteursInscrits = new List<Electeurs>();
        // The connexion
        NpgsqlConnection cnx = new NpgsqlConnection("Server=localhost; Port=5432; User Id=postgres; Password=****; Database=database_name;");

            DataTable dt = new DataTable();
            Npgsql.NpgsqlDataReader rdr = null;

            //Connecting to the database and fetching data 
            NpgsqlCommand cmd = new NpgsqlCommand("SELECT cinelecteur, nomelecteur, prenomelecteur, adresseelecteur, professionelecteur FROM electeur", cnx);
            cnx.Open();
            rdr = cmd.ExecuteReader();
            dt.Load(rdr);
            cnx.Close();

            int countRow = dt.Rows.Count;
            foreach (DataRow drElect in dt.Rows)
            {
                Electeurs electeurinscrit = new Electeurs();
                //electeurinscrit.IdElect = Convert.ToInt32(drElect["objectid"].ToString());
                electeurinscrit.CinElect = drElect["cinelecteur"].ToString();
                electeurinscrit.NomElect = drElect["nomelecteur"].ToString();
                electeurinscrit.PrenomElect = drElect["prenomelecteur"].ToString();
                electeurinscrit.AdresseElect = drElect["adresseelecteur"].ToString();
                electeurinscrit.ProfessionElect = drElect["professionelecteur"].ToString();
                ElecteursInscrits.Add(electeurinscrit);
            }
            return ElecteursInscrits;          
    }
相关问题