Your first C# Web Service

.NET平台与C#开发:从基础到企业级应用实战 现代软件开发中,.NET平台作为跨语言、跨平台的开发框架,通过CLR(公共语言运行时)和BCL(基础类库)提供强大的基础设施支持。其核心原理包括JIT编译、垃圾回收和类型安全机制,显著提升开发效率和运行时性能。在技术价值层面,.NET 6/7的统一SDK设计支持Web、微服务、云原生等多种应用场景,结合C# 10的语言特性如记录类型和最小API,能够快速构建高性能应用。特别是在企业开发中,依赖注入、配置管理和EF Core等组件,为电商、物流等系统提供稳定支持。本文通过容器化部署、性能调优等实战案例,深入解 阅读详情

Introduction

Creating your first web service is incredibly easy. In fact, by using the wizards in Visual Studio. NET you can have your first service up and running in minutes with no coding.

For this example I have created a service called MyService in the /WebServices directory on my local machine. The files will be created in the /WebServices/MyService directory.

A new namespace will be defined called MyService, and within this namespace will be a set of classes that define your Web Service. By default the following classes will be created:

Global (in global.asax)Derived from HttpApplication. This file is the ASP.NET equivalent of a standard ASP global.asa file.
WebService1 (in WebService1.cs)Derived from System.Web.Services.WebService. This is your WebService class that allows you to expose methods that can be called as WebServices.

There are also a number of files created:

AssemblyInfo.csContains version and configuration information for your assembly.
web.configDefines how your application will run (debug options, the use of cookies etc).
MyService.discoDiscovery information for your service.
WebService1.asmxYour WebService URL. Navigate to this file in a browser and you will get back a user-friendly page showing the methods available, the parameters required and the return values. Forms are even provided allowing you to test the services through the web page.
bin/MyService.dllThe actual WebService component. This is created when you build the service.

The class for your service that is created by default is called (in this case) WebService1, and is within the MyService namespace. The code is partially shown below.

namespace MyService
{
    ...
    /// <summary>
    ///    Summary description for WebService1.
    /// </summary>
    [WebService(Namespace="http://codeproject.com/webservices/",
	            Description="This is a demonstration WebService.")]
    public class WebService1 : System.Web.Services.WebService
    {
        public WebService1()
        {
            //CODEGEN: This call is required by the ASP+ Web Services Designer
            InitializeComponent();
        }

        ...
        
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

A default method HelloWorld is generated and commented out. Simply uncomment and build the project. Hey Presto, you have a walking talking WebService. 

A WebService should be associated with a namespace. Your Wizard-generated service will have the name space http://tempuri.org. If you compile and run the service as-is you'll get a long involved message indicating you should choose a new namespace, so we add the namespace, and the WebService description as follows:

[WebService(Namespace="http://codeproject.com/webservices/",
            Description="This is a demonstration WebService.")]
public class WebService1 : System.Web.Services.WebService
{
    ...

To test the service you can right click on WebService1.asmx in the Solution Explorer in Visual Studio and choose "View in Browser". The test page is shown below,

When invoked this returns the following:

Getting the demo application to run

If you downloaded the source code with this article then you will need to create a directory 'WebServices' in your web site's root directory and extract the downloaded zip into there. You should then have:

/WebServices
/WebServices/bin
/WebServices/WebService1.asmx
...

Navigating to http://localhost/WebServices/WebService1.asmx won't show you the WebService because you need to ensure that the webservice's assembly is in the application's /bin directory. You will also find that you can't load up the solution file MyService.sln. To kill two birds with one stone you will need to fire up the IIS management console, open your website's entry, right click on the WebServices folder and click Properties. Click the 'Create' button to create a new application the press OK. The /WebServices directory is now an application and so the .NET framework will load the WebService assembly from the /WebServices/bin directory, and you will be able to load and build the MyService.sln solution.

Extending the example

So we have a WebService. Not particularly exciting, but then again we haven't exactly taxed ourselves getting here. To make things slightly more interesting we'll define a method that returns an array of custom structures.

Within the MyService namespace we'll define a structure called ClientData:

    public struct ClientData
    {
        public String Name;
        public int    ID;
    }

and then define a new method GetClientData. Note the use of the WebMethod attribute in front of the method. This specifies that the method is accessible as a WebService method.

