I have a console app that starts a WCF service and a small WPF app. I'm trying to show a messagebox in the WPF app whenever a method on the WCF service is called for testing purposes. I have based my code on this answer but the synchronizationContext is null. How do i fix this? Or are there other/better ways to make this happen?
Use SignalR . Lots of info here and here . This article also looks very good.
As for actual code, create a WCF service and use NuGet to add a reference to SignalR, then add a hub class:
public class ServiceMonitorHub : Hub
{
}
You'll also need to add an Owin startup class to start the SignalR hub:
[assembly: OwinStartup(typeof(YourNamespace.SignalRStartup))]
namespace YourNamespace
{
public class SignalRStartup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
Your service handlers can then get a reference to this hub and send messages to all the clients that are connected to it:
public class Service1 : IService1
{
public string GetData(int value)
{
// send msg to clients
var hub = GlobalHost.ConnectionManager.GetHubContext<ServiceMonitorHub>();
hub.Clients.All.BroadcastMessage();
return string.Format("You entered: {0}", value);
}
Your WPF client then connects to this SignalR server and hooks in handlers to receive the messages send by the service handlers, this example contains a button handler that calls the service, as well as a connection to the SignalR hub to receive the messages that bounce back:
public partial class MainWindow : Window
{
private HubConnection Connection;
private IHubProxy HubProxy;
public MainWindow()
{
InitializeComponent();
Task.Run(ConnectAsync);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
using (var service = new ServiceReference1.Service1Client())
service.GetData(1);
}
public async Task ConnectAsync()
{
try
{
this.Connection = new HubConnection("http://localhost:59082/");
this.HubProxy = this.Connection.CreateHubProxy("ServiceMonitorHub");
HubProxy.On("BroadcastMessage", () => MessageBox.Show("Received message!"));
await this.Connection.Start();
}
catch (Exception ex)
{
}
}
}
}
Note that clients need the NuGet SignalR.Clients package (as opposed to just SignalR).
There are other ways for services to call the hub clients, this link shows a few others.
Maybe you could try ClientMessageInspector, the AfterReceiveReply of it will be called every time the client receives the message.
public class MyClientMessageInspector : IClientMessageInspector
{
Your message box
public void AfterReceiveReply(ref Message reply, object correlationState)
{
show message in your message box
}
public MyClientMessageInspector ( ){
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
return null;
}
}
To register the Inspector , you need to use an endpointBehavior
public class MyEndpointBehavior : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add( new MyClientMessageInspector();
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
And then add the behavior to your client.
using (ChannelFactory<IService> ChannelFactory = new ChannelFactory<IService>("myservice"))
{
ChannelFactory.Endpoint.EndpointBehaviors.Add(new MyEndpointBehavior());
IService service = ChannelFactory.CreateChannel();
// call method
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.