locked
Web service RRS feed

  • Question

  • User1997423929 posted
    How to call web service?
    Monday, December 10, 2018 3:27 PM

Answers

  • User-271186128 posted

    Hi Adwin Jha,

    Please refer to the following steps:

    1.  create a web service in one application, using the following code:

        /// <summary>
        /// Summary description for first_web_service
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
        public class first_web_service : System.Web.Services.WebService
        {
    
            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }
    
            [WebMethod]
            public string SayHello(string name)
            {
                return "Hello " + name;
            }
        }

    2.  Add service reference in another application:

    Right click the application, click the add service reference options:

    Then using the following code to call the web service:

                ServiceReference1.first_web_serviceSoapClient service = new ServiceReference1.first_web_serviceSoapClient();
                string result = service.SayHello("Dillion");

    the result as below:

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 14, 2018 6:46 AM

All replies

  • User475983607 posted

    How to call web service?

    What kind of web service? 

    3rd party web service come with reference documentation.  Have you tried reading the documentation and can you provide a link to the service?

    What kind of application are you building?

    Monday, December 10, 2018 3:43 PM
  • User1997423929 posted
    I have developed one web based application in asp.net in which one web service has been created. Now i want to call that web service in my dot net application. I had read some documents but i can't get solution. So pls help me.
    Monday, December 10, 2018 4:11 PM
  • User475983607 posted

    I have developed one web based application in asp.net in which one web service has been created. Now i want to call that web service in my dot net application. I had read some documents but i can't get solution. So pls help me.

    And what kind of application have you built; MVC, Web Forms, Razor Pages?  What kind of service did you create; ASMX, WCF, Web API?

    Can you provide a link to the documentation that you have read?

    Is there anyway you can provide the source code so we can have, at least, an idea of what you are doing?

    Monday, December 10, 2018 4:19 PM
  • User1997423929 posted
    Application type is "Web Forms" and the link are as given below :-
    https://www.c-sharpcorner.com/UploadFile/0c1bb2/consuming-web-service-in-Asp-Net-web-application/
    Right now I don't have a code but after referring this i hope u can understand what i am trying to say.
    Monday, December 10, 2018 4:29 PM
  • User-943250815 posted

    I have developed one web based application in asp.net in which one web service has been created.

    If I understand, you created a WebService inside your WebApplication, and want call WebService from WebApplication. Is that correct?

    Monday, December 10, 2018 8:11 PM
  • User-271186128 posted

    Hi Adwin Jha,

    Application type is "Web Forms" and the link are as given below :-
    https://www.c-sharpcorner.com/UploadFile/0c1bb2/consuming-web-service-in-Asp-Net-web-application/
    Right now I don't have a code but after referring this i hope u can understand what i am trying to say.

    From your description, it seems that you have already found the right direction, have you solved the problem?  

    As the article said, if we want to consume the web service, we could add the Reference of the Web Service first, then create a service instance to consume the web service.

    More details steps, you could check this articles:

    https://www.aspsnippets.com/Articles/Call-Consume-Web-Service-ASMX-in-ASPNet-web-application-using-C-and-VBNet.aspx

    If you have any further question about consuming the web service, please feel free let me know. If the problem have been solved, please close this thread.

    Best regards,
    Dillion

    Tuesday, December 11, 2018 2:00 AM
  • User1997423929 posted
    Yes exactly.
    Tuesday, December 11, 2018 3:59 AM
  • User-943250815 posted

    @Adwin Jha, if you say "Yes exactly" to

    If I understand, you created a WebService inside your WebApplication, and want call WebService from WebApplication. Is that correct?


    This the wrong approach, they should be separated things, in link you gave, you already have right direction for your question, you missing the initial part, read it again.
    First create your WebService, and run it, then open another instance of VS and create your WebApplication (WebForm as an example) then add a Reference of running WebService and follow all the article to the end.
    Other than you have no way to add Reference.

    Tuesday, December 11, 2018 12:06 PM
  • User1997423929 posted

    Web services are running successfully but when I am trying to call web service in other instance getting this error (Cannot call "call_web_reference.localhost.first_web_service.name()" because it is a web method).

    Please help me.

    Wednesday, December 12, 2018 5:20 AM
  • User-271186128 posted

    Hi Adwin Jha,

    Web services are running successfully but when I am trying to call web service in other instance getting this error (Cannot call "call_web_reference.localhost.first_web_service.name()" because it is a web method).

    Can you share related code? It might be easier for us to help you find the issue.

    Best regards,
    Dillion

    Thursday, December 13, 2018 1:26 AM
  • User1997423929 posted

    //This is my web service code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Script.Serialization;

    namespace MVC
    {
    /// <summary>
    /// Summary description for first_web_service
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class first_web_service : System.Web.Services.WebService
    {

    public string HelloWorld()
    {
    return "Hello World";
    }

    [WebMethod]
    public string name()
    {
    return "Adwin";
    }
    }
    }

    //Here, I am trying to call that web service

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Services;
    using call_web_reference.localhost;
    using System.Net;

    namespace call_web_reference
    {
    public partial class call_reference : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    localhost.first_web_service web = new localhost.first_web_service();
    web.name();
    }
    }
    }

    Thursday, December 13, 2018 3:44 AM
  • User753101303 posted

    Hi,

    You are trying to call a web service as if it is just a regular method. It is usually done over http from another C# app or from JavaScript. Is this your intent or will you really call that from the same app ? In this later case, a web service doesn't seems to bring anything.

    If you have to do both you should likely separate what the service does (as regular methods you could call directly) and how it is exposed for remote clients.

    Clarify from where you'll actually call your service. BTW if you just start it might be better to use the "web api" service style :

    https://docs.microsoft.com/en-us/aspnet/web-api/index

    (and it's unclear if your intent is to use Web Forms and/or MVC)

    Thursday, December 13, 2018 9:49 AM
  • User1997423929 posted

    Sir, I am calling the web service from my another C# app only but then also I am getting the same error which I have already posted in my prev post.

    Thursday, December 13, 2018 9:56 AM
  • User-271186128 posted

    Hi Adwin Jha,

    Please refer to the following steps:

    1.  create a web service in one application, using the following code:

        /// <summary>
        /// Summary description for first_web_service
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
        public class first_web_service : System.Web.Services.WebService
        {
    
            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }
    
            [WebMethod]
            public string SayHello(string name)
            {
                return "Hello " + name;
            }
        }

    2.  Add service reference in another application:

    Right click the application, click the add service reference options:

    Then using the following code to call the web service:

                ServiceReference1.first_web_serviceSoapClient service = new ServiceReference1.first_web_serviceSoapClient();
                string result = service.SayHello("Dillion");

    the result as below:

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 14, 2018 6:46 AM
  • User1997423929 posted

    Thank You so much. My problem has solved now.

    Friday, December 14, 2018 9:28 AM