How can I connect into a WCF after get ConnectFailure? - connection

I have a WCF service running in a server and a Windows Application which has a timer each 30 seconds checking by WCF some news values from database.
Everything is going well but if my server (when WCF is running) get offline or out for some reason, I get the Exception System.Reflection.TargetInvocationException or System.Net.WebExceptionStatus.ConnectFailure.
What I want is, well, my timer will check every 30 seconds and I want reestablish the connection when my server come back. Actually the only way to do it is close and open the WinForm app.
How can I check if the connection is back or reconnect without close my app?
public MyClass()
{
proxy = new TaskService.Service1Client();
proxy.GetTarefasCompleted += new EventHandler<GetTarefasCompletedEventArgs>
(proxy_GetTarefasCompleted);
timer = new System.Threading.Timer(Callback, null, 0, 30000);
}
private void Callback(Object state)
{
timer.Change(30000, Timeout.Infinite);
proxy.GetTarefasAsync(Environment.UserDomainName, Environment.UserName);
}
void proxy_GetTarefasCompleted(object sender, GetTarefasCompletedEventArgs e)
{
try
{
tarefas = e.Result.OrderByDescending(t => t.Id).ToList();
//code code code
}
catch (Exception ex)
{
if (ex.GetType().ToString() == "System.Net.WebExceptionStatus.ConnectFailure" ||
ex.GetType().ToString() == "System.Reflection.TargetInvocationException")
{
//treat the error
}
}
}

if (proxy.State != System.ServiceModel.CommunicationState.Opened)
{
proxy.Abort();
proxy.Open();
}
I believe this should do the trick.

Related

No errors are being raised when unsuccessfully writing to Azure service bus

When writing a message to the Azure Service Bus (using Microsoft.Azure.ServiceBus standard library, not the .Net Framework version) it works fine. However, when switching networks to a network that blocks that traffic and running it again I would expect an error being raised by SendAsync yet no error is thrown, therefor the function considers the send successful even though it is not.
Am I missing some logic to make sure that errors do get raised and trapped, it seems to be inline with all the examples I have seen.
I have tried this possible solution ..
Trouble catching exception on Azure Service Bus SendAsync method
.ContinueWith(t =>
{
Console.WriteLine(t.Status + "," + t.IsFaulted + "," + t.Exception.InnerException);
}, TaskContinuationOptions.OnlyOnFaulted);
.. and at no point does ContinueWith get hit.
[HttpPost]
[Consumes("application/json")]
[Produces("application/json")]
public ActionResult<Boolean> Post(Contract<T> contract)
{
Task.Run(() => SendMessage(contract));
// Other stuff
}
private async Task<ActionResult<Boolean>> SendMessage(Contract<T> contract)
{
JObject json = JObject.FromObject(contract);
Message message = new Message();
message.MessageId = Guid.NewGuid().ToString();
message.ContentType = ObjectType;
message.PartitionKey = ObjectType;
message.Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(contract));
foreach (KeyValuePair<String, String> route in DataRouting)
{
JToken jToken = json.SelectToken(route.Value);
if (jToken != null)
{
message.UserProperties[route.Key] = jToken.Value<String>();
}
else
{
String routeError = $"Could not find routing information in request for: {route.Key} in {route.Value}";
Logger.LogError(routeError);
return new UnprocessableEntityObjectResult(routeError);
}
}
// Send the message
try
{
await topicClient.SendAsync(message);
}
catch(Exception ex)
{
return new UnprocessableEntityObjectResult($"'Could not transmit message to service bus - {ex.Message}'");
}
return new OkObjectResult(true);
}
I expect that the error trap would be hit if the SendAsync fails to send the message. However it essentially fire and forgets, the message send is blocked by the firewall but is never reported to the caller by throwing an error.
Ok, found the answer, but I will leave this out there in case anyone else does this to themselves. It was down to my general muppetry when putting the MVC Controller together. Set async on the Post action and configure the await on the send. Obvious really but I missed it.
public virtual async Task<ActionResult<Boolean>> Post(Contract<T> contract){}
...
// Send the message
try
{
await topicClient.SendAsync(message).ConfigureAwait(false);
return new OkObjectResult(true); // Success if we got here
}
catch(Exception ex)
{
return new UnprocessableEntityObjectResult($"'Could not transmit message to service bus - {ex.Message}'");
}

Xamarin Forms does not wait for GeoLocation approval on iOS

