建立网站的公司有哪些,wordpress 恶意,顺德网页制作公司,做非经营网站需要营业执照以前搭建的webservice 都是基于.NET fromwork的#xff0c;我们知道.NET fromwork是非跨平台的#xff0c;只能部署在iis上#xff0c;今天教大家用.NET core搭建一个可跨平台的Web Service
新建一个.net core空项目 给项目起一个名字 选一个.net框架#xff0c;我这里选…以前搭建的webservice 都是基于.NET fromwork的我们知道.NET fromwork是非跨平台的只能部署在iis上今天教大家用.NET core搭建一个可跨平台的Web Service
新建一个.net core空项目 给项目起一个名字 选一个.net框架我这里选择的是 .NET 5也可以选择.NET 6 7... 都是一样的 .NET 5会生成一个Startup类.NET 6以上版本已经把Startup类取消了直接把相关服务写在Program里面就行 依赖项 添加 NuGet程序包搜索 soapcore 安装 在Service文件夹下添加一个接口和一个实现类 IContract
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Threading.Tasks;namespace WebServiceDemo.Service.Interface
{[ServiceContract]interface IDemoService{[OperationContract]int Add(string a, string b);}
}DemoService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebServiceDemo.Service.Interface;namespace WebServiceDemo.Service
{public class DemoService : IDemoService{public int Add(string a, string b){return Convert.ToInt32(a) Convert.ToInt32(b);}}
}Startup下添加如下代码注入刚才的类作为单例服务模式同时添加soapcore服务 public void ConfigureServices(IServiceCollection services){services.TryAddSingletonIDemoService, DemoService();services.AddSoapCore();}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IWebHostEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseRouting();app.UseEndpoints(endpoints {endpoints.MapGet(/, async context {await context.Response.WriteAsync(Hello World!);});endpoints.UseSoapEndpointIDemoService(/Service.asmx, new SoapEncoderOptions(),SoapSerializer.DataContractSerializer);});}
启动项目可以看到已经成功运行了webservice 用postman测试一下测试成功 打包发布到服务器
右键 》发布》 选择文件夹 将发布好的文件全部拷贝到对应服务器下 windows服务器的话运行WebServiceDemo.exe就行linux的话运行WebServiceDemo.dll文件 或者指定端口号运行