是否可以将异常从webservice抛出到客户端。 [英] Is it possible to throw exception from webservice to client.

查看:88
本文介绍了是否可以将异常从webservice抛出到客户端。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下代码。



网络服务

< pre lang =c#> public class Service1:System.Web.Services.WebService
{
public class 员工
{
public Employee()
{

}
private string _name = ;
private string _address = ;
public string 名称
{
get
{
return _name;
}
set
{
if (< span class =code-keyword> string .IsNullOrEmpty( value ))
throw new 异常( 名称不能是null或空);
else
_name = value ;
}
}
public string PhoneNo {获得; set ; }
public string 地址
{
get
{
return _address;
}
set
{
if (< span class =code-keyword> string
.IsNullOrEmpty( value ))
throw new 异常( 地址不能是null或空);
else
_address = value ;
}
}
}


[WebMethod]
public < span class =code-keyword> void SetEmployee(员工ID)
{
throw new 异常( 我的测试异常);
}

[WebMethod]
public string HelloWorld()
{
/// < ; 摘要 >
/// 它将返回Hllow Wrold
/// < / summary >
return < span class =code-string> Hello World;
}

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

[WebMethod]
public int 最小值( int a, int b)
{
return a - b;
}



客户

 尝试 
{
ServiceReference1.Employee employee = new ServiceReference1.Employee() ;
employee.Name = ; // 我想在这里为空名字符串抛出execption。
employee.PhoneNo = < span class =code-string> 123456789;
employee.Address = ABC TEMP ADD;
}
catch (例外)
{

;
}







请帮助我在这里尝试。

谢谢。

等待回应。

解决方案

你可以试试这样的......



首先,将下面的课程添加到客户端。

  namespace  EmployeeProxy 
{
public class 员工:ServiceReference1.Employee
{
public new 字符串名称
{
获取
{
return base .Name;
}
设置
{
使用(< span class =code-keyword> var client = new ServiceReference1.Service1Client())
{
client.SetName( );
}
base .Name = value ;
}
}
}
}

其次,对客户端代码进行以下更改:

 尝试 
{
// 将此处的调用更改为新的派生代理...
EmployeeProxy.Employee employee = new EmployeeProxy.Employee();

employee.Name = ; // 我想在这里为空名字符串抛出execption。
employee.PhoneNo = 123456789;
employee.Address = ABC TEMP ADD;
}
catch (例外)
{
throw ;
}

第三,将以下方法添加到服务中:

 [WebMethod] 
public void SetName(员工e, string name)
{
e.Name = name;
}

最后,更新客户服务参考。



注意:这不是一个使用Web服务的好方法,而且更好的方法是:



  1. 使用Employee类创建类型库。
  2. 从客户端项目引用类型库。
  3. 确保将服务引用配置为'在引用的程序集中重用类型...''在配置服务引用对话框中。

然后,在使用Employee类型时,您可以完全忽略服务部分,并且异常将是在没有拨打服务电话的情况下抛出上述位置


是的,你可以。但异常将是 SoapException 。如果在Web服务中得到 NullReferenceException ,则不会在客户端获得 NullReferenceException

请参阅处理和抛出XML Web Services中的异常 [ ^ ]



请参阅以下链接以处理这些例外。

Web服务编程技巧和窍门:使用JAX-RPC进行异常处理 [ ^ ]





--Amit


I am trying following code.

Web Service

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

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.

解决方案

you could try something like this...


First, add the class below to the client.

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:

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:

[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


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


这篇关于是否可以将异常从webservice抛出到客户端。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