【发布时间】:2018-07-02 13:46:11
【问题描述】:
我是 WCF 领域的新手。我想在服务器上制作屏幕截图并使用 WCF nettcpbinding 将它们发送到客户端。在客户端上,我想用这些数据更新 UI。但我不知道我需要什么以及如何做到这一点。我读到了带有回调的全双工合同,但我不知道它是否真的需要。
WPFClient.cs
namespace WPFClient
{
[ServiceContract]
interface IService
{
[OperationContract]
byte[] GetData();
}
public class ViewModel : INotifyPropertyChanged
{
private string bytesSum;
public string BytesSum
{
get { return bytesSum; }
set { bytesSum = value; this.NotifyPropertyChanged("BytesSum"); }
}
public ViewModel()
{
ChannelFactory<IService> channel = new ChannelFactory<IService>(new NetTcpBinding(), new EndpointAddress(@"net.tcp://localhost:8554/"));
IService s = channel.CreateChannel();
//How to get data from server and update UI?
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Server.cs
namespace Server
{
[ServiceContract]
interface IService
{
[OperationContract]
byte[] GetData();
}
public class Service : IService
{
public byte[] GetData()
{
byte[] result = new byte[5000];
return result;
}
}
public class ScreenLogger
{
public byte[] GenerateImage()
{
byte[] result = new byte[5000];
Random rnd = new Random();
for (int i = 0; i < result.Length; i++)
{
result[i] = (byte)rnd.Next();
}
return result;
}
public void Start()
{
var imageTask = Task.Factory.StartNew(() =>
{
byte[] GeneratedBytes = GenerateImage();
//How to send GeneratedBytes to client?
});
}
public void Stop()
{
}
}
class Program
{
static void Main(string[] args)
{
ScreenLogger screenLogger = new ScreenLogger();
screenLogger.Start();
ServiceHost host = new ServiceHost(typeof(Service));
host.AddServiceEndpoint(typeof(IService), new NetTcpBinding(), new Uri(@"net.tcp://localhost:8554/"));
Console.WriteLine("Server start");
host.Open();
Console.ReadLine();
host.Close();
}
}
}
【问题讨论】: