.NetCore创建WebService,并能适配原有客户端调用

.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 中终节点设置的服务名称

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值