.NetCore 无法直接创建WebService 可以借助SoapCore来创建
1,创建项目
2,通过NuGet安装SoapCore
3,创建服务接口及实现类
[ServiceContract]
public interface ISampleService
{
[OperationContract]
string Ping(string s);
[OperationContract]
ComplexModelResponse PingComplexModel(ComplexModelInput inputModel);
[OperationContract]
void VoidMethod(out string s);
[OperationContract]
Task<int> AsyncMethod();
[OperationContract]
int? NullableMethod(bool? arg);
[OperationContract]
void XmlMethod(System.Xml.Linq.XElement xml);
}
public class SampleService : ISampleService
{
public string Ping(string s)
{
Console.WriteLine("Exec ping method");
return s;
}
public ComplexModelResponse PingComplexModel(ComplexModelInput inputModel)
{
Console.WriteLine("Input data. IntProperty: {0}, StringProperty: {1}", inputModel.IntProperty, inputModel.StringProperty);
return new ComplexModelResponse
{
FloatProperty = float.MaxValue / 2,
StringProperty = inputModel.StringProperty,
ListProperty = inputModel.ListProperty,
DateTimeOffsetProperty = inputModel.DateTimeOffsetProperty
};
}
public void VoidMethod(out string s)
{
s = "Value from server";
}
public Task<int> AsyncMethod()
{
return Task.Run(() => 42);
}
public int? NullableMethod(bool? arg)
{
return null;
}
public void XmlMethod(XElement xml)
{
Console.WriteLine(xml.ToString());
}
}
4,Startup种 设置EndPoint
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.TryAddSingleton<ISampleService, SampleService>();
services.AddMvc(x => x.EnableEndpointRouting = false);
services.AddSoapCore();
}
public void Configure(IApplicationBuilder app)
{
app.UseSoapEndpoint<ISampleService>("/Service.svc", new BasicHttpBinding(), SoapSerializer.DataContractSerializer);
app.UseSoapEndpoint<ISampleService>("/Service.asmx", new BasicHttpBinding(), SoapSerializer.XmlSerializer);
app.UseMvc();
}
}
5,启动项目后输入地址 localhost:5050/Service.asmx 就会显示描述内容
SoapCore 创建的webservice 没有可视化调用测试窗口,生成的wsdl 和asp.net 生成的有差别,如果需要适配 asp.net 生成的webService的wsdl ,可以通过复制原有的wsdl 内容 ,替换现有生成的wsdl ,操作如下
1,服务原有服务生成的wsdl 复制保存为old.wsdl ,放置在根目录
2,配置appsetting.json 文件增加如下内容
"FileWSDL": {
"UrlOverride": "",//
"WebServiceWSDLMapping": {
"Service.asmx": { // 需要适配原有服务的名称
"WsdlFile": "old.wsdl",//wsdl 文件名称
"SchemaFolder": "",
"WsdlFolder": "" // wsdl 存放目录 ,
}
}
3,Startup绑定时设置参数内容
// 用于支持老版wsdl 显示问题 , 映射原有webservice wsdl 文件内容
var settings = Configuration.GetSection("FileWSDL").Get<WsdlFileOptions>();
settings.AppPath = env.ContentRootPath; // The hosting environment root path
app.UseSoapEndpoint<TjHisItfInterfaceService>("/Service.asmx", new BasicHttpBinding(), SoapSerializer.XmlSerializer,false,null,settings);
此时服务生成的wsdl 就为老版wsdl 内容了
UrlOverride 可为空,该值会在生成wsdl文件时替换后 wsdl 最下方的请求地址 如果该值生成错误,虽然客户端可以加载出webservice方法但调用时就会出错,为空时默认替换为StartUp.cs 中终节点设置的服务名称