    [WebMethod]
    public ClientData[] GetClientData(int Number)
    {
        ClientData [] Clients = null;

        if (Number > 0 && Number <= 10)
        {
            Clients = new ClientData[Number];
            for (int i = 0; i < Number; i++)
            {
                Clients[i].Name = "Client " + i.ToString();
                Clients[i].ID = i;
            }
        }
        return Clients;
    }

If we compile, then navigate to the the .asmx page then we are presented with a form that allows us to enter a value for the parameter. Entering a non-integer value will cause a type-error, and entering a value not in the range 1-10 will return a null array. If, however, we manage to get the input parameter correct, we'll be presented with the following XML file:

It's that easy.

Caching WebServices

Often a WebService will return the same results over multiple calls, so it makes sense to cache the information to speed things up a little. Doing so in ASP.NET is as simple as adding a CacheDuration attribute to your WebMethod:

[WebMethod(CacheDuration = 30)]
public ClientData[] GetClientData(int Number)
{

The CacheDuration attribute specifies the length of time in seconds that the method should cache the results. Within that time all responses from the WebMethod will be the same.

You can also specify the CacheDuration using a constant member variable in your class:

private const int CacheTime = 30;	// seconds

[WebMethod(CacheDuration = CacheTime)]
public ClientData[] GetClientData(int Number)
{

Adding Descriptions to your WebMethods

In the default list of WebMethods created when you browse to the .asmx file it's nice to have a description of each method posted. The Description attribute accomplishes this.

[WebMethod(CacheDuration = 30,
 Description="Returns an array of Clients.")]
public ClientData[] GetClientData(int Number)
{

Your default .asmx page will then look like the following:

There are other WebMethod attributes to control buffering, session state and transaction support.

Deploying the WebService

Now that we have a WebService it would be kind of nice to allow others to use it (call me crazy, but...). Publishing your WebService on your server requires that your solution be deployed correctly. On the Build menu of Visual Studio is a "Deploy" option that, when first selected, starts a Wizard that allows you to add a Deployment project to your solution. This creates an installation package that you can run on your server which will create the necessary directories, set the correct parameters and copy over the necessary files.

This doesn't really give you an idea of what, exactly, is happening, so we'll deploy our MyService manually.

Deploying the application is done using the steps in Getting the demo application to run. We need to create a directory for our service (or use an existing directory) for our .asmx file, and we need to have the service's assembly in the application's bin/ directory. Either place the .asmx file in a subdirectory on your website and place the assembly in the /bin folder in your website's root, or place the /bin in the subdirectory containing the .asmx file and mark that directory as an application (see above).

If you choose to create a separate directory and mark it as an application then Within this directory you need to add the following files and directories:

MyService.asmxThis file acts as the URL for your service
MyService.discoThe discovery document for your service
web.configConfiguration file for your service that overrides default web settings (optional).
/binThis directory holds the assembly for your service
/bin/MyService.dllThe actual service asembly.
 
C#/.NET开发者系统学习指南:从语言基础到架构实战 C#作为面向对象的编程语言,其核心在于类型安全、内存管理和丰富的语法特性,为构建企业级应用提供了坚实基础。在.NET平台上,通过公共语言运行时(CLR)实现跨语言互操作和自动内存管理,结合ASP.NET Core等框架,开发者能够高效构建高性能的Web服务。掌握C#异步编程、LINQ查询和依赖注入等技术,不仅能提升代码的可维护性和性能,还能应对微服务、云原生等现代架构需求。本文聚焦于一套整合了C#语言核心、.NET平台框架、Web开发实战及架构设计模式的系统性学习资料,通过涵盖Entity Framewor 阅读详情

相关推荐

企业级OA系统现代化演进:从Web架构到核心模块实战解析

企业级办公自动化(OA)系统是现代企业数字化转型的核心基础设施,其核心在于通过流程引擎、信息门户和协同工具实现人、事、物、流程的数字化。从技术原理层面,这类系统通常采用前后端分离架构,前端聚焦于用户体验与权限控制,后端则需保障高并发、高可用与数据安全。其技术价值体现在提升运营效率、强化决策质量与实现复杂组织权限管理。在实际应用场景中,系统需处理工作流引擎集成、消息通知中心构建以及与企业微信、钉钉等第三方平台的深度对接。本文以典型项目为例,深入探讨了在实现OA门户与飞书等平台集成时,如何通过API-First

dicha7140的博客 448

Web Service核心解析:从SOAP到RESTful API的设计与Spring Boot实战

Web Service是软件系统间通过互联网进行标准化通信的核心技术,其核心价值在于实现跨平台、跨语言的互操作性。从技术原理上看,它主要遵循两种主流架构风格:基于XML协议栈、强调严谨规范的SOAP/WSDL,以及基于HTTP协议、以资源为中心的RESTful架构。在工程实践中,RESTful API因其轻量、高效和对Web友好的特性,已成为构建公开API、微服务通信和移动后端的首选方案。本文聚焦于使用Spring Boot框架快速实现一个健壮的RESTful Web Service,涵盖从API设计、统一

weixin_33896726的博客 318

HyperLedger Fabric实战(一):基础环境构建

简介 本文将介绍从一台裸机上面如何构建出一个可用的Fabric网络(单机版,非测试网络)。 基础环境构建 Fabric组件安装

yindarui 1万+

Code Project - Your First C# Web Service

原文连接:http://www.codeproject.com/cs/webservices/myservice.asp作者:Chris Maunder Introduction Creating your first web service is incredibly easy. In fact, by using the wizards in Visual Studio. NET you c...

weixin_30588827的博客 143

深入分析RAG知识库多模态数据预处理

多模态企业数据预处理是构建高质量RAG知识库的关键环节。本文详解了处理流程:1)文档格式识别与路由;2)多模态内容提取(文本、图像、音视频);3)结构解析与语义重建;4)数据清洗与增强;5)智能分块(强调语义完整性);6)元数据注入。推荐使用开源工具如PaddleOCR、Whisper、BLIP-2等,或商业API处理复杂场景,最终生成带丰富元数据的结构化内容,为后续向量化检索奠定基础。

