简体   繁体   English

将类对象传递给WebService

[英]Passing class object to a WebService

How can I pass a class object 'obj' of the class ' BL_Customer' from my Web application to a function 'Insert()' in my Webservice (ASMX) and then access the properties of that object in the Webservice?. 如何将我的Web应用程序中的类'BL_Customer'的类对象'obj'传递给我的Webservice(ASMX)中的函数'Insert()',然后在Webservice中访问该对象的属性? I have included my remote webservice by 'Add WebReference' facility. 我通过“添加WebReference”工具包含了我的远程Web服务。 I have included 'using WebRererence;' 我加入了'使用WebRererence;' namespace also. 命名空间也。 Any help will be appreciated. 任何帮助将不胜感激。

This is my BL_Customer class in Business Layer: 这是我在业务层中的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; }
    } 

}

This is my Data Access Layer: 这是我的数据访问层:

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

This is my webservice: 这是我的网络服务:

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

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

    }
}

Regards, David 问候,大卫

Depending on the technology you are using to build your web service there might be different ways to achieve this. 根据您用于构建Web服务的技术,可能有不同的方法来实现此目的。 If you are using the deprecated now ASMX web services you would add a method that will take as parameter the class you need: 如果您正在使用已弃用的ASMX Web服务,则可以添加一个方法,该方法将所需的类作为参数:

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

If you are using WCF which is the recommended technology to build web services in .NET you would design a service contract: 如果您使用的是WCF,这是在.NET中构建Web服务的推荐技术,那么您将设计一个服务合同:

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

In both cases in order to consume the service you will generate a strongly typed proxy on the client. 在这两种情况下,为了使用服务,您将在客户端上生成强类型代理。 Once again the recommended is to use Add Service Reference dialog in Visual Studio to generate a strongly typed proxy by pointing it to the WSDL of the web service. 建议再次使用Visual Studio中的“添加服务引用”对话框,通过将其指向Web服务的WSDL来生成强类型代理。 Then you will invoke the method: 然后你将调用该方法:

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

And if your client is built on pre-.NET 3.0 you will need to use the Add Web Reference dialog in Visual Studio to generate the client proxy. 如果您的客户端是在.NET 3.0之前构建的,则需要使用Visual Studio中的“添加Web引用”对话框来生成客户端代理。

If you define class 'Bill' in your web service you will be able to use it both in you web app and in web service. 如果您在Web服务中定义类“Bill”,则可以在Web应用程序和Web服务中使用它。 I'm not sure if there is a way to use classes that were defined in app on web service but I think it's not. 我不确定是否有办法在Web服务上使用应用程序中定义的类,但我认为不是。

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

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