这里介绍C#建立Web Service,

下面,咱们看看怎么C#树立Web Service

C#树立Web Service(c# 表达式树)  C#建立Web Service 第1张

C#树立Web Service

1.在wwwroot目录下树立一个叫做WebService的目录。

2.树立下面这样一个文件:

  1. usingSystem;
  2. usingSystem.Web.Services;
  3. publicclassAddNumbers:WebService
  4. {
  5. [WebMethod]
  6. publicintAdd(inta,intb){
  7. intsum;
  8. sum=a+b;
  9. returnsum;
  10. }
  11. }

3.将这个文件保存为AddService.asmx(asmx是扩展名),保存到Webservice的目录中

4.现在咱们树立了Web服务,现已准备好客户端运用

5.现在,你可以用下面的URL拜访这个Web服务:
地址/Webservice/Addservice.asmx/Add?a=10&b=5成果将以XML格局回来


在客户机上布置这个服务

1.在指令行输入:
WSDL地址/WebService/MathService.asmx /n:NameSp /out:FileName.cs这个操作将树立一个称为FileName.cs的文件

阐明:WSDL 指的是WebServices Description Language ,这个程序在Program Files\Microsoft.NET\FrameworkSDK\Bin 目录中。

NameSp是咱们设置的姓名空间的姓名,将在后边布置这个服务的客户端的完成代码中运用到。

2.编译

CSC /t:library /r:system.web.dll /r:system.xml.dll FileName.cs

上述指令将生成一个dll文件,姓名便是上面的asmx文件中的公共类的姓名,在咱们的比如中,便是:AddNumbers.dll

3.将生成的dll文件放到布置机的wwwroot\bin目录中。

在布置机的asp/aspx 中调用这个Web Service

  1. <%@importNamespace="NameSp"%>
  2. <scriptlanguage="c#"runat="server">
  3. publicvoidPage_Load(objecto,EventArgse){
  4. intx=10;
  5. inty=5;
  6. intsum;
  7. //Instantiatingthepublicclassofthewebservice
  8. AddNumbersAN=newAddNumbers();
  9. sum=AN.Add(x,y);
  10. stringstr=sum.ToString();
  11. response.writeline(str);
  12. }
  13. </script>

以上介绍C#树立Web Service

【修改引荐】

  1. 浅谈C#开发WinForm
  2. C#静态结构函数简介
  3. C#完成ControlTemplate办法
  4. C#验证输入办法详解
  5. 浅析C#通明窗体
转载请说明出处
知优网 » C#树立Web Service(c# 表达式树)

发表评论

您需要后才能发表评论