qq_35354529的博客 1379

Qwen3.5在昇腾910B上的vLLM Ascend高性能部署实战

大语言模型推理优化是AI工程落地的核心环节,其本质是计算、内存与通信三者的协同调度。vLLM作为主流推理框架,通过PagedAttention和Continuous Batching显著提升GPU利用率;而昇腾NPU凭借高带宽HBM与专用AI算子,在国产化算力平台中展现出独特优势。当vLLM深度适配昇腾(即vLLM Ascend),不仅保留动态批处理、流式输出等关键能力,更实现KV Cache的HBM感知管理与HCCL直通优化,使中小尺寸模型(如Qwen3.5-35B)在单台Atlas A3服务器上达成百并

weixin_30558697的博客 431

Web API C# (Furion版)带 单元测试

這是官網主要做的是一個後端API接口程序,給前端訪問。用C#,ASP .net開發,框架用了Furion。

Aaron121314的博客 265

企业微信API实战:解密external_userid的跨应用一致性管理

本文深入探讨企业微信API中external_userid的跨应用一致性管理问题,解析其本质及在不同应用场景下的ID不一致挑战。通过ID转换与统一管理方案,结合unionid实现跨应用客户数据整合,提供实战代码示例和架构设计,助力企业构建高效的客户管理系统。

weixin_29258297的博客 637

跨平台.NET应用UI组件DevExpress XAF v22.1 - 增强Web API Service

在新版中DevExpress XAF的Blazor、Web API Service及WinForms (.NET Core)程序集等默认面向.NET 6,欢迎下载最新版体验!

需要界面开发、IDE工具研发中文教程资料的小伙伴,记得私信我~ 1055

YOLOv11 + TensorRT 在 Jetson 设备上的实时部署

YOLOv11 + TensorRT 在 Jetson 设备上的实时部署

p6666666668的博客 1588

23、管状剪纸结构的研究与功能生成连杆机构的综合设计

