简体   繁体   中英

calling methods from an ASMX web service -error-

I am trying to write an application (simple form) that will consume(invoke) the web service (Service1.asmx) and display the results. Now, the web service has a method. Here is the code:

public class Service1 : System.Web.Services.WebService
{

    [WebMethod]
    public Customer getCustomer(String id)
    {
        Customer customer = new Customer();

        customer.CustomerId = id;
        customer.CustomerName = "ABC Warehouse";
        customer.CustomerAddress = "123 Anywhere";
        customer.CustomerCity = "Pittsburgh";
        customer.CustomerState = "PA";
        customer.CustomerZip = 10379;
        customer.CustomerContact = "Dan Smith";
        customer.CustomerPhone = "2484567890";
        customer.CustomerCredit = "True";

        return customer;
    }
} 

When I run the web service from its original project I am able to type text in the text box Example and click invoke to view the xml results Example . Now, the simple form I have in another project has a textbox (txt1), button (btn1), and label (lbl1). I successfully add the web service and all the functions and classes transfer. Now, what I want to happen is when you type something in the text box, click submit, and view the xml results in the label which will include the typed text from the text box, exactly like if I would of run the service on its own. Here is the code where I am having trouble:

    public partial class _Default : System.Web.UI.Page
    {
        protected void btn1_Click(object sender, EventArgs e)
        {
            MyService.Service1 service = new MyService.Service1();
            string message = service.getCustomer(string id);
            ID = txt1.Text;
            lbl1.Text = message; 
        }
    }

Where am I going wrong? I am a beginner obviously, so all help would be appreciated. ps: MyService is what I named the namespace when I added the web service

your code will not compile because getCustomer return Customer object.

    protected void btn1_Click(object sender, EventArgs e)
    {
        MyService.Service1 service = new MyService.Service1();
        MyService.Customer customer= service.getCustomer(string id);
        ID = customer.CustomerId;
        // here you can generate XML based on customer object if you really need to do so
        lbl1.Text = GetCustomerXML(customer);// implement method to get XML
    }

    private string GetCustomerXML( MyService.Customer  customer)
    {
        XmlSerializer xsSubmit = new XmlSerializer(typeof(MyService.Customer));
        StringWriter sw= new StringWriter();
        XmlWriter writer = XmlWriter.Create(sw);
        xsSubmit.Serialize(writer, customer);
        return sw.ToString(); 
    }

First of all in service method you need to define that response format data must be in XML format. and then in client use 'XmlNode' to get data from service. I think this post will be usefull for you

Your error is to be thinking that you're going to get XML back. You're not. You're going to get a MyService.Customer back.

FYI, you should be using "Add Service Reference" to consume the .asmx service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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