lundi 20 avril 2015

WCF webservice- GetElecteurs method returning a blank page

I've developed a WCF webservice with only one method so far, the method is supposed to get all "Electeur" data from my postgresql database. And I'm supposed to call this webservice afterwards, consume it in my jQuery mobile app and display the data in a datatable. I tried following this tutorial while developing the webservice : http://ift.tt/1ue8L8a

When I build the webservice everything seems fine, but when I view it in the browser by navigating to : http://localhost:20913/Service1.svc/GetElecteurs, I get a blank page, I don't know what's wrong or if I had missed something? I don't know also how am I supposed to display this fetched data in my jQuery mobile client app as a datatable. Any help is much appreciated! Thanks in advance.

P.S: I'm using Postgresql that's why I couldn't use LINQ in my code, instead I've copied Npgsql.dll in the bin folder of my folder and added it as a reference, then I used the npgsql commands to connect to my database and fetch the data..

Here's my code :

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>

Aucun commentaire:

Enregistrer un commentaire