Trigger event from ChannelFactory - c#

We are using the ChannelFactory to connect to our WCF Service. When ever I do a call to the service, I want the Channel factory to trigger another event. Something like 'OnFunctionCall'. How can I add such a event handler to the ChannelFactory. I'm kinda rushed for time, so I hope someone could help.
Working with C#, .net 3.5 SP1
Kind Regards

One possible solution is to create an endpoint behavior and attach it to the factory endpoint (see below). That behavior would add an inspector to the channels the factory creates (which can be either an IClientMessageInspector or an IParameterInspector), and your inspector would trigger the OnFunctionCall event whenever a message is sent to the server.
var factory = new ChannelFactory<IService>(...);
factory.Endpoint.Behaviors.Add(new MyInspector());
For more information on message inspectors you can look at http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/19/wcf-extensibility-message-inspectors.aspx, and for parameter inspectors you can look at http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/26/wcf-extensibility-iparameterinspector.aspx.

Related

Publish/Subscribe Pattern in C# WCF

How to register client on server to listen some change? I want to notify my client and send some data when something changed on my server. Also, I use NetTcpBinding. I tried many examples, but I can't get method from server to register in. I don't have any config file.
Thanks in advance.
There is example of registering client on server.
ChannelFactory<ITrending> factory = new ChannelFactory<ITrending>(
new NetTcpBinding(), new EndpointAddress(#"net.tcp://localhost:6000/ITrending"));
proxy = factory.CreateChannel();
You can use the CallBack functionality in WCF services , it can be used for sending the changes in any entity or object value. In this case, Client must act as a server and server as a Client to receive the updates based on the method in the Client which is receiving the changes.
More detailed explanation : http://www.dotnetcurry.com/wcf/721/push-data-wcf-callback-service

How to use a duplex wcf service to distribute messages to all clients?

I am building a simple duplex wcf service. In this service clients send messages to the server and the server distributes the message to all connected clients. However, despite the fact that I defined the ServiceBehavior attribute as
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)], only the client who sent the message receives it back from the server, while the other clients do not. I verified that there is just one instance of the server running.
What did I do wrong? I looked at other similar questions on the web, and they all say that I should define InstanceContextMode = InstanceContextMode.Single, which I already did.
Do you have a callback Contract. So that server will reply back to client.
Check the below tutorial for Implementing Callback Contract
Click here
Also check the below Project Event Notification server. This project is doing similar things what you want.
CodeProject Link
Feel free to ask me if you need any more clarification
You need to maintain the clistList as shown in the code snippet.
List<IMessageServiceCallback> clientList = new List<IMessageServiceCallback>();
public void Register()
{
IMessageServiceCallback callback = OperationContext.Current.GetCallbackChannel<IMessageServiceCallback>();
clientList.add(callback);
}
When you want to broadcast this message. You can iterate through the list and call the callback function to send message to clients.

CommunicationState listener?

Does anyone know of a way to create a listener for a proxy so that when the CommunicationState has changed I can invoke an action or a method?
An example, I want to update my WCF service for a code change. Since the application is in its early development code changes are very frequent. However, instead of annoying my employees with an email tell them that hey they need to restart their application. I would rather avoid them having to restart the app and having to send them an email. I would rather write a listener that looks at the communication state of a service and if it has changed to a faulted stated then attempt to reconnect.
Edit
Maybe some more context here.
InstanceContext context = new InstanceContext(this);
Subscriber = new SubscriptionService.MySubscriptionServiceClient(context);
Subscriber.Subscribe("");
So basically I want to know when the subscription service has stopped so that I can attempt to reconnect every 60 seconds or so. I tried looking for an event in the Subscriber service but I didn't see anything. Would I need to implement something on the service end?
Thanks
You can use the Faulted event available on the InnerChannel property of your generated client class. The State property of the client class is just a wrapper for InnerChannel.State, so this should work as you desire.
(For reference, you can also use the similarly named event on ChannelFactory<TChannel> if you are creating communication channels in code rather than using generated proxies.)

ServiceHost message receive event?

Surely there must be an event to attach onto before a ServiceHost or Channel or Dispatcher handles a message? I'm assuming it can be accessed through OperationContext.Current, but the closest events I can find are Opening and Closing. Is there something like a MessageReceived or BeforeMessageProcessed event?
If not using events, is there some other way using WCF classes/configuration to determine if a ServiceHost SingletonInstance is currently processing a DataContract?
EDIT: IDispatchMessageInspector (AfterReceiveRequest and BeforeSendReply) is perfect. I have about 8 services and need to know what messages (DataContracts) they are currently processing. While it would be possible to use something like a helper method that takes a delegate and does BeforeReceive(); InvokeDelegate(); AfterReceive(); it's far easier to use IDispatchMessageInspector and behaviors.
As far as I know, there isn't such an event at the service host level directly. Could you explain what you're trying to accomplish?
Normally, if you've got any processing that must be done whenever a message arrives, then the right way is to use one of the several extensibility points that WCF offers for this, like IDispatchMessageInspector. In this case, you'd inject your message inspector into the WCF pipeline through a service or an endpoint behavior.

.Net 2.0: How to subscribe to a event publisher on a remote computer using transient subscriptions?

My problem is that I want to have a server application (on a remote computer) to publish certain events to several client computers. The server and client communicate using .Net-Remoting so currently I am using remoted .Net-Events to get the functionality. But there is one drawback: when the server (the event publisher) comes offline and is restarted, the clients lose the connection since the remote object references become invalid.
I am looking into Loosely Coupled Events and Transient COM Subscriptions to solve this issue. I put together a small demo application with one publisher and two subscribers. It works beautifully on one computer.
I am using the COMAdmin-Libraries to create a transient subscription for the event subscribers. The code looks like this:
MyEventHandler handler = new MyEventHandler();
ICOMAdminCatalog catalog;
ICatalogCollection transientCollection;
ICatalogObject subscription;
catalog = (ICOMAdminCatalog)new COMAdminCatalog();
transientCollection = (ICatalogCollection)catalog.GetCollection("TransientSubscriptions");
subscription = (ICatalogObject)transientCollection.Add();
subscription.set_Value("Name", "SubTrans");
subscription.set_Value("SubscriberInterface", handler);
string eventClassString = "{B57E128F-DB28-451b-99D3-0F81DA487EDE}";
subscription.set_Value("EventCLSID", eventClassString);
string sinkString = "{9A616A06-4F8D-4fbc-B47F-482C24A04F35}";
subscription.set_Value("InterfaceID", sinkString);
subscription.set_Value("FilterCriteria", "");
subscription.set_Value("PublisherID", "");
transientCollection.SaveChanges();
handler.Event1 += OnEvent1;
handler.Event2 += OnEvent2;
My question now is: what do I have to change in the subscription to make this work over a network? Is it even possible?
What about MSMQ? It seems perfect for what you are trying to achieve? You can use a traditional publish/subscribe model or multicast the messages.
This might be a step too far, but have you considered using WCF and the callback element of WCF?
Callback effectively turns the what was client into a server. To be honest, I don't know a great deal about callback and have only experimented. Perhaps worth a 10 minute google though.
If your server comes offline every once and a while I cannot see how you can avoid to poll it to check that it is alive.
As you are talking about COM and remote computers, I suspect you'll have to do some DCOM security configuration.

Categories

Resources