Click here to Skip to main content
15,943,362 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am trying following code.

Web Service
C#
public class Service1 : System.Web.Services.WebService
    {
        public class Employee
        {
            public Employee()
            {

            }
            private string _name = "";
            private string _address = "";
            public string Name
            {
                get
                {
                    return _name;
                }
                set
                {
                    if (string.IsNullOrEmpty(value))
                        throw new Exception("Name can't be null or empty");
                    else
                        _name = value;
                }
            }
            public string PhoneNo { get; set; }
            public string Address
            {
                get
                {
                    return _address;
                }
                set
                {
                    if (string.IsNullOrEmpty(value))
                        throw new Exception("Address can't be null or empty");
                    else
                        _address = value;
                }
            }
        }


        [WebMethod]
        public void SetEmployee(Employee id)
        {
            throw new Exception("My exception for testing");
        }
       
        [WebMethod]
        public string HelloWorld()
        {
           ///<summary>
           ///It will return "Hllow Wrold"
           ///</summary>
            return "Hello World";
        }

        [WebMethod]
        public int Add(int a,int b)
        {
            return a + b;
        }

        [WebMethod]
        public int Min(int a, int b)
        {
            return a - b;
        }

Client
C#
try
             {
                 ServiceReference1.Employee employee = new ServiceReference1.Employee();
                 employee.Name = "";  // I want to throw execption here for empty name string.
                 employee.PhoneNo = "123456789";
                 employee.Address = "ABC TEMP ADD";
             }
             catch (Exception)
             {

                 throw;
             }



Please help what i should try here.
Thanks.
Waiting for Response.
Posted
Comments
MuhammadUSman1 27-Mar-13 4:30am    
Why it is not throwing Exception of webservice?
CognitiveFeedback 27-Mar-13 6:59am    
The Employee class on the Client does not implement the exception logic like in the Service side Emmployee class. That is way it is not throwing an exception.

you could try something like this...


First, add the class below to the client.


C#
namespace EmployeeProxy
{
  public class Employee : ServiceReference1.Employee
  {
    public new string Name
    {
        get 
        {
            return base.Name; 
        }
        set 
        {
            using (var client = new ServiceReference1.Service1Client())
            {
                client.SetName(this, value);
            }
            base.Name = value;
        }
    }
  }
}

Secondly, make the following change to the client code:


C#
try
{
    // change call here to the new derived proxy...
    EmployeeProxy.Employee employee = new EmployeeProxy.Employee();

    employee.Name = "";  // I want to throw execption here for empty name string. 
    employee.PhoneNo = "123456789";
    employee.Address = "ABC TEMP ADD";
}
catch (Exception)
{
    throw;
}

Thirdly, add the following method to the service:


C#
[WebMethod]
public void SetName(Employee e, string name)
{
    e.Name = name;
}

And finally, update the Service Reference on the Client.


NOTE: This is not a very good way to use web services however, and a better method would be:



  1. Create a Type Library with the Employee class.
  2. Reference the Type Library from the Client project.
  3. Ensure the Service Reference is configured to 'Reuse types in referenced assemblies...' on Configure Service Reference dialog box.

Then you can ignore the service part altogether when working with the Employee type, and exceptions will be thrown where indicated above without making a service call

 
Share this answer
 
v4
Yes you can. But the exception will be a SoapException. If you get a NullReferenceException in web service, you're not going to get a NullReferenceException on the client side.
See Handling and Throwing Exceptions in XML Web Services[^]

Refer the link below to handle those exceptions.
Web services programming tips and tricks: Exception Handling with JAX-RPC[^]


--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900