.NET Core 6中使用SoapCore创建SOAP Web服务遇参数转换错误求助
.NET Core 6 中使用 SoapCore 时的参数类型错误解决方法
问题描述
在.NET Core 6中使用SoapCore NuGet包创建SOAP Web服务时,运行代码出现错误:Argument 3: cannot convert from 'System.ServiceModel.BasicHttpBinding' to 'SoapCore.SoapEncoderOptions'
我的代码如下:
builder.Services.AddSoapCore(); builder.Services.TryAddSingleton<ISampleService, SampleService>(); app.UseSoapEndpoint<ISampleService>("/Service.asmx", new BasicHttpBinding(), SoapSerializer.XmlSerializer);
错误由new BasicHttpBinding()这一行引发。
参考的教程代码示例:
public class Startup { public void ConfigureServices(IServiceCollection services) { services.TryAddSingleton<ISampleService, SampleService>(); services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSoapEndpoint<ISampleService>("/Service.asmx", new BasicHttpBinding(), SoapSerializer.XmlSerializer); } }
问题原因
参考教程使用的是旧版本的SoapCore,而当前安装的是新版本SoapCore。新版本的UseSoapEndpoint方法参数已变更,不再接受BasicHttpBinding类型,转而需要SoapEncoderOptions类型作为第二个参数。
修复方案
将.NET Core 6中的代码修改为如下形式:
builder.Services.AddSoapCore(); builder.Services.TryAddSingleton<ISampleService, SampleService>(); // 替换BasicHttpBinding为SoapEncoderOptions app.UseSoapEndpoint<ISampleService>("/Service.asmx", new SoapEncoderOptions(), SoapSerializer.XmlSerializer);
额外说明
如果需要配置绑定相关参数(如消息大小、安全设置等),可通过SoapEncoderOptions或在AddSoapCore时添加配置项实现。
内容的提问来源于stack exchange,提问作者Jonas Muehleder
相关产品推荐
相关产品推荐