I am have Xamarin Forms cross platform application for iOS, Android and UWP. I use the Xam.Plugin.Geolocator to get the location from each of the devices. My challenge with iOS is on the first launch of the app on a device. My code runs through and detects that IsGeolocationEnabled for the Plugin.Geolocator.Abstractions.IGeolocator object is false before the use is ever presented with the option to allow the application to use the device's location. This causes my app to inform the user that Location Services are not enabled for the application.
Basically I am hitting the line of code below before the use is ever asked about location services:
if (!App.gobj_RealGeoCoordinator.IsGeolocationEnabled)
ls_ErrorMessage = resourcestrings.GetValue("NoLocationServicesMessage");
On the other platforms, UWP at least, it seems that the app is paused while waiting for the user to respond to the request to use location services. Android just seems to automatically allow access to location if an app uses it.
Any idea how I can have the iOS detect if the request to use location services has been answered or not on the first run? Any suggestions would be greatly appreciated.
UPDATE(1):
I have all the correct items in my info.plist as seen below. I do eventually get the request to use the location just after my app has already checked IsGeolocationEnabled and decided the user has not enabled location services for the app.
UPDATE (2):
So I made a little progress using the following code.
try
{
while (!App.gobj_RealGeoCoordinator.IsGeolocationEnabled)
{
await Task.Delay(1000);
}
ViewModelObjects.AppSettings.CanAccessLocation = App.gobj_RealGeoCoordinator.IsGeolocationEnabled;
}
catch (Exception ex)
{
XXXXXXX
}
The challenge is that the plugin appears to provide me no way of knowing in the user has not responded to the location services dialog (i.e. IsGeolocationEnabled == false) versus the user said no to the location services dialog (also IsGeolocationEnabled == false). Any suggestions?
The way this type of permission request occurs on iOS is through an asynchronous dialog prompt, which is only shown if needed (and not until it is needed). Basically, you need to set up a callback from the CLLocation API. I have a helper class that I use for this purpose, which makes it even easier. Just call GetCurrentDeviceLocation() and pass it a callback function. The callback will only be invoked once the user has granted permission to the app, or if they previously granted permission:
public class GeoLocationService
{
readonly CLLocationManager _locationManager;
WeakReference<Action<Position>> _callback;
public GeoLocationService()
{
_locationManager = new CLLocationManager ();
_locationManager.AuthorizationChanged += AuthorizationChanged;
}
void AuthorizationChanged (object sender, CLAuthorizationChangedEventArgs e)
{
Action<Position> callback;
if (_callback == null || !_callback.TryGetTarget (out callback)) {
return;
}
if (IsAuthorized(e.Status)) {
var loc = _locationManager.Location;
var pos = new Position(loc.Coordinate.Latitude, loc.Coordinate.Longitude);
callback (pos);
}
}
static bool IsAuthorized(CLAuthorizationStatus status)
{
return
status == CLAuthorizationStatus.Authorized
|| status == CLAuthorizationStatus.AuthorizedAlways
|| status == CLAuthorizationStatus.AuthorizedWhenInUse;
}
public void GetCurrentDeviceLocation (Action<Position> callback)
{
_callback = new WeakReference<Action<Position>> (callback);
if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
if (_locationManager.Location == null) {
_locationManager.RequestWhenInUseAuthorization ();
return;
}
}
AuthorizationChanged (null, new CLAuthorizationChangedEventArgs (CLAuthorizationStatus.Authorized));
}
}

HTTP Request in LocationManager.DidDetermineState

I have written IBeacon App and my goal is to send Http request when the app is in background. I use the DidDetermineState event to determine the entrance/leave of a region. The thing is that when the event is called Http request unexpectedly stops somewhere. No exception is thrown and can not finish the request.
The strange thing is that the call stops randomly on different lines.
[Export ("locationManager:didDetermineState:forRegion:")]
public void DidDetermineState (CLLocationManager manager, CLRegionState state, CLRegion region)
{
string message = "";
if (state == CLRegionState.Inside) {
message = "Inside";
var url = String.Format ("{0}About", "https://myAPIURl/");
try {
var _httpClient = new HttpClient ();
_httpClient.Timeout = TimeSpan.FromMilliseconds (30000);
lock (_httpClient) {
try {
var getResult = _httpClient.GetAsync (url).Result;
if (getResult.IsSuccessStatusCode) {
var contentString = getResult.Content.ReadAsStringAsync ().Result;
var contentObject = JsonConvert.DeserializeObject<AboutUsModel> (contentString);
message = "result ok";
}
}
//ncrunch: no coverage start
catch (Exception e) {
message = e.Message;
}
}
} catch (Exception ex) {
message = ex.Message;
}
}
if (state == CLRegionState.Outside) {
message = "Outside";
}
}
P.S The request is successfully sent when the app is in foreground mode.
Any help is appreciated!
Apple only gives you about five seconds of background execution time after getting a beacon callback in the background. If the web service does not respond quickly enough, the app will simply suspend.
A typical way to solve this problem is to request extra background running time -- you can get up to 3 minutes worth just by asking. I wrote a blog post about the process here. The blog post is specifically about extending background ranging time, but the same technique applies to getting extended time for web service callbacks.

