首页 > 解决方案 > 来自 .NET Core 应用程序的 SOAP Web 服务调用

问题描述

我目前有以下代码

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace ProjectName.Services
{
    [ServiceContract(Namespace = "http://domainName/WS")]
    interface WS
    {
        [OperationContract()]
        string GetChart(string id, bool Small, string ChartType);
    }

    public class ChartService : IChartService
    {
        public void GetChart()
        {
            var binding = new BasicHttpBinding();
            var Uri = new Uri("http://siteUrl/ws/serviceName.asmx");
            var endpoint = new EndpointAddress(Uri);
            var channelFactory = new ChannelFactory<WS>(binding, endpoint);
            var serviceClient = channelFactory.CreateChannel();
            var result = serviceClient.GetChart("123456", true, "V");
            channelFactory.Close();
        }
    }
}

哪个(我认为)正确调用了 Web 服务,但是,它抛出了以下异常:

   "OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'GetChartResponse' and namespace 'http://domainName'. 
   Found node type 'Element' with name 'GetChartResponse' and namespace 'http://domainName/WS/'"

但是将 '/WS' 添加到命名空间(这是我尝试调用的服务的实际命名空间)会引发:

   "System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://domainName/WS/WS/GetChart.\n   
   at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()\n   at System.Web.Services.Protocols.SoapServerProtocol.Initialize()\n   
   at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)\n   
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)"

如何设置预期的命名空间或使其不将接口名称添加到 SOAPAction 标头?

标签: c#.netasp.net-coresoap

解决方案


推荐阅读