Notification fromClient1 to Client2 through WCF - c#

I'm new at WCF and I've got one problem, and I don't have an idea how to solve it.
I've got Windows Service app listening to COM ports, and GUI WPF app for configuring Windows Service app. Among that my idea is to send notification to WPF app every time when something comes to COM port and maybe send that data to WPF.
For communicaton between those two clients I use WCF, and for sending data from WCF service to WPF app I got to use callbacks.
I got to use two different ServiceContracts because of two different clients. One of ServiceContacts supports callback method, and one of them not so there are two different bindings.
Is there any way to invoke callback method implemented on WPF app when WS gets data from COM port. On WS I invoke proxy method defined in ServiceContract for WS, and get data in WCF service but when I invoke callback its always null. I know that there is problem with OperationContext and I know what problem is but I dont know how to solve it, or is there any way to solve it.
Please help me and thank you.

I think duplex WCF (i.e. contract with Callback) is one way to go if your WFP client is normally calling the "service" via WCF anyway. You will obtain the callback context in the "service" contract class (its constructor usually) and then use it to forward anything to the WPF client at any time. Google "Duplex WCF".
However this is a situation for which in enterprise scale you will use a middleware solution, i.e. a separate "message queue/bus" infrastructure to which the WPF clients subscribe and get their updates. If no one is subscribed, messages are sent to /dev/null. The "service" publishes everything to the bus at any case.

Related

WCF send info from service to client when event happens in service

I'm trying to build basic prototypes from a project that will take form a little later down the road.
For this prototype I currently have a console application hosting WCF services and another console application consuming the WCF services.
I have read of the three different message patterns for WCF and from what I understand I can either :
1)Call the WCF service and wait for the answer, locking the consumer while its waiting for the answer.
2)Call the WCF service asynchronously, which doesn't lock the consumer while its waiting for the answer. An answer event will eventually happen.
3)Call the WCF service without expecting and answer at all.
What I would like to know is if it is possible to use WCF to send a message to the client when an event is triggered on the service side. For example if a variable changes on the application hosting my services, a message would be sent to a client.
The only way I can imagine this would be to have my client also host WCF services and send a one way message from my initial host as the client.
This seems like a somewhat odd way of doing things for some reason and I would love to know if anybody has another idea on how this could be done.
Thanks a lot!
Note: Just in case anybody gets stuck on that, I am not looking for a way to trigger events when a variable changes. Also, there is no code in the question because it is mostly an architectural question. If you can point me towards the right tools to achieve what I'm looking for I'm sure i'll find plenty of code examples on how to use them!

Notify all clients when specific event happens. Where to subscribe to events?

I googled two examples on how I can notify all clients when specific event happens and I'm confused which of them I should use:
Example 1 - http://www.nullskull.com/a/1360/notify-client-applications-using-wcf-callbacks.aspx
Example 2 - http://www.anujvarma.com/wcf-publish-subscribe-a-full-example-the-service-side-part-2-implementation/
In first example event, eventargs etc are placed on client side, in second - on service side. Where should I place events?
Brief of what I'm trying to achieve: simple card game project that uses local network. I want to notify all clients when one of the clients throws a card (placed it on table and ended his turn) and update they're interfaces, so each client can see thrown card.
If you are going to use WCF, then you need to implement broadcasting events with a duplex WCF service (broadcaster service). The first link does mention this.
I think the events you are referring to are the operations that occur on the service and client:
To create a duplex contract you create a pair of interfaces. The first
is the service contract interface that describes the operations that a
client can invoke. That service contract must specify a callback
contract in the ServiceContractAttribute.CallbackContract property.
The callback contract is the interface that defines the operations
that the service can call on the client endpoint.
You may want to also consider using ASP.NET SignalR.
SignalR allows bi-directional communication between server and client.
Servers can now push content to connected clients instantly as it
becomes available

Communication from WCF Service to UWP Application

