我已经编写了WCF服务应用程序和类库(Logger.dll
)。我已经使用发布-订阅模式编写了代码。
在类库中,通过使用委托和事件进行发布来推送数据。它通过订阅WCF服务应用程序中的事件来获取数据。因此,在托管服务应用程序之后,我需要将数据从WCF服务推送到另一个控制台应用程序。在将WCF服务中的事件订阅到另一个控制台应用程序时,如何推送数据?任何人都知道该怎么做?
public class Data
{
public string ID { get; set; }
public string Description { get; set; }
}
public delegate void LogHandler(Data log);
public class ClsSub
{
public event LogHandler OnDataRetrieved;
public void LoadData()
{
DataTable dt = GetData();
foreach (DataRow row in dt.Rows)
{
Data logdata = new Data();
if (row["Description"].ToString().Contains("User found"))
{
logdata.ID = row["UserID"].ToString();
logdata.Description = row["Description"].ToString();
}
if (row["Description"].ToString().Contains("User not found"))
{
logdata.ID = row["UserID"].ToString();
logdata.Description = row["Description"].ToString();
}
if (OnDataRetrieved != null)
{
OnDataRetrieved(logdata);
}
}
}
private DataTable GetData()
{
var result = new DataTable();
result.Columns.Add("UserID", typeof(string));
result.Columns.Add("Description", typeof(string));
result.Rows.Add("user1", "description1");
result.Rows.Add("user2", "description2");
return result;
}
}
public class ClsMain
{
public event LogHandler OnDataRetrieved;
public void ReadData()
{
ClsSub sub = new ClsSub();
sub.OnDataRetrieved += OnDataRetrieved;
sub.LoadData();
}
}
Using Logger;
[ServiceContract(CallbackContract = typeof(IDataServiceCallBack))]
public interface IDataService
{
[OperationContract(IsOneWay = true)]
void RetrieveLogData();
}
[ServiceContract]
public interface IDataServiceCallBack
{
[OperationContract(IsOneWay = true)]
void OnLoggerData(Data log);
}
public class Service1 : IDataService
{
public IDataServiceCallBack callBack
{
get
{
return OperationContext.Current.GetCallbackChannel<IDataServiceCallBack>();
}
}
public void RetrieveLogData()
{
ClsMain main = new ClsMain();
main.OnDataRetrieved += ClsMain_OnDataRetrieved;
main.ReadData();
}
private static void ClsMain_OnDataRetrieved(Data log)
{
// I am getting the data when the publisher publishes something.
// I need to send this data to another console application.
//Console.WriteLine(log.ID + " " + log.Description);
}
}
当发布者发布某些内容并且订阅者在WCF服务中订阅事件时,我正在获取数据。可以将这些数据推送到另一个控制台应用程序吗?有人知道怎么做吗?