Recently, in your project, you have encountered an example of a real-time situation where you need to remotely monitor the software running automatically. Because MS has WCF for service-oriented aspects, and read some information, WCF is more suitable for this application. Therefore, we decided to use WCF to implement this function.
First of all, the specific application, monitoring, as the name suggests, there are two aspects of the meaning, on the one hand is "supervision", that is, remote to be able to real-time view of the terminal situation. Here actually refers to be monitored to be able to active, real-time to the remote control to send their own situation. The other is control, that is, the remote side can release command control terminal to execute. And a certain execution information is returned by the terminal.
And here is a one-to-one relationship, where a terminal can be monitored by multiple management terminals. In fact, a management end can also monitor multiple terminals, in this analysis, we can understand that the real operation of the service is the terminal, rather than the management side.
For simplicity, assume that the operation of the remote Send command is void Operation (), and that the terminal update is UpdateStatus ();
This idea, from design to implementation, undergoes the following three stages of deformation.
The original idea was to be a service class. An interface. That
That is, the control-side calls the operation to send the command, the terminal calls the UpdateStatus update state.
Corresponding, must have a certain callback, namely the control end sends the operation, the terminal must have a callback to receive, corresponding, when the terminal uses UpdateStatus, the control end also must have a callback to receive.
When both of these operations are integrated into a service, our WCF interface becomes the following structure.
namespace GEYUNFEI.WCF
{
[ServiceContract (
SessionMode = sessionmode.required
,
callbackcontract = typeof (Isevicecallback)
)]
public Interface IService
{
[OperationContract]
///<summary>
///Remote Send control command
///</summary>
///
void Operation ();
[OperationContract]
///<summary>
///Update Status
///</summary>
void UpdateStatus ();
}
public Interface Isevicecallback
{
void Receivestatus ();
void Receivecommand ();
}
public static class Global
{
public static list<isevicecallback> callbacks = new list<isevicecallback> ();
}
[ServiceBehavior]
public class Service:iservice,idisposable
{
Isevicecallback CallBack;
#region IService Members
public Service ()
{
callBack = system.servicemodel.operationcontext.current.getcallbackchannel<isevicecallback> ();
Global.callBacks.Add (CallBack);
}
///<summary>
///When the server calls this, send a command to the terminal
///</summary>
public void Operation ()
{
foreach (Var r in Global.callbacks)
{
R.receivecommand ();
}
}
<summary>
///when the terminal calls, send a command to the server
///</summary>
public void UpdateStatus ()
{
foreach (Var r in Global.callbacks)
{
R.receivestatus ();
}
}
#endregion
#region IDisposable Members
public void Dispose ()
{
Global.callBacks.Remove (This.callback);
}
#endregion
}
}