2011-10-29 119 views
1

如何將我的Web應用程序中的類「BL_Customer」的類對象'obj'傳遞給我的Webservice(ASMX)中的函數'Insert()',然後訪問該類的屬性在Webservice中的對象?我通過'添加WebReference'工具包含了我的遠程web服務。我已經包括'使用WebRererence';'命名空間也。任何幫助將不勝感激。將類對象傳遞給WebService

這是我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 
    } 
} 

這是我的web服務:

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

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

    } 
} 

問候, 大衛

+0

ASMX web服務或WCF? – Damith

+0

@UnhandledException asmx –

回答

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); 
} 

而且如果你的客戶是建立在pre-.NET 3.0則需要使用添加Web引用對話框在Visual Studio中生成客戶端代理。

+0

我正在使用ASMX。 'TextBox3.Text = Convert.ToString(obj.Multiply(Convert.ToDouble(TextBox1.Text),Convert.ToDouble(TextBox2.Text)));'。 ** Multiply **是我的web服務中的一個功能。現在我想要將一個類對象傳遞給Multiply函數 –

+0

@DavidJohn,那麼您必須先修改Multiply Web服務方法,以便它將所需的對象作爲參數,而不是它當前所做的兩個雙精度值。完成此操作後,必須重新生成客戶端代理,以便反映新方法簽名,並且可以使用它。 –

+0

@DavidJohn更改您的Web服務將方法Multiply接受到類並更新Web服務引用。那麼你可以解析新類的對象到你的web服務方法 – Damith

0

如果您在Web服務中定義了「Bill」類,那麼您將能夠在Web應用程序和Web服務中使用它。 我不確定是否有一種方法可以使用Web服務應用程序中定義的類,但我認爲不是。

+0

@DavidJohn你的實體應該與你的層級分開存在。被傳遞的對象需要從每一層引用,因此所有的層都將依賴於實體類庫。 – rie819