C#创建WebService接口并连接的全过程
作者:李公子lm 发布时间:2023-04-07 07:47:21
创建WebService项目
首先安装下.NET Framework4.6.2-4.7.1开发工具。
然后就是新建 ASP.NET Web应用程序 项目。
输入项目名称WebServiceDemo
选择空,然后先去掉HTTPS配置。
项目创建好之后,开始添加asmx
文件.
添加好之后在添加一个有参数的名为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.然后就可以启动了。
直接启动的话可能会报下面的错误,这是因为没有设置起始页。
可以直接输入地址访问。
http://localhost:81/webservice1.asmx
也可以在IIS默认文档中添加webservice1.asmx
文件。下次在浏览就可以直接打开了。
出现下图的页面,就表示服务已经部署成功了。
连接到WebService服务
新建一个控制台应用。
然后打开webservice地址输入。
http://localhost:81/webservice1.asmx?wsdl
会打开一个xml文件。
接着右键文件另存为,把文件保存下来。并修改文件后缀名为wsdl
。
在VS中添加,添加服务引用。选择WCF Web Service。
这里其实可以直接输入WebService的地址点击转到即可。当考虑到要连接的服务在本地不一定是可以访问的,所以我们可以点击浏览通过上面生成的wsdl文件来生成对应的代码。
添加进来后如下图所示,命名空间可以按照实际名称修改。
之后点击下一步,然后点击完成即可。
完成之后这里就多了两个文件。
调用方式如下,直接实例化对应的类,然后就可以像调用普通方法一样,调用远程的服务接口了。
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);
}
}
}
返回结果如下,就像调用本地方法一样自然。
不过这里应该有地方需要按需修改一下,在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


猜你喜欢
- 上篇文章给大家介绍了springboot全局字符编码设置解决乱码问题 感兴趣的朋友可以点击查看,下面通过两种方式给大家介绍Spri
- 一、线程间的共享1.1 ynchronized内置锁用处Java支持多个线程同时访问一个对象或者对象的成员变量关键字synchronized
- 一、User Agent的含义User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及
- 简介说明本文用实例来介绍@Autowired解决循环依赖的原理。@Autowired是通过 * 缓存来解决循环依赖的。 除了@Aut
- js和python是两种语言,js处理网页数据,python可作为服务端开发,两者通过websocket进行通信。websocket是soc
- Java序列化JSON时long型数值,会出现精度丢失的问题。原因:java中得long能表示的范围比js中number大,也就意味着部分数
- ViewPager这个小demo实现的是可以左右循环滑动图片,下面带索引,滑到最后一页在往右滑动就要第一页,第一页往左滑动就到最后一页,先上
- 讲这个例子前,咱们先来看一个简单的程序:字符串数组实现数字转字母:#include <stdio.h>#include <
- 本例子演示如何添加一个简单的单页导航,在此基础上,再演示如何在第2个页面中显示第1个页面中拨打过的所有电话号码。(1)通过该例子理解Andr
- 由于springboot常用war包部署,改为cloud开发模式多端口情况下,部署反而不习惯毕竟,war包要不要项目名访问都必须放在tomc
- 本文实例讲述了C#操作SQLite方法。分享给大家供大家参考。具体分析如下:地址:System.Data.Sqlite入手。。。首先impo
- 本文实例为大家分享了Android屏幕适配工具类的具体代码,供大家参考,具体内容如下DimenToolgithub地址Android 屏幕适
- 前言:我们在Android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的“Go”按键
- 大家好,这一篇博客来教大家一个类似于LED闹钟显示屏样式的小案例,UI比较美观,文末会提供下载相关资源地址供大家下载,首先我们来看一看这个案
- 下面通过一段内容有文字说明有代码分析,并附有展示图供大家学习。要解析HTTP报文,需要实现以下操作:读取HTTP报头提供的各种属性分析属性值
- 前言比较运算符用于判断两个数据的大小,例如:大于、等于、不等于。比较的结果是一个布尔值( true 或 false )。Java 中常用的比
- Spring之动态注册bean什么场景下,需要主动向Spring容器注册bean呢?如我之前做个的一个支持扫表的基础平台,使用者只需要添加基
- 前言 Spring中最重要的概念IOC和AOP,实际围绕的就是Bean的生成与使用。什么叫做Bean呢?
- 1. SpringBoot 配置文件1.1 配置文件的作用配置文件中配置了项目中重要的数据, 例如:数据库的连接信息 (用户名密码)项目的启
- C# Class写入Json/// <summary> /// 写入jso