我在一个问题上停留了 2 周。
假设我们有一个 Duplex WCF 服务和一个调用该服务的客户端应用程序。
该应用程序有一个由服务组成的类 MyCallBackClass。我想要实现的是在 MyCallBackClass 客户端应用程序的构造函数中传递实例化的服务(失去耦合)。所以它看起来像一个方法的服务和一个方法的回调:
双重服务合同:
[ServiceContract(SessionMode=SessionMode.Required,CallbackContract=typeof(ICallback))]
public interface IService{
[OperationContract(IsOneWay = true)]
void GetDataFromService();
}
双工回调:
public interface ICallback{
[OperationContract(IsOneWay = true)]
void ReceiveMessage(string message);
}
双工服务实施
public class Service : IService{
//... here a reference to the Callback endpoint
void GetDataFromService(){
callBackEndPoint.ReceiveMessage("Service was called.");
}
}
我实现回调的类:
public class MyCallBackClass : ICallback, Widnows.Form
{
IService service;
public MyCallBackClass (){
InstanceContext instanceContext = new InstanceContext(this);
this.service = new ServiceClient(instanceContext);
}
public ReceiveMessage(string message){
this.textBoxMessage.Text = message;
//here I want to stress that I would like my CallBack object to be a Form or WPF Form
//so that I can react on callbacks by modyfing the Controls like TextBox, ListBox directly
}
}
现在在应用程序中,我被迫在实现回调接口的对象的构造函数中实例化服务,假设它是一个表单或 WPF 表单(如下所示):
public void Main(string[] args){
MyCallBackClass myWindow = new MyCallBackClass();
myWindow.GetDataFromService();
}
我想要的是在回调处理程序的构造函数中传递服务,如下所示:
public void Main(string[] args){
Iservice service = new ServiceClient();// but what about the InstanceContext that is the MyCallBackClass object...???
MyCallBackClass myWindow = new MyCallBackClass(service);
myWindow.GetDataFromService();
}
当然,该类的 MyCallBackClass 构造函数会更改为:
public class MyCallBackClass : ICallback, Widnows.Form
{
IService service;
public MyCallBackClass (IService _service){
InstanceContext instanceContext = new InstanceContext(this);
this.service = _service;
...
}
这样我就可以向客户端类注入任何类型的实现 IService 接口的服务,并且通过模拟服务很容易测试客户端类。不幸的是,我遇到了一个依赖循环。InstanceContext 依赖 MyCallBackClass 依赖 IService 依赖 InstanceContext ...
您能否尝试理解并尝试指导我解决此问题的任何方向?