我正在听我的工作人员,并将其托管在azure云服务中。
当通过wcf服务进行任何数据库事务时,WorkerRole基本上是向连接的设备发送更新
wcf服务在天蓝色网站上托管。
如果通过wcf服务调用(例如方法XYZ)在数据库中进行任何更新,我怎么能触发tcplistner。
工作人员角色代码
private AutoResetEvent connectionWaitHandle = new AutoResetEvent(false);
public override void Run()
{
TcpListener listener = null;
try
{
listener = new TcpListener(
RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["LiveUpdateEP"].IPEndpoint);
listener.ExclusiveAddressUse = false;
listener.Start();
}
catch (SocketException)
{
Trace.Write("Echo server could not start.", "Error");
return;
}
while (true)
{
IAsyncResult result = listener.BeginAcceptTcpClient(HandleAsyncConnection, listener);
connectionWaitHandle.WaitOne();
}
}
public override bool OnStart()
{
try
{
ServicePointManager.DefaultConnectionLimit = 12;
return base.OnStart();
}
catch (Exception)
{
return true;
}
// Set the maximum number of concurrent connections
}
private void HandleAsyncConnection(IAsyncResult result)
{
// Accept connection
TcpListener listener = (TcpListener)result.AsyncState;
TcpClient client = listener.EndAcceptTcpClient(result);
connectionWaitHandle.Set();
// Accepted connection
Guid clientId = Guid.NewGuid();
string Description = "Updated Items list :: ";
// Setup reader/writer
var data = new byte[client.ReceiveBufferSize];
StringBuilder dataString = new StringBuilder();
StreamWriter writer;
NetworkStream netStream = client.GetStream();
writer = new StreamWriter(netStream);
int readCount;
readCount = netStream.Read(data, 0, client.ReceiveBufferSize);
dataString.Append(Encoding.UTF8.GetString(data, 0, readCount));
try
{
JsonSerializer serializer = new JsonSerializer();
Updates objUpdate = JsonConvert.DeserializeObject<Updates>(dataString.ToString());
dataString.Clear();
dataString.Append(" Database WR - " + objUpdate.DBName);
dataString.Append(" DeviceId WR - " + objUpdate.DeviceId);
dataString.Append(" Date WR- " + objUpdate.Lud);
}
catch (Exception ex)
{
dataString.Append(ex.Message);
}
EventWaitHandle dbTriggerEvent = new EventWaitHandle(false, EventResetMode.ManualReset,
"DB_Update_Trigger_Event");
while (client.Connected)
{
Thread.Sleep(1000);
try
{
Description = DateTime.Now.ToString();
writer.AutoFlush = true;
string dataToWrite = Description + "---" + dataString;
writer.WriteLine(dataToWrite);
dbTriggerEvent.WaitOne();
// EventWaitHandle dbTriggerEvent1 = new EventWaitHandle(false, EventResetMode.ManualReset,
// "DB_Update_Trigger_Event");
// dbTriggerEvent1.Set();
}
catch(Exception ex)
{
writer.WriteLine(ex.Message);
}
}
// Done!
client.Close();
}
Wcf服务方法:
public WSResponseObject TCP_Test(string Item_Name)
{
WSResponseObject response = new WSResponseObject();
try
{
Run();
}
catch (Exception ex)
{
response.IsError = -1;
response.Data = ex.Message;
}
response.Data = "DONE";
return response;
}
public void Run()
{
TcpListener listener = null;
AutoResetEvent connectionWaitHandle = new AutoResetEvent(false);
Thread.Sleep(1000);
try
{
IPAddress ipadd = IPAddress.Parse("xxx.xx.xx.xxx");
int port = 10950;
IPEndPoint ip = new IPEndPoint(ipadd, port);
TcpClient client = new TcpClient();
client.Connect(ip);
connectionWaitHandle.Set();
string msg = @"{""DeviceId"" : ""Test Device 11111"",""BranchId"" : ""11"",""DBName"" : ""Test DB2222"",""Lud"" : ""2014-01-15 05:43:43 +0000""}";
byte[] bytes = Encoding.Default.GetBytes(msg);
int length = bytes.Length;
byte[] datalength = new byte[4];
NetworkStream Streamm = client.GetStream();
StreamWriter writer = new StreamWriter(Streamm);
writer.AutoFlush = true;
writer.WriteLine(msg);
}
catch (SocketException ex)
{
throw ex;
}
}