将类对象传递给WebService

时间:2011-10-29 12:01:25

标签: c# asp.net web-services oop

如何将我的Web应用程序中类'BL_Customer'的类对象'obj'传递给我的Webservice(ASMX)中的函数'Insert()',然后在Webservice中访问该对象的属性?我通过“添加WebReference”工具包含了我的远程Web服务。我加入了'使用WebRererence;'命名空间也。任何帮助将不胜感激。

这是我在业务层中的BL_Customer类:

public class BL_Customer
{
    public BL_Customer()
    {

    }  
    string c_Cust_Name = string.Empty;    
    string c_Mobile_no = string.Empty;    
    public string Cust_Name
    {
        get { return c_Cust_Name; }
        set { c_Cust_Name = value; }
    }  
    public string Mobile_no
    {
        get { return c_Mobile_no; }
        set { c_Mobile_no = value; }
    } 

}

这是我的数据访问层:

public class DAL_Customer
{
    public SqlConnection con = new SqlConnection();
    WebReference.Service objWEB = new WebReference.Service();  //objWEB -> Webservice object
    Connection c = new Connection(); 
    public DAL_Customer()
    {
    }
    public int Customer_Insert(BL_Customer obj)
    {
      ---------
      ---------
        return objWEB.Insert(obj);  // Insert() is a function in my remote webservice 
    }
}

这是我的网络服务:

public class Service : System.Web.Services.WebService
{   
   public Service () {
    }

    [WebMethod]
    public string insert(**What should be here?**)
    {
        -----
        -----

    }
}

此致 大卫

2 个答案:

答案 0 :(得分:4)

根据您用于构建Web服务的技术,可能有不同的方法来实现此目的。如果您正在使用已弃用的ASMX Web服务,则可以添加一个方法,该方法将所需的类作为参数:

[WebMethod]
public void DoSomething(Person p)
{
    ...
}

如果您使用WCF,这是在.NET中构建Web服务的推荐技术,那么您将设计服务合同:

[ServiceContract]
public interface IMyService
{
    void DoSomething(Person p);
}

在这两种情况下,为了使用该服务,您将在客户端上生成强类型代理。建议再次使用Visual Studio中的“添加服务引用”对话框,通过将其指向Web服务的WSDL来生成强类型代理。然后你将调用方法:

using (var client = new MyServiceClient())
{
    Person p = new Person
    {
        FirstName = "john",
        LastName = "smith"
    };
    client.DoSomething(p);
}

如果您的客户端是在.NET 3.0之前构建的,则需要使用Visual Studio中的“添加Web引用”对话框来生成客户端代理。

答案 1 :(得分:0)

如果您在Web服务中定义“Bill”类,则可以在Web应用程序和Web服务中使用它。 我不确定是否有办法在Web服务上使用应用程序中定义的类,但我认为不是。

相关问题