Core 开发 wcf
测试工具:找到Vs的安装路径,找到Common7\IDE\WcfTestClient.exe
1.添加包
CoreWCF.Http 和 CoreWCF.Primitives 这里代码都是 1.0.1 版本
2.修改配置文件
在最前面添加 "Urls": "http://localhost:5000;https://localhost:5001",
3.1将服务协定实现在一个类里面,利用partial将实现分散到多个文件,然后添加多个终结点,只需要开启一个ServiceHost.
再core 6.0的开发
var builder = WebApplication.CreateBuilder(args); builder.WebHost.ConfigureKestrel((context, options) => { options.AllowSynchronousIO = true; }); // Add WSDL support builder.Services.AddServiceModelServices().AddServiceModelMetadata(); builder.Services.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>(); var app = builder.Build(); //1.将服务协定实现在一个类里面,利用partial将实现分散到多个文件,然后添加多个终结点,只需要开启一个ServiceHost. app.UseServiceModel(builder => { builder.AddService<MyWcfService>() .AddServiceEndpoint<MyWcfService, IEchoService>(new BasicHttpBinding(), "/WcfService/IEchoService") .AddServiceEndpoint<MyWcfService, IEMessService>(new BasicHttpBinding(), "/WcfService/IEMessService"); }); var serviceMetadataBehavior = app.Services.GetRequiredService<ServiceMetadataBehavior>(); serviceMetadataBehavior.HttpGetEnabled = true; app.Run();
调用网站:http://localhost:5000/WcfService/IEchoService
3.2.每个服务开启一个ServiceHost
using CoreWCF; using CoreWCF.Configuration; using CoreWCF.Description; using WebEmpty; var builder = WebApplication.CreateBuilder(args); builder.WebHost.ConfigureKestrel((context, options) => { options.AllowSynchronousIO = true; }); // Add WSDL support builder.Services.AddServiceModelServices().AddServiceModelMetadata(); builder.Services.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>(); var app = builder.Build(); //2.每个服务开启一个ServiceHost,这种做法在实际实现上也会有所不同,比如可以需要调用某个服务的时候再去开启,利用线程锁来控制同时开启的服务数,或者一次开启所有的服务. app.UseServiceModel(builder => { builder.AddService<EchoService>() .AddServiceEndpoint<EchoService, IEchoService>(new BasicHttpBinding(), "/WcfService/IEchoService") .AddService<EMessService>() .AddServiceEndpoint<EMessService, IEMessService>(new BasicHttpBinding(), "/WcfService/IEMessService"); }); var serviceMetadataBehavior = app.Services.GetRequiredService<ServiceMetadataBehavior>(); serviceMetadataBehavior.HttpGetEnabled = true; app.Run();
分别 调用网站:http://localhost:5000/WcfService/IEchoService 和 http://localhost:5000/WcfService/IEMessService