2017-07-07 16 views
-1

我们有一个ASP.NET Core系统,我们需要使用SOAP连接到另一个Web服务(我们从客户那里接收到了WSDL)。带SOAP服务的Dotnet内核

过去,我们应该使用Visual Studio中使用WCF选项的“添加服务引用”。

对于DOTNET核心项目,该选项是没有更多的可用,但有几个选项来获得相同的解决方案:

使用SvcUtil工具在命令行或在这里安装插件https://blogs.msdn.microsoft.com/webdev/2016/06/26/wcf-connected-service-for-net-core-1-0-0-and-asp-net-core-1-0-0-is-now-available/生成的.cs文件

两种解决方案都需要结合使用这些的NuGet包https://github.com/dotnet/wcf

所以我的问题:是否有另一种解决方案比使用WCF访问C#SOAP服务?

回答

2

您可以使用像Soap UI这样的工具来获取xml请求的实际格式。

然后,您可以使用WebRequest执行请求 - 类似下面的代码。据我所知,没有办法自动生成类,所以你必须自己去做序列化:

public async static Task<string> CallWebService(string webWebServiceUrl, 
           string webServiceNamespace, 
           string methodVerb, 
           string methodName, 
           Dictionary<string, string> parameters) { 
     const string soapTemplate = 
     @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" 
         xmlns:{0}=""{2}""> 
      <soapenv:Header /> 
      <soapenv:Body> 
       <{0}:{1}> 
        {3} 
       </{0}:{1}> 
      </soapenv:Body> 
     </soapenv:Envelope>"; 

     var req = (HttpWebRequest)WebRequest.Create(webWebServiceUrl); 
     req.ContentType = "text/xml"; //"application/soap+xml;"; 
     req.Method = "POST"; 

     string parametersText; 

     if (parameters != null && parameters.Count > 0) 
     { 
      var sb = new StringBuilder(); 
      foreach (var oneParameter in parameters) 
       sb.AppendFormat(" <{0}>{1}</{0}>\r\n", oneParameter.Key, oneParameter.Value); 

      parametersText = sb.ToString(); 
     } 
     else 
     { 
      parametersText = ""; 
     } 

     string soapText = string.Format(soapTemplate, 
         methodVerb, methodName, webServiceNamespace, parametersText); 

     Console.WriteLine("SOAP call to: {0}", webWebServiceUrl); 
     Console.WriteLine(soapText); 

     using (Stream stm = await req.GetRequestStreamAsync()) 
     { 
      using (var stmw = new StreamWriter(stm)) 
      { 
        stmw.Write(soapText); 
      } 
     } 

     var responseHttpStatusCode = HttpStatusCode.Unused; 
     string responseText = null; 

     using (var response = (HttpWebResponse)req.GetResponseAsync().Result) { 
      responseHttpStatusCode = response.StatusCode; 

      if (responseHttpStatusCode == HttpStatusCode.OK) 
      { 
       int contentLength = (int)response.ContentLength; 

       if (contentLength > 0) 
       { 
        int readBytes = 0; 
        int bytesToRead = contentLength; 
        byte[] resultBytes = new byte[contentLength]; 

        using (var responseStream = response.GetResponseStream()) 
        { 
         while (bytesToRead > 0) 
         { 
          // Read may return anything from 0 to 10. 
          int actualBytesRead = responseStream.Read(resultBytes, readBytes, bytesToRead); 

          // The end of the file is reached. 
          if (actualBytesRead == 0) 
           break; 

          readBytes += actualBytesRead; 
          bytesToRead -= actualBytesRead; 
         } 

         responseText = Encoding.UTF8.GetString(resultBytes); 
         //responseText = Encoding.ASCII.GetString(resultBytes); 
        } 
       } 
      } 
     } 
     return responseText; 
     //return responseHttpStatusCode; 
    }