Why a PageMethods calling doesn't work? - c#

I have a javascript call to a C# WebMethod. The same page has another call and it is working. I debugged the javascript code, this is called:
function userUpdReq_onOk()
{
...
var clientValidationPassed =Page_ClientValidate();
if( clientValidationPassed )
{
PageMethods.RequestUserUpdate(username, email, sex, zipCode, state, city, neighborhood, address, addressNumber, addressComplement, phone, promotionalInfo, connectionType, connectionSpeed, userUpdReq_OnComplete, userUpdReq_OnError);
}
...
}
The debugger passes by this line, but the next method it enters is userUpdReq_OnError( ). Why does it happen?

What is the message in error argument passed to userUpdReq_OnError()?
The OnError method is called when an error occurs inside of your page method. Sometimes this will be a casting issue, or a server error for some other reason. The error message passed to your OnError method should be able to guide you to the reason for the failure.
To get the error message, you can define the error handler as follows:
function userUpdReq_OnError(error){}
The error parameter will have a message indicating the reason for the failure.

Here is another issue "innocent" I think but it givme a lot troubbles, however, for some unknown reason, in some place aspx lost some reference to ScriptManager, so, what we must do to fix it is remove ScriptManager from aspx, add it again and set EnablePageMethods located in properties window to true.
Regards.

Related

Subscribing to an event provided by a WCF service

I have a third party application that I'm interacting with via a WCF service that they're exposing. When my application is loaded, the WCF service is supposed to hook into the callbacks I've defined and give me data back. This is straight-forward in VB but I think my issue is syntax in accomplishing this in C#.
Their documentation states the following: Your user control will need to define an internal event with a signature that matches the event that our service is listening for.
Here are my declarations for the handler, as provided by this services' documentation:
public delegate void MyDel(ref string Param1, ref string Param2);
public event MyDel GetInfo;
The documentation then says that I should be able to call this event in the following fashion, and the data I need from the service will be inside of the output variable I pass in by reference:
string output = "";
string methodName = "SpecificAction"
// this is null and throws an exception
// i've tried wrapping it in an if != null block to no avail
GetInfo(ref methodName, ref output);
Console.WriteLine(output.ToString());
In VB the following works without issue:
Public Event GetInfo (ByRef EventName As String, ByRef XmlString As String)
Dim Output As String = ""
RaiseEvent GetInfo("SpecificAction", Output)
The reason that I think I'm doing something wrong with the C# syntax is, as I said, it seems to work in VB just fine. Any suggestions would be appreciated.
Update
So I'm calling this method from my controls' public constructor, which is failing. If I move this exact code inside of a button click handler, for instance, its returning the data that I expect. I'm guessing my program is initializing and hitting that call before their service has hooked in? Is there a way to wait for that to happen?
After seeing that the above code worked when I removed it from my constructor and placed it in a button click handler, I realized that the issue was probably that I was trying to access the data before the service had hooked into it. By instead placing the code inside of my controls Load event, I was able to get back the data I needed.

Response.write in application_error not working?

Ok, I have a weird problem and can't find anything about it online. I'm trying to get custom application-level error handling working in ASP.NET. I have customErrors turned off in the web.config with the hopes of handling everything in application_error. Bear with me...
My code in global.asax is very simple:
void Application_Error(Object sender, EventArgs e) {
HttpContext.Current.Trace.Write("ERROR MESSAGE");
Response.TrySkipIisCustomErrors = true;
var error = Server.GetLastError();
HttpContext.Current.Response.Write("ERROR MESSAGE");
HttpContext.Current.ClearError();
}
I created a simple aspx page and threw an error in Page_Init or Page_Load, and everything worked as expected, i.e.: I see "ERROR MESSAGE" on a blank page when an error occurs.
Now I dynamically add some user controls to that aspx page and everything renders as expected. If I then throw an error from INSIDE one of the controls, I only get a blank white page. "ERROR MESSAGE" does not appear.
Now I know that application_error is still firing because when I remove the call to ClearError(), I get a Yellow Screen Of Death. Also, I can execute a Server.Transfer in there and that works fine. But nothing will come out for Response.Write.
This goes further: I can set Response.StatusCode, but a Response.Redirect will error out (and thus throw me into an infinite loop). Trying to write to the Event Log also errors out, but instead of throwing a new error, it throws the original, i.e.: "Input string was not in a correct format." when I try to convert a string to a number. As mentioned, Response.Write doesn't do anything, though it does not throw an error.
So looking at my trace log, in the second case (exception inside dynamically added user control) I see a full control tree and the error occurs right after Begin Render. In the first case, the tree is empty and the error is thrown either after Init or Load. Both times, trace.axd reports Unhandled Execution Error.
When I move the throw inside the control to the control's constructor or OnInit, things work as expected. When I move it to OnLoad or Render, it gets goofy.
So I'm wondering if at some point the Response object loses certain functionality. I've tried all sorts of permutations, from syntax (using HttpContext.Current.Response vs Context.Response vs pulling the Response object from the "sender" parameter), to moving the ClearError() or Response.Clear(), etc methods around, etc. I've tested the Response object for "null-ness" as well, and it never reports a null. I can set some response properties (http status code) but not others.
I'm using IIS7.5 integrated mode (.NET v4), but experienced similar problems when I tried Classic mode.
So I'm trying to solve this mystery, obviously, but my goal is to ultimately handle all errors, no matter what point in the asp.net lifecycle they occur, and to be able to write out some information from the handler (ie application_error).
Handled unhandled exceptions using this approach. Custom error is off in web.config.
All 3 options work.
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Response.TrySkipIisCustomErrors = true;
this.Server.ClearError();
this.Server.GetLastError();
//DO SOMETHING WITH GetLastError() may be redirect to different pages based on type of error
//Option 1:
Response.Write("Error");
//Option 2:
Response.Redirect("~/Error.aspx");
//Option 3:
this.Server.Transfer("~/Error.aspx");
}

