我正在使用WCF,JSON和Windows窗体制作彩票程序。
基本上,目标是当用户单击表单上的爱尔兰乐透或乐透抽奖按钮时,使用返回并打印在表单上每个标签上的wcf服务生成6个唯一的随机数。数组的最小值是1,最大值分别是爱尔兰和乐透开奖的json值29和59。
编辑*
这里是作业简介。我不确定使用Json来回发送数据是什么意思。
回顾一下您作为起点创建的最后一个WCF服务和JSON任务。
以与执行此操作相同的方式创建WCF服务应用程序。
添加一种方法来创建一个从1到将要发送的值的随机数。
创建一个允许用户选择“乐透抽奖”或“爱尔兰乐透”的表格。
将所需数量的球发送到服务并获取一组随机数。在表单上显示这些。 (两次抽奖都有不同数量的数字可供选择)
您的彩票表单必须使用WCF服务,并且不能直接在表单上进行随机数生成。使用JSON来回发送数据。
测试您的应用程序。
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文件
{
"LottoDraw": 59,
"IrishLotto": 29
}
目前,当运行wcf时,单击invoke时,程序仅返回一个随机值。一旦我可以返回整个数组,就可以将值打印到表格上的标签上。
我使用WCF和JSon的原因是为了分配,而不是选择。
我认为我没有为[OperationContract]输入任何参数是错的吗?
显然,我只返回数组的单个项目,但我不知道如何返回全部。我只是想知道,它每次都会生成一个随机数,当我将randomNums [0]更改为[1]时,它也会更改,所以这很好吗?
什么都没有打印到控制台上,它并不需要我只是尝试了一下并且没有将它取出来。
我认为这很清楚,如果不是的话,我们深表歉意
我只是了解wcf和json,所以我的知识非常基础。
在此先感谢您的任何建议或朝正确方向的指示!
答案 0 :(得分:1)
简单一个。
只需更改GenerateLottDrawNums
的签名即可返回数组:
[OperationContract]
int[] GenerateLottoDrawNums();
并更改实现以返回整个数组,而不是第一个元素
public int[] GenerateLottoDrawNums()
{
// (...)
//declare and initilize an array with 6 values
int[] randomNums = new int[6];
// (...)
return randomNums;
}