簡體   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