Exit Method without ArgumentException

i have a Cancel button on a Progressbar, which is used to Show the Progress of Uploading e-Mails,
now i want this button to Exit the method which Uploads e-Mails.
My plan, once the button gets pressed, make bool cancelUpload true.
i have been unlucky to exit the method with the use of break or simply through an if Statement.
but now i found online that i could throw an Argument Exception,
which i implemented as follows:
if (cancelUpload)
{
throw new ArgumentException("SomeText");
}
but the Problem i have with this, is that once the User clicks on Cancel, he gets an Exception, which Looks like an Error or something went wrong, is there a way to get out of the method, without it looking as though something went wrong?(similiar to ArgumentException)
Thanks a lot in Advance!
Edit: The Method (void Method) is to big to be shown,
but when i tried to return;, i got an Error in Visual Studio saying:
TargetInvocationException was unhandled Exception has been thrown by the target of invocation

How to return response from webservice

I have made a web service and clients will call its methods. Suppose one method takes four parameters and I want to check that if any one of these is empty I want to send a response back to the client and I want to send it like an alert so client knows what argument is empty. I can not force client to give all parameters accurate. Is this possible?
[WebMethod]
Public void Method1(string str1,string str2,string str3,string str4)
{
if((str1=="") || (str2==""))
{
//send response back to client in form of an alert
}
}
As far as I know, you can send some agreed-upon object back to the sender, which the sender should be prepared to receive. It could be as simple as a string that contains "OK" or "ERROR", or it could be complex and have an error code, description, severity level, etc. What the sender does with it when they consume it is completely up to them though.
You could, for instance, make one of the fields in your response a severity level (maybe using something like Microsoft's Severity enum), which the sender could then use to determine whether they display an alert to their users or simply log the error somewhere on their system.
Don't reinvent the wheel with custom objects. Look at this MSDN article.
The easiest thing is just to throw an exception (sounds like an ArgumentException is appropriate in your case) and the client will receive a SoapException.
Or throw a SoapException yourself if you want more control, such as the ability to set a fault code.
You said you can't control the person calling the web method. As the method is of type void, the user will only know if something drastic happens. How about throwing an exception, and the error will propagate, to what detail it depends on the settings of the web server hosting the web service and the web server hosting the client (if the client is a web page).
throw new Exception("str1 or str2 in Method1 is empty");
In the "best" case, the user will see the error message "str1 or str2 in Method1 is empty. In the "worst" case, the user will get the standard IIS/ASP.NET error page. Like you said you have no control over the caller, it depends on how the caller codes the client and how he deploys the application. But whatever the case, it gets his attention!
I put best and worst in quotes because best and worst mean different things to different people. For people who want maximum security, best is when the error page says nothing about the error. For people who want user friendliness and fast troubleshooting, best is when the error message has a lot of details and what to do next.

"Callback Error: Invalid response from server" from C# ASP.Net application

I have an ASP.Net application with a button containing the following Javascript to be invoked when it is clicked:-
function calculate() {
sectionIndex = getSelectedRadioIndex("section");
compositionIndex = getSelectedRadioIndex("composition");
CallBackOU.callback( "calculate" , sectionIndex, compositionIndex );
}
I can verify that control reaches the last line of this function by setting a breakpoint on it. But instead of invoking the method in the code-behind file...
protected void CallBackOU_Callback (object sender, ComponentArt.Web.UI.CallBackEventArgs e)
{
//blah blah
}
I get a dialogue reporting
Callback Error: Invalid response from server.
This dialogue appears three times, after which the page sits there doing nothing (forever, so far as I can make out).
I can't find any information about this. Can anyone give me any clues or pointers about how to go about diagnosing the problem?
Without seeing the signature of the calculate callback method this is only a shot in the dark but some issues i have encounter when invoking web methods from javascript are make sure the method is properly decorated [WebMethod], make sure the method is static, make sure the parameters are of the correct type and named properly (they are infact case sensitive when deserializing JSON iirc). A little more information regarding how the call is made (JSON/XML) and the signature might help. Also, you can try using fiddler to see if you get any more information regarding the error.

Categories

Resources