Hands-on: Menning av einum klienti til FOLK í C#

Í hesi vegleiðingini fara vit at menna ein "klient" til FOLK webservice'ina.

Áðrenn tú byrjar er neyðugt at tú kennir URL til FOLK webservice'ina á tínum security servara, og at maskinan sum tú mennir á, hevur samband við security servarin. 

Tendra ein kaga/browsara, og far inn á http://[tín.sec.serv.ip]/cgi-bin/uriproxy?producer=elktest. Kanna um henda síða svarar, og at síðan ið kemur fram líkist myndini niðanfyri.

Eisini skulu vit nýta ConsumerProxy URL, sum verður tann adressan sum okkara klient-forrit skal samskifta við FOLK umvegis. Adressan er: http://[tín.sec.serv.ip]/cgi-bin/consumer_proxy og síðan eigur at síggja nakað soleiðis út, um vit kalla á hana í einum kaga/browsara:

1: Lat upp Visual Studio, og far inn á File à New project

2: Vel Visual C# à Windows Desktop à Windows Forms Application. Skriva eitt navn í Name teigin, og vel hvar prosjekti skal goymast. Trýst síðani á OK.

3: Set properties fyri formin:

  • Width: 800
  • Height: 500
  • Text: Fyrispyr uppá FOLK webservice hjá Umhvørvisstovuni

4a: Legg hesi controls inn á formin:

  • Label (System.Windows.Forms.Label)
    • Name: lblStatus
    • Text: Er ikki loggaður á
    • BackColor: Red
    • Location: x=12,y=8
  • Button (System.Windows.Forms.Button)
    • Name: btnLogin
    • Text: Innrita til webservice
    • Location: x=15, y=24
    • Size: W:159, H:23
  • ListView (System.Windows.Forms.ListView)
    • Name: lvwPersons
    • Anchor: Top, Left, Bottom, Right
    • FullRowSelect: True
    • GridLines: True
    • View: Details
    • Location: x=192, y=24
    • Size: W:580, H:398
    • Columns:
      • Name: colPublicID, Text: PublicID, Width: 80
      • Name: colName, Text: Navn, Width: 180
      • Name: colAddress, Text: Bústaður, Width: 250
  • TextBox (System.Windows.Forms.TextBox)
    • Name: txtRequestSSN
    • Anchor: Bottom, Left
    • Location: x=192, y=431
    • Size: W:100, H:20
    • PasswordChar: *
  • Button  (System.Windows.Forms.Button)
    • Name: btnRequestSSN
    • Anchor: Bottom, Left
    • Text: Finn persón
    • Location: x=298, y=429
    • Size: W:75, H:23
  • ToolTip (System.Windows.Forms.TootlTip)
    • Name: myToolTip

Hesi niðanfyri skulu eisini inn á formin, fyri at fáa stig 13 at virka, men tú kanst eisini gera stig 5 til 12 fyrst, og síðani koma aftur til 4.b.

4.b: Legg hesi controls inn á formin:

  • Label (System.Windows.Forms.Label)
    • Text: Finn broytingar (frá dato):
    • Location: x=12, y=72
  • DateTimePicker (System.Windows.Forms.DateTimePicker)
    • Name: dtFromDate
    • Location: x=15, y=88
    • Size: W:159, H:20
  • Label (System.Windows.Forms.Label)
    • Text: Finn broytingar (til dato)
    • Location: x=12, y=120
  • DateTimePicker (System.Windows.Forms.DateTimePicker)
    • Name: dtToDate
    • Location: x=15, y=136
    • Size: W:159, H:20
  • Button (System.Windows.Forms.Button)
    • Name: btnGetCommunityChangeIds
    • Text: Finn broytingar
    • Location: x=15, y=162
    • Size: W:159, H:23
  • ListBox (System.Windows.Forms.ListBox)
    • Name: lstIds
    • Anchor: Top, Bottom, Left
    • Location: x=15, y=191
    • Size: W:159, H:225
  • Button  (System.Windows.Forms.Button)
    • Name: btnGetInfoForPersons
    • Anchor: Bottom, Right
    • Text: Heinta upplýsingar um Persónarnar
    • Location: x=581, y=428
    • Size: W:191, H:23

Nú sær formurin nakað soleiðis út:

5: Høgraklikka á prosjekti, og trýst á Add --> Service Reference:

