当前位置 : 主页 > 网络编程 > net编程 >

看看.net core中的WCF

来源:互联网 收集:自由互联 发布时间:2023-09-03
.net core下迎来了WCF——CoreWCF1.0,做了一个最初级的Demo,尝尝鲜。 首先在NuGet中引入下面两个包 CoreWCF.Http CoreWCF.Primitives 本demo是用WCF实现一个ToDoList的几个服务端方法,然后用创建客户

  .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<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>();

var app = builder.Build();
app.UseServiceModel(builder =>
{
builder
.AddService(typeof(ToDoService))
.AddServiceEndpoint<ToDoService, IToDoService>(new BasicHttpBinding(), "/ToDoService/basichttp")
.AddServiceEndpoint<ToDoService, IToDoService>(new WSHttpBinding(SecurityMode.Transport), "/ToDoService/WSHttps");
});

var serviceMetadataBehavior = app.Services.GetRequiredService<ServiceMetadataBehavior>();
serviceMetadataBehavior.HttpGetEnabled = true;

app.Run();

[DataContract]
public record Item
{
[DataMember]
public string? Title { get; set; }
[DataMember]
[AllowNull]
public string? Description { get; set; }
[DataMember]
public DateTime CreateOn { get; set; }
[DataMember]
public bool IsComplete { get; set; } = false;
}

[ServiceContract]
public interface IToDoService
{
[OperationContract]
bool Add(Item item);

[OperationContract]
List<Item> GetList();

[OperationContract]
bool Remove(string? title);
}

public class ToDoService : IToDoService
{
static List<Item> _list = new List<Item>();

public bool Add(Item item)
{
item.CreateOn = DateTime.Now;
_list.Add(item);
return true;
}

public List<Item> 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服务的情况下,执行下面图示:

看看.net core中的WCF_WCF

看看.net core中的WCF_看看.net core中的WCF_02

看看.net core中的WCF_看看.net core中的WCF_03

看看.net core中的WCF_WCF_04

看看.net core中的WCF_WCF_05

   引入ToDoService后会多一个Connected Services的文件夹,刚才一顿操作的产物。因为在服务端Item类是record类型,自动生成会把它变成class,这里需要手动改成record,方便输出时数据好看一些。

看看.net core中的WCF_看看.net core中的WCF_06

 客户端代码,直接使用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;
}
}

结果:

看看.net core中的WCF_WCF_07

  想要更快更方便的了解相关知识,可以关注微信公众号 

看看.net core中的WCF_看看.net core中的WCF_08

上一篇:管理者的4种类型
下一篇:没有了
网友评论