I am currently building a WCF Service to make use of some DLL's that are not UWP compatible at the moment. I've gotten my service running, included, and accessible from my UWP app but because I'm very new to WCF I'm not sure the best way to go about reporting the status of certain activities of the WCF Service. Specifically, I have a function that I call from my service that does some things, reaches a certain point, and then does some more things. I would like the notify the UWP app that this certain point has been reached. How would I go about doing this?
I've looked into callbacks but I haven't found a clear resource on them and am hoping there is a simpler way to accomplish what I'm looking for.
Edit: After doing some reading on the various forms of communication in WCF(mainly duplex and subpub) I think my solution will be more along the lines of subpub although it seems like overkill. From what I've seen I would be unable to make 2 callback calls from within one service function call and that would be ideal.
Edit: So apparently I am able to make 2 callback calls from a service function by modifying my ServiceContract to something like:
[ServiceContract(SessionMode = SessionMode.Required,
CallbackContract = typeof(IRecordServiceCallback_duplex),
CallbackContract = typeof(IRecordServiceCallback_duplex))]
So now I know that's what I'm trying to accomplish.
Update:
My problem now, I've looked through various examples and none of them seem to implement a duplex wcf service without some other complications which make understanding what's going on a bit difficult. Or they are missing the config file. Ideally I think I'd like to have a Duplex WCF Service hosted in IIS that communicates to a Windows Universal App, I'm currently hung up on how to call my duplex wcf service from within a UWP app. I've called a basic WCF Service before so I'm sure it can be done.

Control windows service using WCF

I am creating windows service, which must be able to receive commands from client located on other computer.
So far it seemed like using WCF for comunication is a good idea. However I am unable to pass any argument to my service on server side. All tutorials on WCF show only basic methods, which do not interact witch windows service.
Is there any way how to communicate with windows service? - Be able to call methods, that return some data. Using WCF it feels like I am only a step away. I can call methods on server and getting data back. I am however not able to access parameters of the service to modify them.
Basicaly what I want is to have a thread running in the windows service doing periodiccaly some actions, and to be able to remotely interact with it (it is not enough to pass only number code, I need to be able to pass at least string (preferably custom type) and get answer (string or custom type))
Thanks
If you can pass the number you can modify the service contract to use a (custom) data contract including strings and also more complex types. You need to check [DataContract] for more those complex types.
This link covers the basics: http://www.codeproject.com/Articles/653493/WCF-Hosting-with-Windows-Service
It starts with creating the service and creating an installer for the windows service.
Starting with the Fourth module there it is explained how to complete coding on the client side.
You need to expose the service metadata for service reference to complete programming the client.
If you have the metadata exchange, you can also check if you can find the hosted WCF service with tools like the WCFtestclient.

wcf http binding vs dualhttp binding

I'm trying to build a http listener (webservice) with wcf. This listener is part of a bigger desktop application. This desktop application also invokes the http listener.
When the listener receives data it should be passed to the desktop application. I tried to build an httpbinding service and use the callback mechanism
[OperationContract(IsOneWay = true)]
void OnDataReceived(Data data);
The problem is that I need wsDualhttpbinding because of the callback.
Do I really need to use the dualhttp to send data from one .NET app to another or am I missing something?
Thanks
Yes you need dual or duplex communication. HTTP by its very nature is a single direction protocol. The client has to invoke the server. The server has no way to talk to the client with out that.
Your problem is you have an event source which is exposed as a WCF service. The only way to for the service to send events to it's clients in real time is via a full duplex connection which means using either duathttp or dualtcp bindings.
However, using duplex bindings is complex at best. It would be simpler to allow the client to subscribe to the service and to then receive messages when events happen which the client is interested in.
This is much simpler than duplex bindings because there's no actual connection between client and service, only asynchronous messages.
While WCF provides bindings for msmq transport, it does not provide a binding with this kind of pub-sub support, so you can either code for this or you can use an open source messaging bus like NServiceBus.
If you are using a callback mechanism, as opposed to a polling mechanism, then yes, you need Dual binding as the WCF services needs to be able to both Send and Receive messages (as opposed to Receive and return responses)

Categories