带有SOAP服务的Dotnet核心 [英] Dotnet core with SOAP service

查看:63
本文介绍了带有SOAP服务的Dotnet核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a ASP.NET Core system and we need to connect to another webservice using SOAP (we received the WSDL from the customer).

过去,我们应该在Visual Studio中使用WCF选项使用添加服务引用".

In the past, we should have use the "add service reference" using WCF options in Visual Studio.

对于dotnet核心项目,这些选项不再可用,但是有几个选项可以得到相同的解决方案:

For dotnet core projects, the options is no more available but there are several options to get the same solutions:

在命令行中使用SvcUtil或在此处安装插件 https://blogs.msdn.microsoft.com/webdev/2016/06/26/wcf-connected-service-for-net-core-1-0-0和as-net-net-core-1-0-0-snow-available/生成.cs文件

Use SvcUtil in the command line or install the plugin here 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/ to generate the .cs files

这两个解决方案都需要与这些nuget软件包结合使用 https://github.com/dotnet/wcf

Both solutions need to be used in conjunction with these nuget packages https://github.com/dotnet/wcf

所以我的问题:除了使用WCF在C#中访问SOAP服务之外,还有其他解决方案吗?

So my question: Is there another solution than using WCF to access a SOAP service in C#?

推荐答案

您可以使用 Soap UI 获取xml请求的实际格式.

You can use a tool like Soap UI to get the actual format of the xml request.

然后,您可以使用WebRequest执行请求-类似于下面的代码.据我所知,无法自动生成类,因此您必须自己进行反序列化:

You can then execute the request using a WebRequest - something like the code below. As far as I know there is no way to auto-generate the classes so you would have to do the deserialization yourself:

    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;
    }

这篇关于带有SOAP服务的Dotnet核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