C#创建WebService接口并连接的全过程

作者:李公子lm 时间:2023-04-07 07:47:21 

创建WebService项目

首先安装下.NET Framework4.6.2-4.7.1开发工具。

C#创建WebService接口并连接的全过程

然后就是新建 ASP.NET Web应用程序 项目。

C#创建WebService接口并连接的全过程

输入项目名称WebServiceDemo

C#创建WebService接口并连接的全过程

选择空,然后先去掉HTTPS配置。

C#创建WebService接口并连接的全过程

项目创建好之后,开始添加asmx文件.

C#创建WebService接口并连接的全过程

添加好之后在添加一个有参数的名为Hello的方法。代码如下图。

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

namespace WebServiceDemo
{
   /// <summary>
   /// WebService1 的摘要说明
   /// </summary>
   [WebService(Namespace = "http://tempuri.org/")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   [System.ComponentModel.ToolboxItem(false)]
   // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
   // [System.Web.Script.Services.ScriptService]
   public class WebService1 : System.Web.Services.WebService
   {

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

然后就可以直接启动了,也可以发布到IIS中启动。这里先发布到IIS,一会在新建一个控制台项目用于连接到该服务。

发布好之后在IIS中添加网站,并绑定端口号为81.然后就可以启动了。

直接启动的话可能会报下面的错误,这是因为没有设置起始页。

C#创建WebService接口并连接的全过程

可以直接输入地址访问。

http://localhost:81/webservice1.asmx

也可以在IIS默认文档中添加webservice1.asmx文件。下次在浏览就可以直接打开了。

C#创建WebService接口并连接的全过程

出现下图的页面,就表示服务已经部署成功了。

C#创建WebService接口并连接的全过程

连接到WebService服务

新建一个控制台应用。

然后打开webservice地址输入。

http://localhost:81/webservice1.asmx?wsdl

会打开一个xml文件。

C#创建WebService接口并连接的全过程

接着右键文件另存为,把文件保存下来。并修改文件后缀名为wsdl

C#创建WebService接口并连接的全过程

在VS中添加,添加服务引用。选择WCF Web Service。

C#创建WebService接口并连接的全过程

这里其实可以直接输入WebService的地址点击转到即可。当考虑到要连接的服务在本地不一定是可以访问的,所以我们可以点击浏览通过上面生成的wsdl文件来生成对应的代码。

C#创建WebService接口并连接的全过程

添加进来后如下图所示,命名空间可以按照实际名称修改。

C#创建WebService接口并连接的全过程

之后点击下一步,然后点击完成即可。

C#创建WebService接口并连接的全过程

完成之后这里就多了两个文件。

C#创建WebService接口并连接的全过程

调用方式如下,直接实例化对应的类,然后就可以像调用普通方法一样,调用远程的服务接口了。

using ServiceReference1;
using System;
using System.Threading.Tasks;

namespace TestProject
{
   public class Program
   {
       static async Task Main(string[] args)
       {
          await Test();
       }
       public static async Task Test()
       {
           var reference = new WebService1SoapClient(WebService1SoapClient.EndpointConfiguration.WebService1Soap12);
           var helloWorldResult = await reference.HelloWorldAsync();
           Console.WriteLine(helloWorldResult.Body.HelloWorldResult);
           var str = "张三";
           var helloResult = await reference.HelloAsync(str);
           Console.WriteLine(helloResult.Body.HelloResult);
       }
   }

}

返回结果如下,就像调用本地方法一样自然。

C#创建WebService接口并连接的全过程

不过这里应该有地方需要按需修改一下,在Reference.cs文件中,远程服务地址是写死的。所以需要改成参数。

private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
       {
           if ((endpointConfiguration == EndpointConfiguration.WebService1Soap))
           {
               return new System.ServiceModel.EndpointAddress("http://localhost:81/webservice1.asmx");
           }
           if ((endpointConfiguration == EndpointConfiguration.WebService1Soap12))
           {
               return new System.ServiceModel.EndpointAddress("http://localhost:81/webservice1.asmx");
           }
           throw new System.InvalidOperationException(string.Format("找不到名称为“{0}”的终结点。", endpointConfiguration));
       }

改造方法也简单。添加一个url的入参。

private static System.ServiceModel.EndpointAddress GetEndpointAddress(string url,EndpointConfiguration endpointConfiguration)
   {
       if ((endpointConfiguration == EndpointConfiguration.WebService1Soap))
       {
           return new System.ServiceModel.EndpointAddress(url);
       }
       if ((endpointConfiguration == EndpointConfiguration.WebService1Soap12))
       {
           return new System.ServiceModel.EndpointAddress(url);
       }
       throw new System.InvalidOperationException(string.Format("找不到名称为“{0}”的终结点。", endpointConfiguration));
   }

以及引用这个方法的这里都加上url。

public WebService1SoapClient(string url, EndpointConfiguration endpointConfiguration) :
           base(WebService1SoapClient.GetBindingForEndpoint(endpointConfiguration), WebService1SoapClient.GetEndpointAddress(url,endpointConfiguration))
   {
       this.Endpoint.Name = endpointConfiguration.ToString();
       ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
   }

调用的时候把Url传进去即可。

var url = "http://localhost:81/webservice1.asmx";
var reference = new WebService1SoapClient(url, WebService1SoapClient.EndpointConfiguration.WebService1Soap12);
var helloWorldResult = await reference.HelloWorldAsync();
Console.WriteLine(helloWorldResult.Body.HelloWorldResult);
var str = "张三";
var helloResult = await reference.HelloAsync(str);
Console.WriteLine(helloResult.Body.HelloResult);

have a wonderful day。

总结 

来源:https://blog.csdn.net/u012869793/article/details/128341811

标签:c#,创建,webservice接口
0
投稿

猜你喜欢

  • 深入聊一聊JDK中的Map和Set

    2023-10-21 15:54:20
  • eclipse报错 eclipse启动报错解决方法

    2023-04-28 08:43:49
  • RocketMQ源码解析broker 启动流程

    2022-12-25 10:50:54
  • Spring Boot接口幂等插件用法示例解析

    2022-04-29 16:47:11
  • Springboot日志开启SLF4J过程解析

    2022-04-23 01:29:57
  • android编程实现对话框的封装实例

    2022-02-12 12:58:54
  • C#装箱和拆箱的原理介绍

    2022-06-18 02:33:17
  • IDEA安装阿里巴巴编码规范插件的两种方式详解(在线安装和离线安装)

    2022-07-23 19:18:54
  • IDEA集成JProfiler11可视化工具的详细流程(安装、集成、测试)

    2021-12-12 04:05:35
  • SpringBoot结合JWT登录权限控制的实现

    2023-10-06 04:54:22
  • Java实现多任务执行助手

    2023-05-17 14:15:30
  • C# WinForm国际化实现的简单方法

    2023-08-07 04:29:34
  • java常用工具类 Random随机数、MD5加密工具类

    2023-02-14 17:55:08
  • Spring Batch入门教程篇

    2022-02-15 01:53:01
  • 轻松理解Java面试和开发中的IoC(控制反转)

    2023-08-10 03:00:35
  • C# Dictionary的使用实例代码

    2021-07-29 14:02:02
  • C#实现基于XML配置MenuStrip菜单的方法

    2023-03-06 21:48:50
  • java多线程中的异常处理机制简析

    2021-11-18 01:54:39
  • Java冒泡排序及优化介绍

    2023-11-11 13:05:51
  • Android Compose 属性动画使用探索详解

    2022-08-07 11:06:57
  • asp之家 软件编程 m.aspxhome.com