<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          看看.net core中的WCF

          共 3501字,需瀏覽 8分鐘

           ·

          2022-05-20 22:24

          .net core下迎來了WCF——CoreWCF1.0,做了一個最初級的Demo,嘗嘗鮮。

          首先在NuGet中引入下面兩個包

          CoreWCF.Http?

          CoreWCF.Primitives


          本demo是用WCF實現(xiàn)一個ToDoList的幾個服務端方法,然后用創(chuàng)建客戶端服務調用,代碼如下:

          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();
          [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 GetList();
          [OperationContract] 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; } }}


          創(chuàng)建調用項目,右鍵項目“添加”-“服務引用”,選擇WCF Web Service,在啟動WCF服務的情況下,執(zhí)行下面圖示:



          引入ToDoService后會多一個Connected Services的文件夾,剛才一頓操作的產物。因為在服務端Item類是record類型,自動生成會把它變成class,這里需要手動改成record,方便輸出時數(shù)據(jù)好看一些。


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

          結果:


          瀏覽 98
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  无码AV成人导航网站 | 牛牛视频在线观看 | 国产美女做爱A片是免费 | 四虎久久久国产无码精品 | 青娱乐国产视频分类极品 |