I have WPF application that needs to access WCF service at start ( login window ). Each time application runs on Windows 7 it freezes on login until gets a responce from WCF. Is there any way to design this process differently?
It sounds like you need to make your calls asynchronously. Either start the call on a new thread (preferably using a Task), or call the WCF service using an asynchronous design pattern.
Put the WCF call in a background thread
There are two causes for something perceived as "freezing" when carrying out WCF Service Calls:
Calling the service in a synchronous fashion will block your UI thread until the call has completed. This bad and the reason why Silverlight forbids synchronous calls and forces you to follow the Begin/End Async pattern for any kind of RPC - be it WebRequest or WCF Layer. By default the async methods are not generated when adding a service reference to your WPF project but you can turn it on using Configure Service Reference.
The second cause is less obvious. The initial service client instantiation can take almost 3 seconds - even on fast machines. That's why you are well advised to QueueWorkUserItem the proxy instantiation and the BeginXXX call even when using the async pattern.
If you need the response from this service call to start the application, you can use a background thread to call this service and handle the return value. While this thread is consuming the service you can display your window or a splash screen.
If you don't need the retrun value from this service method you can use [OperationContract(IsOneWay=true)] on your service. So that you don't need to worry about threading and stuff.
You're calling a WCF call on the main thread, Therefore it will appear to crash.
You can either, put it in a thread and call it at the start of your app.
Put it in a background process ( if you're in visual studio you can drag this off the tool bar)
You can do threads quite easily by defining a Thread, then defining a threadstart, passing in your login WCF call, and the call thread.start(); and pass in your threadstart you defined.
A background worker is pretty similar, you can put your code in the backgroundWorker1_DoWork() method
Or make your WCF call Async, so it will send off the login response, and your login code will call on the "OnTaskCompleted" method( you could also put it on a new thread as well, but don't really have to)
Try this thread for Async WCF calls
How to make a call to my WCF service asynchronous?
Related
Someone else created a WSDL service for me in Java. There is so far only one simple method to call. We tested this method in a C# console app by adding a service reference with the service's url. If I make a call to the generated method that has 'Async' at the end, I get an error. However, since the synchronous method is available, making calls to that work perfectly.
The trouble is, when I add a service reference to the same service from within a Silverlight project, the synchronous method is not available. A call to the async method also errors out.
Is there a different way I can do this so I can call the synchronous method from Silverlight?
I use NLog's WebService target in Silverlight and run into a problem if the logging service is unavailable.
What happens is that all calls to the logging service hang for a long time until they time out.
This is firstly ugly and secondly problematic in the face of a request limit, which I have under my given circumstances. After the request limit is reached due to several pending logging requests, the application also fails to make requests that are not logging related.
Ideally I'd like a WebService target that sends the requests sequentially, but I can't configure it to do that, can I?
Since I have full control about the logging server I could also move to a different target, but I'd rather have a purely configuration-based solution.
Some time back I implemented a logging target like that for Silverlight. We were using Common.Logging for .NET and it did not support Silverlight. So, we ported part of Common.Logging to Silverlight and implemented a "logging service adapter" to send our logging messages to a logging service. I implemented a logging queue using the producer/consumer pattern. Maybe you will find it useful.
In the end, the project that I was working on when I implemented this didn't go anywhere, so this particular piece of code is not in use.
Using WCF service via async interface from worker thread, how do I ensure that events are sent from the client "in order"
I am currently developing a windows form application as the subscriber for my WCF service and a asp.net application as the publisher for the service.
Whenever I launch the service or the client, and attempt to "Connect" (AKA subscribe) to the service, it would take awhile to load before it displays a successful message or exception handling. And while the thing "loads", the windows form sort of "hangs" temporary until the thing loads finish.
Is it possible to create a loading effect while they are "loading" to feedback to the user? Anyone can guide me on how to actually start developing this effect.
Thanks!
You can use asynchronous service calls. Before calling the asynchronous call you can show graphical representation of this and in the completion of the asynchronous call you can stop displaying the progress of the call.
Refer to this link to know how you can call the wcf service asynchronously.
I am using WCF DataService I have a method:
[OperationContract]
public List<string> GetAges()
{
return _registrationData.GetAges();
}
on my client side, I instantiate it this way:
_registrationDataServices = new RegistrationDataServiceClient();
it now exposes a delegate
_registrationDataServices.GetAgesCompleted += GetAgesCompleted;
and a function call:
_registrationDataServices.GetAgesAsync()
the problem with this is that the call to the service will be asynchronous and the results comes in the delegate function, I want to be able to call the service and wait (block) until the results come back, how can this be done using WCF ?
I have to use a wcf, because I have a silverlight application that needs data from a database, the WCF is being called from a .NET assembly as my data access layer.
It is a very bad practice to call web service from a Silverlight/WPF application synchronously. The sync call would hang your UI until the web service call has ended, which will cause bad user experience.
However, if you're still up for it, here is a very helpful link.
Don't call it synchronously -- you will block the UI thread. Users usually think their software has crashed if the UI is frozen.
Ideally you would make a modal busy indicator that animates and shows 'progress' while you wait for the call to come back, and it goes away when you receive the Completed event.
Even if you were still convinced that blocking the UI thread was OK in your case, you can't actually call it synchronously! It was designed this way for a reason - go with the flow.
I'm not sure if I'm on the right lines but this is what I'm trying to do, I have a Silverlight application and a WCF service, the Silverlight app "subscribes" to the WCF service using PollingDuplex and the service can send data to any connected clients which works.
The service is marked with [ServiceContract(CallbackContract = typeof(IServiceCallback))] and it is single instanced
The problem is there is another service which should be able to call a standard method on this service to pass it data that will get distributed to the connected Silverlight clients, but because of the above settings it requires it to use callbacks (I can't change the other service).
Is there a way to have both types of operations, callback and standard in the same service if that makes sense?
Thanks for your time
Yes. It is possible. I guess CallbackContract parameter will not stop you from using your service as a regular request/response service (though I have not tried it).
But for the same contract, you may have to define two end points with different bindings, one with PollingDuplexHttpBinding and another one with basicHttpBinding (with silverlight this is the only other option).
You have to make sure that you are calling the right operation from the clients using duplex and basic http bindings.