1、创建项目
2、通过NuGet安装SoapCore
3、创建服务接口及实现类
本项目会创建2个webservice服务文件用于测试,所以创建了2个model类和2个接口文件。
Model类:
[DataContract]
public class FactoryModel
{
/// <summary>
/// 工厂号
/// </summary>
[DataMember]
public string Fno { get; set; }
/// <summary>
/// 姓名
/// </summary>
[DataMember]
public string Name { get; set; }
}
[DataContract]
public class UserModel
{
/// <summary>
/// 工号
/// </summary>
[DataMember]
public string Sno { get; set; }
/// <summary>
/// 姓名
/// </summary>
[DataMember]
public string Name { get; set; }
}
接口:
[ServiceContract]
public interface IMesOperate
{
/// <summary>
/// 查询工厂信息
/// </summary>
/// <param name="fno">学号</param>
/// <returns>工厂信息</returns>
[OperationContract]
FactoryModel GetInfo(string fno);
/// <summary>
/// 添加工厂信息
/// </summary>
/// <param name="factory">工厂信息</param>
/// <returns>result</returns>
[OperationContract]
string AddInfo(FactoryModel factory);
}
[ServiceContract]
public interface IScadaOperate
{
/// <summary>
/// 查询学生信息
/// </summary>
/// <param name="sno">学号</param>
/// <returns>学生信息</returns>
[OperationContract]
UserModel GetInfo(string sno);
/// <summary>
/// 添加学生信息
/// </summary>
/// <param name="student">学生信息</param>
/// <returns>result</returns>
[OperationContract]
int AddInfo(UserModel student);
}
实现类:
public class MesService : IMesOperate
{
public FactoryModel GetInfo(string fno)
{
return new FactoryModel() { Fno = fno, Name = "第一工厂" };
}
/// <summary>
/// 添加工厂记录
/// </summary>
/// <param name="factory"></param>
/// <returns></returns>
public string AddInfo(FactoryModel factory)
{
return factory.Name;
}
}
public class ScadaService : IScadaOperate
{
public UserModel GetInfo(string sno)
{
return new UserModel() { Sno = sno, Name = "小红" };
}
/// <summary>
/// 添加学生信息
/// </summary>
/// <param name="student"></param>
/// <returns></returns>
public int AddInfo(UserModel student)
{
return 1;
}
}
4、修改Startup.cs
修改ConfigureServices函数
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
//下面为增加项
services.TryAddSingleton<IScadaOperate, ScadaService>();
services.TryAddSingleton<IMesOperate, MesService>();
services.AddSoapCore();
}
修改Configure函数,设置EndPoint。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//省略非关键代码...
app.UseSoapEndpoint<IScadaOperate>("/Scada/ScadaService.asmx", new BasicHttpBinding(), SoapSerializer.XmlSerializer);
app.UseSoapEndpoint<IMesOperate>("/Mes/MesService.asmx", new BasicHttpBinding(), SoapSerializer.XmlSerializer);
}
5、启动项目
7、创建net core测试项目
8、net core测试项目添加服务连接,以及通过NuGet安装SoapCore
填写webservice服务访问额地址,点击“转到”查看连接是否正常。
9、net core测试项目测试服务访问
class Program
{
static void Main(string[] args)
{
//正式使用client对象创建单例即可
ServiceReference1.ScadaOperateClient client = new ScadaOperateClient();
var response = client.GetInfoAsync("123");
var result = response.Result;
Console.WriteLine(JsonConvert.SerializeObject(result));
//正式使用client对象创建单例即可
ServiceReference2.MesOperateClient client2 = new MesOperateClient();
var response2 = client2.GetInfoAsync("123");
var result2 = response2.Result;
Console.WriteLine(JsonConvert.SerializeObject(result2));
Console.ReadLine();
}
}
10、创建framework测试项目
11、framework测试项目添加服务连接
12、framework测试项目测试服务访问
class Program
{
static void Main(string[] args)
{
//正式调用只需创建单例
WebServiceTest2.ScadaService.IScadaOperate service = new WebServiceTest2.ScadaService.IScadaOperate();
var s=service.GetInfo("123");
Console.WriteLine(JsonConvert.SerializeObject(s));
Console.ReadLine();
}
}
13、更改命名空间
若项目需要,需要更改默认的webservice命名空间,可以在接口类更改特性标签,如下图:
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/zouzh/article/details/124086104