How to detect opening of a websocket connection from a controller

I have a controller (MyController), which requests data from a websocket connection (wsService) while initialising.
What is the best way to detect and wait that the websocket connection is open and then place the request from the controller?
Now I'm using following solution:
my_controller.dart:
MyController(wsService ws){
// when refresh() in wsService is called,
// the call is redirected to MyController's load()
ws.refresh = load;
}
load(){
ws.send(request);
}
ws_service.dart:
onConnect(){ //this is called when websocket connection is opened
refresh(); //this calls MyController's load()
}
I still think you should do something like this instead of make Angular polling the state.
MyController(wsService ws){
if(ws.readyState == WebSocket.OPEN) {
load();
} else {
ws.onOpen.first.then((_) => load());
}
}
load(){
ws.send(request);
}
This solution is still using polling, but with this solution the handling of websocket connection is kept in one place (wsService) and there is no duplicates in function calls.
MyController(wsService ws){
new Timer.periodic(new Duration(milliseconds: 100), (t){
if(ws.webSocket.readyState == WebSocket.OPEN) {
t.cancel();
load();
}
});
}
load(){
ws.send(request);
}

Blackberry: Make a iterative HTTP GET petition using Comms API

I want to store position coords (latitude, longitude) in a table in my MySQL DB querying a url in a way similar to this one: http://locationstore.com/postlocation.php?latitude=var1&longitude=var2 every ten seconds. PHP script works like a charm. Getting the coords in the device ain't no problem either. But making the request to the server is being a hard one. My code goes like this:
public class LocationHTTPSender extends Thread {
for (;;) {
try {
//fetch latest coordinates
coords = this.coords();
//reset url
this.url="http://locationstore.com/postlocation.php";
// create uri
uri = URI.create(this.url);
FireAndForgetDestination ffd = null;
ffd = (FireAndForgetDestination) DestinationFactory.getSenderDestination
("MyContext", uri);
if(ffd == null)
{
ffd = DestinationFactory.createFireAndForgetDestination
(new Context("MyContext"), uri);
}
ByteMessage myMsg = ffd.createByteMessage();
myMsg.setStringPayload("doesnt matter");
((HttpMessage) myMsg).setMethod(HttpMessage.POST);
((HttpMessage) myMsg).setQueryParam("latitude", coords[0]);
((HttpMessage) myMsg).setQueryParam("longitude", coords[1]);
((HttpMessage) myMsg).setQueryParam("user", "1");
int i = ffd.sendNoResponse(myMsg);
ffd.destroy();
System.out.println("Lets sleep for a while..");
Thread.sleep(10000);
System.out.println("woke up");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Exception message: " + e.toString());
e.printStackTrace();
}
}
I haven't run this code to test it, but I would be suspicious of this call:
ffd.destroy();
According to the API docs:
Closes the destination. This method cancels all outstanding messages,
discards all responses to those messages (if any), suspends delivery
of all incoming messages, and blocks any future receipt of messages
for this Destination. This method also destroys any persistable
outbound and inbound queues. If Destination uses the Push API, this
method will unregister associated push subscriptions. This method
should be called only during the removal of an application.
So, if you're seeing the first request succeed (at least sometimes), and subsequent requests fail, I would try removing that call to destroy().
See the BlackBerry docs example for this here
Ok so I finally got it running cheerfully. The problem was with the transport selection; even though this example delivered WAP2 (among others) as an available transport in my device, running the network diagnostics tool showed only BIS as available. It also gave me the connection parameters that I needed to append at the end of the URL (;deviceside=false;ConnectionUID=GPMDSEU01;ConnectionType=mds-public). The code ended up like this:
for (;;) {
try {
coords.refreshCoordinates();
this.defaultUrl();
this.setUrl(stringFuncs.replaceAll(this.getUrl(), "%latitude%", coords.getLatitude() + ""));
this.setUrl(stringFuncs.replaceAll(this.getUrl(), "%longitude%", coords.getLongitude() + ""));
cd = cf.getConnection(this.getUrl());
if (cd != null) {
try {
HttpConnection hc = (HttpConnection)cd.getConnection();
final int i = hc.getResponseCode();
hc.close();
} catch (Exception e) {
}
}
//dormir
Thread.sleep(15000);
} catch (Exception e) {
} finally {
//cerrar conexiones
//poner objetos a null
}
Thanks for your help #Nate, it's been very much appreciated.

Resources