6: Skriva URL til webservice wsdl fíluna ígjøgnum tín security servara (http://SecurityServerIP/cgi-bin/uriproxy?producer=elktest), trýst á “Go” og áset eitt virði í Namespace teigin, og trýst síðani á OK:

7: Trýst á CTRL+SHIFT+B, ella “Build Solution” umvegis valmyndina:

Vit hava nú generera klient-kotuna til FOLK, men vit skulu eisini definera eitt endpoint.

8: Høgra klikka á app.config fíluna, og vel Edit WCF Configuration

9: Vel ”Create a new client” í “Tasks” økinum:

9.a) Vel ”Manually”

9.b) Trýst ”Browse”

9.c) Vel bin mappuna

9.d) Vel Debug mappuna

9.e) Vel tína exe-fílu (sum bleiv skapt í build í stig 7)

9.e) Vel tína type name

9.f) Vel Next

9.g) Vel ”New binding configuration”

9.h) Vel ”HTTP"

9.i) Vel ”Basic Web Services interoperability”

9.j) Skriva adressuna á consumer proxy á tínum security servara (http://[tín.security.server.ip]/cgi-bin/consumer_proxy)

9.k) Lat navnið “(Default)” standa:

9.l) Trýst á ”Finish”

9.m) Vel ”File” à ”Save” og síðani ”File” à ”Exit”

10: Set using statement fyri webservice og ger ein public property á formin fyri login status og til at goyma login token :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FOLK_Client.FolkWS;

namespace FOLK_Client
{
    public partial class Form1 : Form
    {
        public String tempToken;
        public Boolean isLoggedOn = false;
        public Form1()
        {
            InitializeComponent();
        }
    }
}

11: Kota til knøttin ”btnLogin”

Dupultklikka á knøttin ”Innrita til webservice” og set hesa kotuna inn:

        private void btnLogin_Click(object sender, EventArgs e)
        {
            FolkWS.CrsPortTypeClient cl = new CrsPortTypeClient();
            String consumer = "343269",
                   producer = "elktest",
                   userId = "12345", id = "1",
                   service = "elktest.Login.v1";
            Login lg = new Login();
            lg.request = new loginRequest { username = "??????", password = "?????????" };
            LoginResponse rsp = cl.Login(ref consumer, ref producer, ref userId, ref id, ref service, lg);
            if (rsp.faultCode == null && rsp.faultString == null) {
                this.lblStatus.Text = "Er loggaður á";
                this.isLoggedOn = true;
                this.lblStatus.BackColor = Color.Green;
                this.btnLogin.Enabled = false;
            }
            tempToken = rsp.response.token;
        }

12: Kota til knøttin ”btnRequestSSN”

Dupultklikka á knøttin ”Finn persón”

