.NET core 搭建一个跨平台的 Web Service

以前搭建的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.TryAddSingleton<IDemoService, 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.UseSoapEndpoint<IDemoService>("/Service.asmx", new SoapEncoderOptions(),
        SoapSerializer.DataContractSerializer);
            });
        }

启动项目,可以看到已经成功运行了webservice

用postman测试一下,测试成功!

打包发布到服务器

右键 》发布》 选择文件夹 

将发布好的文件全部拷贝到对应服务器下

windows服务器的话运行WebServiceDemo.exe就行,linux的话运行WebServiceDemo.dll文件

或者指定端口号运行:

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2023-12-12 16:24:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-12 16:24:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-12 16:24:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-12 16:24:02       18 阅读

热门阅读

  1. 数据库基础DDL

    2023-12-12 16:24:02       41 阅读
  2. mac 安装anaconda和lightgbm

    2023-12-12 16:24:02       42 阅读
  3. TCP协议

    TCP协议

    2023-12-12 16:24:02      31 阅读
  4. YOLOv6 学习笔记

    2023-12-12 16:24:02       41 阅读
  5. Git 的基本概念和使用方式

    2023-12-12 16:24:02       34 阅读
  6. 光伏设计方案:实现清洁能源的未来

    2023-12-12 16:24:02       37 阅读
  7. Python基础知识学习

    2023-12-12 16:24:02       35 阅读
  8. Qt基础-修改Qt Creator界面字体

    2023-12-12 16:24:02       36 阅读
  9. 【Vue】使用moent转换GMT时间格式为北京时间

    2023-12-12 16:24:02       43 阅读
  10. 对接海康明眸门禁设备-查询人员信息

    2023-12-12 16:24:02       46 阅读
  11. 网络安全中的加解密问题

    2023-12-12 16:24:02       34 阅读
  12. ACWing week 3(C语言) 722.数字序列和它的和

    2023-12-12 16:24:02       38 阅读