- Home /
Coroutine freeze other animation & UI part on service request
Hello,
When I do request for service call that time my coroutine freeze my game until response not come.... How can i do service request in unity coroutine or i used threading for that...plz help me
IEnumerator ServiceCall() {
ChannelFactory<IService> factory_1 = null;
IService channel_1 = null;
string resultstr = "";
try {
WSHttpBinding binding = new WSHttpBinding ();
EndpointAddress address = new EndpointAddress ("https://192.168.0.1/Service/Service.svc");
binding.Security.Mode = SecurityMode.Transport;
factory_1 = new ChannelFactory<IService> (binding, address);
factory_1.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
channel_1 = factory_1.CreateChannel ();
resultstr = channel_1.GetData(10);
yield return new WaitForSeconds (5);
Debug.Log(resultstr);
} catch (Exception e) {
Debug.Log (e.Message);
}
yeild return break;
}
Answer by Dave-Carlile · Jul 31, 2015 at 12:53 PM
I don't really know much about WCF, but my guess would be that the GetData
call is a synchronous call so it waits until the data is available before it returns. Coroutines are a form of cooperative multitasking, so if the coroutine doesn't run quickly between yield statements, everything freezes.
You should be able to call an asynchronous method with WCF, so the call returns immediately, then the coroutine can loop and poll with yields inside the loop. Or use a callback or something, but that should give you something to google for.
I was going to point to $$anonymous$$icrosoft's async example, but I'm not 100% sure that will work in the version of $$anonymous$$ono used by Unity. It's worth giving it a shot though.
Otherwise you might need to use threading, or use Unity's WWW class to grab the data. There are JSON serializers that can then turn it into a class for you.