看看.net core中的WCF
DotNetCore实战
共 3501字,需浏览 8分钟
·
2022-05-20 22:24
.net core下迎来了WCF——CoreWCF1.0,做了一个最初级的Demo,尝尝鲜。
首先在NuGet中引入下面两个包
CoreWCF.Http
CoreWCF.Primitives
本demo是用WCF实现一个ToDoList的几个服务端方法,然后用创建客户端服务调用,代码如下:
WCF服务配置文件
{
"Urls": "http://localhost:5000;https://localhost:5001",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
WCF服务代码:
using CoreWCF;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
using CoreWCF.Configuration;
using CoreWCF.Description;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel((context, options) =>
{
options.AllowSynchronousIO = true;
});
builder.Services.AddServiceModelServices().AddServiceModelMetadata();
builder.Services.AddSingleton
();
var app = builder.Build();
app.UseServiceModel(builder =>
{
builder
.AddService(typeof(ToDoService))
.AddServiceEndpoint
(new BasicHttpBinding(), "/ToDoService/basichttp") .AddServiceEndpoint
(new WSHttpBinding(SecurityMode.Transport), "/ToDoService/WSHttps"); });
var serviceMetadataBehavior = app.Services.GetRequiredService
(); serviceMetadataBehavior.HttpGetEnabled = true;
app.Run();
[ ]
public record Item
{
[ ]
public string? Title { get; set; }
[ ]
[ ]
public string? Description { get; set; }
[ ]
public DateTime CreateOn { get; set; }
[ ]
public bool IsComplete { get; set; } = false;
}
[ ]
public interface IToDoService
{
[ ]
bool Add(Item item);
[ ]
List
- GetList()
;
[ ]
bool Remove(string? title);
}
public class ToDoService : IToDoService
{
static List
- _list = new List
- ();
public bool Add(Item item)
{
item.CreateOn = DateTime.Now;
_list.Add(item);
return true;
}
public List
- GetList()
=> _list;
public bool Remove(string? title)
{
var item = _list.SingleOrDefault(s => s.Title == title);
if (item != null)
{
return _list.Remove(item);
}
else
{
return false;
}
}
}
创建调用项目,右键项目“添加”-“服务引用”,选择WCF Web Service,在启动WCF服务的情况下,执行下面图示:
引入ToDoService后会多一个Connected Services的文件夹,刚才一顿操作的产物。因为在服务端Item类是record类型,自动生成会把它变成class,这里需要手动改成record,方便输出时数据好看一些。
客户端代码,直接使用ToDoService:
using ToDoServiceReference;
var client = new ToDoServiceClient(ToDoServiceClient.EndpointConfiguration.WSHttpBinding_IToDoService, "https://localhost:5001/ToDoService/WSHttps");
while (true)
{
Console.WriteLine("1、Add ToDo 2、Get ToDoList 3、Remove ToDo");
switch (Console.ReadLine())
{
case "1":
var addResult = await client.AddAsync(new AddRequest
{
item = new Item { Title = "title_" + DateTime.Now.ToString("yyyyMMddHHmmss"), Description = "description_" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") }
});
Console.WriteLine(addResult.AddResult);
break;
case "2":
var listResult = await client.GetListAsync(new GetListRequest { });
foreach (var item in listResult.GetListResult)
{
Console.WriteLine(item);
}
break;
case "3":
var delResult = await client.RemoveAsync(new RemoveRequest
{
title = Console.ReadLine()
});
Console.WriteLine(delResult.RemoveResult);
break;
default:
Console.WriteLine("input error");
break;
}
}
结果:
评论