Search code examples
c#arraysjsonwcf

How do I return all of the Array's items instead of just a single item of the array using WCF


I'm making a lottery program using WCF, JSON and Windows Forms.

Basically the goal is when a user clicks either Irish lotto or Lotto draw buttons on the form, 6 unique random numbers are generated using wcf services returned and printed on each label on the form. The min value of the array is 1, the max is the json values 29 and 59 for Irish and Lotto draw respectively.

Edit*

Heres the assignment brief. I'm not sure what it means by use Json to send the data back and fourth.

  1. Look back at the last WCF Services and JSON tasks you have created as a starting point.

  2. Create a WCF services application in the same way that you did for this.

  3. Add a method to create a random number from 1 to value you will have been sent.

  4. Create a form that will allow the user to choose either The Lotto Draw or the Irish Lotto.

  5. Send the required number of balls to the services and get back a set of random numbers. Display these on the form. (Both draws have a different amount of numbers to choose from)

  6. Your Lottery form must use WCF Services and not do the random number generation directly on the form. Use JSON to send the data back and forth.

  7. Test your application.

LottoService.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.IO;

namespace WcfLottoServiceApp
{

public class LottoService : ILottoService
{

    public int GenerateLottoDrawNums()
    {
        //Create a new instance of JObject to parse the value from JSON file and use that as the max value for the array
        JObject myJsonFile = JObject.Parse(File.ReadAllText(@"C:\Users\davie\Documents\UniWork\Advanced Programing\Lab3LottoJSONProgram\WcfLottoService\WcfLottoServiceApp\Lotto.Json"));

        //set min/max value of array to 1 and the json file value which is 59;
        int min = 1;
        int max = (int)myJsonFile["LottoDraw"];

        //declare and initilize an array with 6 values
        int[] randomNums = new int[6];

        //create new instance of Random 
        Random rand = new Random();

        //i starts at 0, keep looping whilst i is less than the lengh of the array which is 6
        for (int i = 0; i < randomNums.Length; i++)
        {
            //values are initially stored in tempNum to be checked against the current values in the array
            int tempNum = rand.Next(min, max); 

            // IsDup is true, tempNum is the temporary 7th value in the array. ie. 
            //the array only stores 6 values but there is a 7th tempary number incase any of the initial 6 
            //values are the same. The 7th number will keep looping until it is unique and add to the array so there is 6 unique values. 
            while (IsDuplicate(tempNum, randomNums))
            {
                tempNum = rand.Next(7);
            }
            randomNums[i] = tempNum;



        }



        PrintTheArray(randomNums);
        return randomNums[0];

    }


    public int GenerateIrishLottoNums()
    {
        //Create a new insatnce of JObject to parse the value from JSON file and use that as the max value for the array
        JObject myJsonFile = JObject.Parse(File.ReadAllText(@"C:\Users\davie\Documents\UniWork\Advanced Programing\Lab3LottoJSONProgram\WcfLottoService\WcfLottoServiceApp\Lotto.Json"));

        //set min/max vlaue of array to 1 and the json file value whcih is 29. ;
        int min = 1;
        int max = (int)myJsonFile["IrishLotto"];



        //declare and initilize an array with 6 values
        int[] randomNums = new int[6];

        //create new instance of Random 
        Random rand = new Random();

        //i starts at 0, keep looping whilst i is less than the lengh of the array which is 6, i + 1 after each loop
        for (int i = 0; i < randomNums.Length; i++)
        {
            //values are initially stored in tempNum to be checked against the current values in the array
            int tempNum = rand.Next(min, max);

            // IsDup is true, tempNum is the temporary 7th value in the array. ie. 
            //the array only stores 6 values but there is a 7th temporary number incase any of the initial 6 
            //values are the same. The 7th number will keep looping until it is unique and add to the array so there is 6 unique values. 
            while (IsDuplicate(tempNum, randomNums))
            {
                tempNum = rand.Next(7);
            }
            randomNums[i] = tempNum;

        }

        PrintTheArray(randomNums);

        return randomNums[0];


    }

    //Print the array to console to check if the numbers are gnerating and correct.
    private void PrintTheArray(int[]randomNums)
    {
        foreach (var item in randomNums)
        {
            Console.WriteLine(item);
        }
    }

    //This method returns true or false and checks whether the items in the array are a duplicate with the tempNum if yes then IsDup is true.
    public Boolean IsDuplicate(int tempNum, int[]randomNum)
    {
        foreach (var item in randomNum)
        {
            if (item == tempNum)
            {
                return true;
            }
        }
        return false;
    }
}
}

ILottoService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfLottoServiceApp
 {

//NOTE: You can use the "Rename" command on the "Refactor" menu to change   
the interface name "ILottoService" in both code and config file together.

[ServiceContract]
public interface ILottoService
{

    [OperationContract]

    int GenerateLottoDrawNums();

    [OperationContract]

    int GenerateIrishLottoNums();



}


}

FrontEndForm

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 Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.IO;

namespace Lotto_FrontEnd_Wcf_Json
{
public partial class FrontEnd : Form
{
    LottoServiceReference.LottoServiceClient ws = null;
    public FrontEnd()
    {
        InitializeComponent();
    }

    private void FrontEnd_Load(object sender, EventArgs e)
    {
        ws = new LottoServiceReference.LottoServiceClient();
    }

    private void btnLotto_Click(object sender, EventArgs e)
    {


    }

    private void btnIrishLotto_Click_1(object sender, EventArgs e)
    {

    }
}
}

JSON File

{
"LottoDraw": 59,


"IrishLotto": 29
}

At the moment the program simply returns a random value when invoke is clicked when wcf is running. Once I can return the whole array I shall then work at printing the values to a label on the form.

The reason i'm using WCF and JSon is for an assignemnt, not by choice.

I think I'm going wrong by not passing in any parameters for the [OperationContract]?

and obviously i'm only returning a single item of the array but I can't figure out how to return it all. I just wnated to see thast it is is generating a random number each time and when I change the randomNums[0] to [1] it changes so that's good right?

And nothing is printing to console, it doens't really need to I just tried it and didn't take it out.

I think this is clear, apologies if it's not

I've only just learnt about wcf and json so my knowledge is extremely basic.

Any advice or point in the right direction is greatly appreciated, thanks in advance!


Solution

  • Easy one.

    Just change the signature of GenerateLottDrawNums to return an array:

    [OperationContract]
    int[] GenerateLottoDrawNums();
    

    And change the implementation to return the entire array, rather than the first element

    public int[] GenerateLottoDrawNums()
    {
         // (...)
        //declare and initilize an array with 6 values
        int[] randomNums = new int[6];
        // (...)
        return randomNums;
    }