webservice and using some kind of response to the client

T

Tony Johansson

Hello!

When using ASP.NET(aspx page) I can use Respone.Write to send information to
the browser..

I know that it would be better to use Windows-application event handle log
but just for
learning assume that I want to send ex.Message to the browser when an
exception occur how can I do that.
I tested to use Respone.Write but that was not possible ?

[WebMethod]
public decimal HowMuchWillItCost(string productName, int howMany)
{
SqlConnection sqlConn = null;
try
{
ConnectionStringSettings cs =
ConfigurationManager.ConnectionStrings["NorthwindConnectionString"];
string connString = cs.ConnectionString;
sqlConn = new SqlConnection(connString);
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = string.Format(@"select UnitPrice from Products
where productName = {0}", productName);

sqlCmd.Connection = sqlConn;
sqlConn.Open();
decimal price = (decimal)sqlCmd.ExecuteScalar();

return price * howMany;
}
catch (Exception ex)
{
return 1;
}
finally
{
if (sqlConn != null)
sqlConn.Close();
}
}

//Tony
 
G

Gregory A. Beamer

Avoid at all costs using Response.Write in an ASP.NET app - it's
*ALWAYS* the wrong solution, and completely unnecessary.

An addition to Mark's comments:

The exception to using Response.Write "in ASP.NET" is compiled controls.
Not user controls, as they are part of the ASP.NET tagged world, but
server controls that you compile into a library. The reason for this is
you need to fully render the control as it is called from the "page". By
using Response.Write, you output the content to the Response stream at
the point when it is called.

The big key here is "do I need to directly output to the Response
stream". In 99.999% of the cases, the answer is most likely NO.

When you use Response.Write in a page, you are outputting to the stream
when the event handler is called. If you put it in Page_Load, for
example (or even a button click, et al), you end up with your crud at
the top of the page, like so:

My Crud Here
<html>

Not very pretty or pratical.

Now, if you wanted to rewrite the ASP.NET engine and output all of the
HTML by hand, then Response.Write might work.

NOTE: If you are still insistent on writing ASP in ASP.NET (not a good
idea, imo), you can embed code in the page. But this leads to a
combination of concerns instead of a separation of concerns. You also
end up with a more tightly coupled application in most cases. Ouch!

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
G

Gregory A. Beamer

Hello!

When using ASP.NET(aspx page) I can use Respone.Write to send
information to the browser..

This is not a good idea unless you understand how to control the
Response stream. I have seen Response.Write used in many cases, and I am
not sure I have ever seen it used correctly. In general, when it is
used, the developer ends up reinventing the rendering engine (good case)
or writing an ASP page in an ASPX page, which is nasty.
I know that it would be better to use Windows-application event handle
log but just for
learning assume that I want to send ex.Message to the browser when an
exception occur how can I do that.
I tested to use Respone.Write but that was not possible ?

If you want to handle errors in your web services, you need to account
for them in your WSDL. Soap Exceptions are part of the framework and are
much better solutions to solving the exception problem than outputting
to the Response stream.

For the record, you can use the Response stream in a web service, but
you end up having to rewrite part of the "engine" in the process. You
cannot easily use a hybrid approach, which is what you are attempting.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
A

Abdul Sami

Unlike ASP.NET applications Global.asax cannot be used to catch errors on
application level in web services.

Inside web service we can throw exception details by mentioning error
message, type of error, actor (page which caused the error) and inner
exception. For example


string AnErrorString = "a custom error message";
SoapException ex = new
SoapException(AnErrorString,SoapException.ClientFaultCode);
throw ex;

On client side we can access it by:


catch (SoapException soapExc)
{
lblExceptionMessage.Text = soapExc.Message;
}


For further examples you can refer
http://www.codeproject.com/KB/WCF/WCF_Web_Service_Remoting.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top