私は1つのデバイスをユーザーのデスクに「インストール」しています(デスクはユーザーが座る椅子やテーブルに過ぎません)。何千ものデスクをサポートします。シリアルポートへのデータが非同期になると、Webサービスを介してデータを格納する方法は?
ユーザーには1つの「チップ」があり、ユーザーはこのチップを自分のデスクにインストールされているデバイスでスキャンします。
デバイスはチップからデータを読み取り、このデバイスがインストールされているラップトップに送信しますが、このデバイスはすべてのユーザースキャンチップデータを収集する主要デバイスです。
すべてのデータはWi-Fiルーター経由で私のデバイスにルーティングされ、私はメインデバイスからこれを聞き、シリアルポート接続を介してノートパソコンからこのデバイスのデータを読み込みます。
このデータ送信は、各ユーザー番号が自分のチップをスキャンするときに発生します。
ラップトップでバックグラウンドで継続的に実行されるWindowsフォームアプリケーションを作成し、メインデバイスが接続されているシリアルポートを聞いています。
これは、ここから取った私のコードです:Source Code Reference:
public partial class MainUI : Form
{
SerialPortManager _spManager;
public MainUI()
{
InitializeComponent();
UserInitialization();
}
}
private void UserInitialization()
{
_spManager = new SerialPortManager();
_spManager.NewSerialDataRecieved += new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved);
this.FormClosing += new FormClosingEventHandler(MainUI_FormClosing);
}
private void MainUI_Load(object sender, EventArgs e)
{
_spManager.StartListening()
}
void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int dataLength = _serialPort.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = _serialPort.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return;
// Send data to whom ever interested
if (NewSerialDataRecieved != null)
{
NewSerialDataRecieved(this, new SerialDataEventArgs(data));
}
}
void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
{
if (this.InvokeRequired)
{
// Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
this.BeginInvoke(new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e });
return;
}
//data is converted to text
string str = Encoding.ASCII.GetString(e.Data);
if (!string.IsNullOrEmpty(str))
{
//Here i will store that data in to my database through web service.
//What i should use whether WCF service or Web Api because data will be continuos like at a
//time more than 10 or 100 user can scan data at the same time so this event will be fired continuously.
//I am using entity framework to store data in to my database and how to ansynchornously call web service to store my data
//so that my call doesnt block incoming data to serial port
}
}
私の主な関心事は、私が同時にデータをスキャンし、どのように私は10以上または100のユーザーを処理する多数のユーザを持つことになりますです同時にデータをスキャンします。
この潜在的な問題を緩和するにはどうすればよいですか?
私のデータはデータ受信イベントになりますので、各データはデータベースに保存しますが、データベースにデータを保存すると、受信したデータ受信イベントが発生しますagain.so私が欲しいのは、自分のデータベースの保存操作を独立させたい –