Call a callback method on WCF server wihout waiting client must finish - c#

I am using two-ways WCF model.
WCF server can send a data (broadcast) to WCF clients using callback method. However server must wait clients to finish the this method. With a lot of clients, server must wait a lot time. Please help me how to fix this problem at server side so that server mustn't wait clients. Thanks.

Mark the callback method as oneway:
[ServiceContract]
interface IMyContract
{
[OperationContract(IsOneWay = true)]
void MyMethod()
}

Related

WCF REST Service and status polling

I am currently developing a REST Service where one of the methods is a long running task (named CalculatePrimeOneWay). Its definition is
[OperationContract(IsOneWay = true)]
[WebInvoke(Method = "POST", UriTemplate = "primeoneway/{jobnumber}")]
void CalculatePrimeOneWay(string jobnumber, Prime prime);
As there is no callback capability with a REST Service, I thought about implementing a kind of polling method to obtain status information. This polling method is defined as
[OperationContract]
[WebGet(UriTemplate = "poll/{jobnumber}")]
Job GetJobStatus(string jobnumber);
I write status information into a MS SQL table.
I start my client and invoke the CalculatePrimeOneWay Method. Subsequent calls to GetJobStatus return a WebException with Protocol Error, Status Code 400 and Status Description Bad Request.
However, if CalculatePrimeOneWay is finished and I then invoke GetJobStatus, all works perfectly fine.
Why am I unable to call GetJobStatus while CalculatePrimeOneWay is still running?
What other solutions would be possible for my scenario of a long runnning task and a polling mechanism?
Thanks
The behaviour you're getting may be a result of one or both of two things:
You are running the service with per-session or singleton instancing
You are re-using the same client channel to make the polling call as you did to make the one-way call.
You should have the service configured to per-call instancing and make the polling call on a new client channel instance.
I have now explicitly defined the service PerCall. For each call from
the client I get a new HttpWebRequest object and after the call, I
close the web response object. The behaviour is still the same
OK try replacing
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
with
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
ConcurrencyMode = ConcurrencyMode.Multiple,
UseSynchronizationContext = false)]
I am using IIS Express out of my VisualStudio
OK - the reason you are getting this behaviour is because you're hosting using the Visual Studio which uses the cassini web server. This does not support concurrency when processing requests. See posts here and here.
Try hosting the service temporarily in a console app and this problem will be resolved.

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.

Debug WCF callback service

i am implementing a wcf callback service following this tutorial.
The thing is that my callback method on the client side is never called.
public void NotifyClient(object sender, EventArgs args)
{
INotificationCallback callback = OperationContext.Current.GetCallbackChannel<INotificationCallback >();
callback.OnStepReached(((ModuleEventArgs)args).Step);
}
The callback is called on the server side but never reaches the client side. I don't know what went wrong, the only I've got is a TimeOutException after a while.
My callback on the server side is a System.Runtime.Remoting.Proxies._TransparentProxy.
I'd like to know if there is an easy way to debug this behavior.
If you have not already, you may want to consider enabling WCF tracing to ensure the server is really calling the client callback method (callbackInstance.OnCallback(); ).
For reference, the following link provides an overview of WCF Tracing:
http://msdn.microsoft.com/en-us/library/ms733025.aspx
The servicecontract and the callback contract should be one-way. The linked tutorial is missing that.
So, Update the operation contract to [OperationContract(IsOneWay = true)]

WCF service instance will not close despite only calling oneway method

I have a WCF service running inside a windows service on a remote machine.
In the WCF service's contract, I have a method that takes a long time to run set up as
[OperationContract(IsOneWay = true)]
void Update(myClass[] stuff);
Everything works fine, the method gets called, I can see what it needs to do start getting done.
The problem is when I go to close the instance of the WCF service in my code, it times out and I get:
The socket connection was aborted.
This could be caused by an error
processing your message or a receive
timeout being exceeded by the remote
host, or an underlying network
resource issue. Local socket timeout
was '00:02:00'.
I thought the one way contract allowed me to fire and move on. Is there something I am missing? If not are there workarounds for this?
The ServiceContract attribute on your service's interface definition defaults the SessionMode property to SessionMode.Allowed, i.e.,
[ServiceContract(SessionMode = SessionMode.Allowed)]
public interface IMyContract
{
[OperationContract(IsOneWay = true)]
void Update(myClass[] stuff);
}
According to Juval Lowy's Programming WCF Services,
...when the SessionMode property is
configured with SessionMode.Allowed,
it merely allows transport sessions,
but does not enforce it. The exact
resulting behavior is a product of the
service configuration and the binding
used.
Thus, if you are using the WSHttpBinding with security or reliable messaging, the NetTcpBinding, or the NetNamedPipeBinding, then the service will behave as a per-session service. This simply means that as long as the client proxy has not been closed, a session will still be in place between the service and the client. By closing the client proxy as suggested by Shiraz should fix this.
Juval's book also says this with regard to one-way operations:
If the number queued messages has
exceeded the queue's capacity, then
the client will block, even when
issuing a one-way call. However, one
the call is queued, the client is
unblocked and can continue executing,
while the service processes the
operation in the background.
So while one-way operations do allow for fire-and-forget operation, you can still run into cases where your client may block.
Your "Update" is a method on the service.
When you open the wcf client, a connection to the service remains open until you call Close (or Abort).
You are probably not calling close, and it is therefore remaining open until it timesout.

WCF: Callback is not asynchronous

I'm trying to program a client server based on the callback infrastructure provided by WCF but it isn't working asynchronously.
My client connects to the server calling a login method, where I save the clients callback channel by doing
MyCallback callback =
OperationContext.Current.GetCallbackChannel<MyNamespace.MyCallback>()
After that the server does some processing and uses the callback object to communicate with the client.
All this works, the problem resides on the fact that even though I've set the method in the OperationContract as IsOneWay=true, the server still hangs when doing the call to the client.
I've tested this by launching the server for debug in the Visual Studio, detaching it, launching the client, calling the above mentioned login method, putting a break point in the implemented callback method of the client, and making the server send a response to the client. The server stops doing what it's supposed to do, waiting for the response of the client.
Any help is appreciated.
The trick is, to call the callback asynchronously from the server. Look at this:
[OperationContract(IsOneWay = true, AsyncPattern = true)]
IAsyncResult BeginOnMessageReceived(LiveDataMessage message, AsyncCallback acb, object state);
void EndOnMessageReceived(IAsyncResult iar);
I think the sollution to your problem is to properly set the 'ConcurecyMode' and 'Instance ContextMode' attributes for your service. To do that you must decorate your service declaration with those attributes as shown in the exemple below:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class SubscriberService: ISubscriberServiceContract
{...}
InstanceContextMode.Single builds your service as a Singleton object so there is only one instance of your service running for all clients;
ConcurencyMode.Reentrant or ConcurencyMode.Multiple enables multithreaded work for the service instance. For 'Multiple' you must take care of thread syncronization in your service.
Did you try to set
[CallbackBehavior(UseSynchronizationContext = false)]
on the client side object implementing the callback interface ?

Categories

Resources