private void btnRequestSSN_Click(object sender, EventArgs e)
        {
            if (isLoggedOn) {
                if (string.IsNullOrEmpty(this.txtRequestSSN.Text)) {
                    MessageBox.Show("Skriva fyrst eitt p-tal!");
                    return;
                }
                CrsPortTypeClient cl = new FolkWS.CrsPortTypeClient();
                string consumer = null;
                string producer = null;
                string userid = null;
                string id = null;
                string service = null;
                consumer = "343269";
                producer = "elktest";
                userid = "user 1";
                id = "123456789";
                service = "elktest.GetPeoplePublicInfo.v1";

                //Set token in headar
                FolkWS.publicInfoRequestRequestHeader hdr = new FolkWS.publicInfoRequestRequestHeader();
                hdr.token = tempToken;
                publicInfoRequestRequestBody bdy = new publicInfoRequestRequestBody();
                List<PersonPublicInfoCriteria> lst = new List<PersonPublicInfoCriteria>();
                PersonPublicInfoCriteria tmpPersSSN = new PersonPublicInfoCriteria();
                tmpPersSSN.SSN = this.txtRequestSSN.Text;
                lst.Add(tmpPersSSN);
                bdy.ListOfPersonPublicInfoCriteria = lst.ToArray();

                //set header & body into the request
                publicInfoRequest req = new publicInfoRequest();
                req.requestHeader = hdr;
                req.requestBody = bdy;

                //Set request property of the getCommunityPeopleIds
                GetPeoplePublicInfo ppid = new GetPeoplePublicInfo();
                ppid.request = req;
                GetPeoplePublicInfoResponse rsp = cl.GetPeoplePublicInfo(ref consumer, ref producer, ref userid, ref id, ref service, ppid);
                this.lvwPersons.Items.Clear();
                if (object.ReferenceEquals(rsp.faultCode, "") & string.IsNullOrEmpty(rsp.faultString)) {
                    foreach (PersonPublicInfo itm in rsp.response.ListOfPersonPublicInfo) {
                        if (string.IsNullOrEmpty(itm.ErrorMessage)) {
                            string nms = "";
                            foreach (Name nm in itm.Names) {
                                if (nm.Type == NameType.OfficialName) {
                                    nms = nm.Value;
                                }
                            }

                            ListViewItem lvi = new ListViewItem();
                            lvi.Text = Convert.ToString( itm.PublicId);
                            lvi.SubItems.Add(nms);
                            ListViewItem.ListViewSubItem lvisi = new ListViewItem.ListViewSubItem();
                            try {lvisi.Text += itm.Address.Street; } catch (Exception ex) {}
                            try {lvisi.Text += " " + itm.Address.BuildingNumber; } catch (Exception ex) {}
                            try {lvisi.Text += ", " + itm.Address.PostalCode; } catch (Exception ex) {}
                            try {lvisi.Text += " " + itm.Address.City; } catch (Exception ex) {}
                            try {lvisi.Text += " " + itm.Address.Country.Name; } catch (Exception ex) {}
                            lvi.SubItems.Add(lvisi);
                            this.lvwPersons.Items.Add(lvi);
                        } else {
                            ListViewItem errItm = new ListViewItem(itm.ErrorMessage);
                            errItm.SubItems.Add(itm.ErrorMessage);
                            errItm.BackColor = Color.Red;
                            this.lvwPersons.Items.Add(errItm);
                        }
                    }
                }
            } else {
                myToolTip.Show("Tú mást fyrst logga á FOLK webservice!", btnLogin, 2000);
            }
        }

Tú kann nú testa klientin við at fara til stig: 15. 
Fyri at halda áfram við stig 13, er neyðugt at gera stig 4.b fyrst

13: Kota til knøttin ”btnGetCommunityChangeIds”

Dupultklikka á knøttin ”Finn broytingar” og set hesa kotuna inn:

    private void btnGetCommunityChangeIds_Click(object sender, EventArgs e)
    {
        if (isLoggedOn) {
            CrsPortTypeClient cl = new FolkWS.CrsPortTypeClient();
            string consumer = null;
            string producer = null;
            string userid = null;
            string id = null;
            string service = null;
            consumer = "343269";
            producer = "elktest";
            userid = "user 1";
            id = "123456789";
            service = "elktest.GetCommunityPeopleIds.v1";

            //Set token in headar
            communityPeopleIdsRequestRequestHeader hdr = new communityPeopleIdsRequestRequestHeader();
            hdr.token = tempToken;

            //Set dates í body
            communityPeopleIdsRequestRequestBody bdy = new communityPeopleIdsRequestRequestBody();
            bdy.ModifiedSince = new DateTime(dtFromDate.Value.Year, dtFromDate.Value.Month, dtFromDate.Value.Day, 0,0,0);
            bdy.ModifiedSinceSpecified = true;
            bdy.ModifiedBefore = new DateTime(dtToDate.Value.Year, dtToDate.Value.Month, dtToDate.Value.Day, 0, 0, 0);
            bdy.ModifiedBeforeSpecified = true;

            //set header & body into the request
            dynamic req = new communityPeopleIdsRequest();
            req.requestHeader = hdr;
            req.requestBody = bdy;

            //Set request property of the getCommunityPeopleIds
            dynamic ppid = new GetCommunityPeopleIds();
            ppid.request = req;
            GetCommunityPeopleIdsResponse rsp = cl.GetCommunityPeopleIds(ref consumer, ref producer, ref userid, ref id, ref service, ppid);
            if (object.ReferenceEquals(rsp.faultCode, "") & string.IsNullOrEmpty(rsp.faultString)) {
                foreach (PersonIds itm in rsp.response.PeopleIds) {
                    this.lstIds.Items.Add(itm.PublicId);
                }
            }
        } else {
            myToolTip.Show("Tú mást fyrst logga á FOLK webservice!", btnLogin, 2000);
        }
    }

14: Kota til knøttin ”btnGetInfoForPersons”:

Dupultklikka á knøttin “Heinta upplýsingar um Persónarnar”:

    private void btnGetInfoForPersons_Click(object sender, EventArgs e)
    {
        if (isLoggedOn) {
            CrsPortTypeClient cl = new FolkWS.CrsPortTypeClient();
            string consumer = null;
            string producer = null;
            string userid = null;
            string id = null;
            string service = null;
            consumer = "343269";
            producer = "elktest";
            userid = "user 1";
            id = "123456789";
            service = "elktest.GetPeoplePublicInfo.v1";

            //Set token in headar
            FolkWS.publicInfoRequestRequestHeader hdr = new FolkWS.publicInfoRequestRequestHeader();
            hdr.token = tempToken;
            publicInfoRequestRequestBody bdy = new publicInfoRequestRequestBody();
            List<PersonPublicInfoCriteria> lst = new List<PersonPublicInfoCriteria>();
            foreach (int persid in this.lstIds.Items) {
                PersonPublicInfoCriteria tmpPerID = new PersonPublicInfoCriteria();
                tmpPerID.PublicId = persid;
                tmpPerID.PublicIdSpecified = true;
                lst.Add(tmpPerID);
            }
            bdy.ListOfPersonPublicInfoCriteria = lst.ToArray();
            //set header & body into the request
            publicInfoRequest req = new publicInfoRequest();
            req.requestHeader = hdr;
            req.requestBody = bdy;
            //Set request property of the getCommunityPeopleIds
            GetPeoplePublicInfo ppid = new GetPeoplePublicInfo();
            ppid.request = req;
            GetPeoplePublicInfoResponse rsp = cl.GetPeoplePublicInfo(ref consumer, ref producer, ref userid, ref id, ref service, ppid);
            this.lvwPersons.Items.Clear();
            if (object.ReferenceEquals(rsp.faultCode, "") & string.IsNullOrEmpty(rsp.faultString)) {
                foreach (PersonPublicInfo itm in rsp.response.ListOfPersonPublicInfo) {
                    if (string.IsNullOrEmpty(itm.ErrorMessage)) {
                        string nms = "";
                        foreach (Name nm in itm.Names) {
                            if (nm.Type == NameType.OfficialName) {
                                nms = nm.Value;
                            }
                        }
                        ListViewItem lvi = new ListViewItem();
                        lvi.Text = System.Convert.ToString(itm.PublicId);
                        lvi.SubItems.Add(nms);
                        ListViewItem.ListViewSubItem lvisi = new ListViewItem.ListViewSubItem();
                        try { lvisi.Text += itm.Address.Street; } catch (Exception ex) { }
                        try { lvisi.Text += " " + itm.Address.BuildingNumber; } catch (Exception ex) {}
                        try { lvisi.Text += ", " + itm.Address.PostalCode; } catch (Exception ex) {}
                        try { lvisi.Text += " " + itm.Address.City; } catch (Exception ex) {}
                        try { lvisi.Text += " " + itm.Address.Country.Name; } catch (Exception ex) {}
                        lvi.SubItems.Add(lvisi);
                        this.lvwPersons.Items.Add(lvi);
                    } else {
                        ListViewItem errItm = new ListViewItem(itm.ErrorMessage);
                        errItm.SubItems.Add(itm.ErrorMessage);
                        errItm.BackColor = Color.Red;
                        this.lvwPersons.Items.Add(errItm);
                    }
                }
            }
        } else {
            myToolTip.Show("Tú mást fyrst logga á FOLK webservice!", btnLogin, 2000);
        }
    }

 

Nú er klárt at tendra klientin og testa soleiðis:

15: Trýst á Run í Visual Studio:

16: Nú tendrar henda myndin niðanfyri:

Soleiðis nýta vit myndina:

1: Áðrenn knøttarnir virka, er neyðugt at logga á webservice'ina við fyrsta knøttinum.

2: Í dato-dropdown kunnu vit velja at vit vilja síggja broytingar í FOLK frá og til dato.

3: "Finn broytingar" biður FOLK geva okkum tær broytingar sum eru hendar millum frá og til dato.

4: Tá vit hava heinta listan yvir broytingar við knøtti 3, so kunnu vit eftirfylgjandi heinta upplýsingar, so sum nøvn, bústað, osfr., fyri teir broyttu persónarnar í listanum.

5: Eisini kunnu vit skriva eitt p-tal í tekstboksina og leita eftir tí persóninum við knøtti 5.

Reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
reply to topic
Like Follow
  • 2 yrs agoLast active
  • 283Views
  • 1 Following

Virksemi á síðuni