本文研究了管状剪纸结构的压缩运动特性,通过剪纸技术将非刚性可折叠的Yoshimura和Kresling折纸结构转化为刚性可折叠结构,并提出径向可闭合剪纸(RC-kiri)构型。推导了位移与折叠角之间的数学关系,设计了带外部LET接头的RC-kiri模型,建立了力-位移理论模型并通过Abaqus进行有限元验证,制作了3D打印物理模型以测量直径变化。同时,研究了四杆和六杆功能生成连杆机构的综合设计方法,基于负闭合运动链和最小二乘法实现函数生成。结果表明理论、仿真与实验整体吻合良好,误差来源得到分析,为机器人、机

nokia的博客 167

C#.Net MVC_02_EF框架(Code First)环境搭建

C# .NET MVC 常用框架 —— EF框架(Code  First)环境搭建   框架了解 ORM框架 ORM框架是对于使用了ORM(Object Relational Mapping:对象关系映射)技术的框架的一种统称。其用来把对象模型表示的对象关系映射到基于SQL的关系模型数据库结构中去。如此,在操作实体对象的同时,就不需要复杂的SQL语句,直接可以以操作实体对象的属性和方法。O...

新青年1号 1973

ansys license许可证问题:Ansys License Manager Release 2024 R1...如何解决?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 5369

C# 编写Windows Service(windows服务程序)

Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序。Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就能开始运行了,它必须有特定的启动方式。这些启动方式包括了自动启动和手动启动两种。对于自动启动的Windows服务程序,它们在Windows启动或是重启之后用户登录之前就开始执行了...

a022756的博客 364

《C/C++等级考试(1~8级)历届真题解析》专栏总目录

中国电子学会《全国青少年软件编程等级考试》C/C++编程(1~8级)历届真题解析。订阅后可阅读专栏内所有真题解析,真题持续更新中!

人生就是一个不断学习的过程 2万+

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

C#+ EF + Oracle 采用 Database First开发出现的错误

C#+EF+Oracle 进行Database First (数据优先) 方式开发,如果设置不当,可能会造成莫名的错误。指定的存储区提供程序在配置中找不到,或者无效错误现象程序调试运行时出现以下错误“System.ArgumentException”类型的未经处理的异常在 System.Data.Entity.dll 中发生其他信息: 指定的存储区提供程序在配置中找不到,或者无效。异常:已引发: ...

mystonelxj的博客 1万+

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Intro to ASP.NET MVC 3 (C#)

Tweet This tutorial will teach you the basics of building an ASP.NET MVC Web application using Microsoft Visual Web Developer 2010 Express Service Pack 1, which is a free version of Microsoft Visua

wilsonke的专栏 1077

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

C#/.NET 系统学习指南:从核心语法到项目实战的完整路径

C# 是一种面向对象的现代编程语言,运行于 .NET 平台之上,其核心原理在于通过公共语言运行时(CLR)提供内存管理、安全性和跨语言互操作性。掌握 C# 和 .NET 技术栈对于构建企业级应用具有重要价值,尤其在 Web 服务、桌面应用和云原生开发等场景中应用广泛。本文聚焦于一套整合了 **高薪实战课程** 的系统性学习资料,该资料覆盖了从 C# 语言基础、ASP.NET Core WebAPI 开发到 Entity Framework Core 数据访问等关键技术模块,旨在通过结构化的学习路径和配套的实

weixin_30617797的博客 575

windows Service 创建部署

windows Service 创建部署 Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序。Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就能开始运行了,它必须有特定的启动方式。这些启动方式包括了自动启动和手动启动两种。对于自动启动...

weixin_30472035的博客 112

Java Web开发环境搭建与Servlet入门指南

Java Web开发是现代企业级应用的基础技术栈,其核心在于理解HTTP请求-响应模型与Servlet容器的工作原理。通过配置JDK运行环境和Tomcat服务器,开发者可以快速搭建Java Web开发环境。Servlet作为处理Web请求的核心组件,遵循特定的生命周期管理,配合JSP技术实现动态页面渲染。在企业级开发中,合理的项目结构、代码规范和安全管理是保障系统稳定性的关键。掌握这些基础技术后,开发者可以平滑过渡到Spring Boot等现代框架,构建更复杂的分布式系统。

weixin_33979203的博客 547
上一篇: 开发基于ASP.NET WebService的图片验证码服务
下一篇: 秘笈一: prototype.js
flyxiaoye
博客等级 码龄19年 8粉丝 154原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值