繁体   English   中英

将类对象传递给WebService

[英]Passing class object to a WebService

如何将我的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?**)
    {
        -----
        -----

    }
}

问候,大卫

根据您用于构建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引用”对话框来生成客户端代理。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM