I have a named pipe WCF connection where I have a timer elapsed heartbeat checking my connection is a live.
The client and timer is running before my service and that is ok.
My code looks like below:
try
{
serviceCallback = new ServiceCallback();
serviceProxy = new ServiceProxy(serviceCallback);
serviceChannel = serviceProxy.Channel;
serviceChannel.HeartBeat();
}
catch(Exception exception)
{
}
If my service is not running the "serviceChannel.HeartBeat()" will throw an exception, and after a while my timer will ensure I try again.
It looks like there is no way checking the WCF before actually trying it. Is that correct?
I'm running with first chance exception in Visual Studio 2013, but can I do something in the code, to prevent the debugger to be triggered here? I have tried [DebuggerStepThrough] but without luck.
Of course I can ignore this kind of exception in Visual Studio but I would prefer to only do it with this code.
Related
This is the exception I get:
An exception of type 'System.ServiceModel.FaultException`1' occurred in TestService.exe but was not handled in user code
I try to run my client and the debugger gets caught when I try to do the following
catch (Exception ex)
throw new FaultException<GeneralCalculatorFault>(fault, ex.ToString());
What could be the problem. Let me know if I need to provide more info.
I answered the question myself. It was giving me trouble because of a setting in Visual Studio 2012.
Whenever the exception was thrown if I pressed "Continue", it handled everything just fine on the client side just like it should. There is a checkbox on the server side that allows me to ignore exceptions of particular types, so I checked it and now the server side no longer complains that the exception is not being handled.
Maybe this will help some other newbie like me. :)
I'm currently building a WP7 app that consumes WCF Data Services hosted on a web server. What I'd like to deal with is
cathayService.ServiceException += (sender, e) =>
{
Debug.WriteLine(e.Exception.ToString());
MessageBox.Show(e.Exception.ToString(), "Service Error", MessageBoxButton.OK);
};
The service exception triggers if I have a lack of internet connectivity. It also triggers when I face with fast app switching. How'd I be able to differentiate the source of the ServiceException?
Hope someone can give me an idea... Many thanks! :)
[It's unclear if you are getting a ServiceException instance, or if you are referring to the ServiceException event in some places above]
Check the exception you get - if it's typed as ThreadAbortException, that means you are being switched out. If you actually get a ServiceException thrown, check it's inner Exception and see if THAT guy is ThreadAbortException.
My suggestion is that you don't hook that event though and instead use the actual callback events on the WCF client to check the .Error property of the EventArgs you get back.
I am just learning WCF and wrote a Windows Service hosting a WCF service. Ever since I started it in service.msc in the remote server (physically remote, and very slow) I think I have already hit and fixed like a hundred errors here and there already. I am now finally stuck.
At this point, I have enabled tracing and message logging. The first and only function call looks like this:
[OperationContract]
public MyServiceResponse ConnectToAXDynamicsViaDotNET2BusinessConnectorWithSadFace()
{
try
{
throw new NotImplemented();
}
catch(Exception ex)
{
return new MyServiceResponse(ex, ex.Message);
}
}
[DataContract]
public class MyServiceResponse
{
// ...
}
Upon calling the operation, the client just sits and waits until timeout. Checking the trace, it records my thrown exception. So it makes me wonder if WCF actually blocks there and ignore my catch clause.
I then tested with just a simple return value without throwing and it FINALLY works.
My question is, how then can I make the above scenario work, ie. catch the exception and return a sensible result back to client? Maybe it's just the tracing that blocks me, but I want to enable tracing during this whole debugging process otherwise it's really hard to work.
Thanks for your precious help!
EDIT: Sorry, I found this right after I posted this question. Happens all the time: http://msdn.microsoft.com/en-us/library/ee942778.aspx but I have yet to read it as I got to run off now. Not sure it it will solve my problem.
Risk being downvoted, but just for documentation sake and general usefulness of having this question:
Through reading the FaultException handling articles, I guess the above behavior is due to Exception class not serializable. And the service silently disconnects itself without returning any error messages eventhough the "send (unknown) faults to client" is enabled. As for why it does so, I have no idea yet. Will update if I find out more.
I have since changed to throw new FaultException() instead of returning my own MyServiceResponses.
In my application, I am using client.open and client.close to open a WCF proxy channel, use the wcf method and finally close it. I do this is several parts of my Windows Forms application.
However, I have noticed that client.close does not necessarily close the connection immediately. Why is this? Should I use client.Abort instead?
Am I using the WCF client the right way? i.e. clien.open -> call WCF method -> client.close.
Thanks for any help in advance!
Subbu
The .Close() method will try to close the connection gracefully, e.g. a pending call will still be finished by the server, and only after that's done, the connection is terminated. Use this whenever possible - it's the gentle and preferred way of closing a client connection.
.Abort() really just tears down the communication channel - not matter whether a call is still in progress or not. It's the ultimate, last resort escape hatch, if .Close() failed.
As a best practice, I would wrap my service calls into a try...catch block, something like this:
try
{
myServiceClient.CallSomeMethod();
myServiceClient.Close();
}
catch(FaultException<SomeError>)
{
myServiceClient.Abort();
}
// possibly other catch clauses, too - call .Abort() inside them
catch(CommunicationException)
{
myServiceClient.Abort();
}
I recently migrated a VB6 app to VB.Net. Entire VB6 dependency are removed . And the code was working fine just for a single module mean to say like for WinApp it is working fine.
Now my requirement has been changed, now the same class will be accessed by multiple application , it might a Windows App, Web App or a web service. So I am not able decide any efficient error handling pattern.
So you you guys help me out. Currently what I am doing is that parent function , i am passing two vars LogError as bool and ErrorMessage as string parameter, that will further check something like this
Catch(ex as Exception)
If LogError then
MessageBox.Show("My_Module_Name :" & ex.Message)
EndIf
ErrorMessage = ex.Message
End Try
Also same catch block is used in all other functions/ subroutines.
So what I need is any elegant handling method that will work efficiently on cross-app platform(win/web/service)
I'd suggest logging the messages, either to a log file or to the Event log, but why not let the clients choose, in that you could add some methods to let the client decide where it should be logged. Though instead of having your exception handler handle the message and put it into an errormessage variable I'd just follow the logging with a throw so that the Exception continued up in the call chain until some code that knew how to handle it got it.
In general it's not good practice to catch Exception, you should catch only the exceptions that you can handle. But I think it's ok if you just want to log it and then will re-throw it again.