Here is a simple example of a C# web service code:

using System;
using System.Web.Services;

namespace MyWebService
{
    [WebService(Namespace = "http://mycompany.com/webservices")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class MyService : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
}

This code defines a web service named MyService with two methods: HelloWorld and Add. The HelloWorld method simply returns the string "Hello World", while the Add method takes two integer parameters a and b, and returns their sum.

To test this web service, you can create a new ASP.NET web application project in Visual Studio, add a web reference to the service URL (e.g. http://localhost/MyWebService/MyService.asmx), and then call the methods from your code. For example:

MyService service = new MyService();
string result1 = service.HelloWorld();
int result2 = service.Add(2, 3);
Console.WriteLine(result1); // prints "Hello World"
Console.WriteLine(result2); // prints 5
``
c# webservice sample code

原文地址: https://www.cveoy.top/t/topic/f4pl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录