Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# dotnetcore与SOAP服务_C#_Web Services_Wcf_Soap_.net Core - Fatal编程技术网

C# dotnetcore与SOAP服务

C# dotnetcore与SOAP服务,c#,web-services,wcf,soap,.net-core,C#,Web Services,Wcf,Soap,.net Core,我们有一个ASP.NET核心系统,需要使用SOAP连接到另一个Web服务(我们从客户那里收到了WSDL) 在过去,我们应该在VisualStudio中使用WCF选项来使用“添加服务引用” 对于dotnet核心项目,选项不再可用,但有几个选项可以获得相同的解决方案: 在命令行中使用SvcUtil或在此处安装插件以生成.cs文件 这两种解决方案都需要与这些nuget软件包结合使用 所以我的问题是:除了使用WCF访问C#中的SOAP服务之外,还有其他解决方案吗?您可以使用类似这样的工具来获取xml请求

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

在过去,我们应该在VisualStudio中使用WCF选项来使用“添加服务引用”

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

在命令行中使用SvcUtil或在此处安装插件以生成.cs文件

这两种解决方案都需要与这些nuget软件包结合使用

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

您可以使用类似这样的工具来获取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;
    }
public异步静态任务CallWebService(字符串WebServiceURL,
字符串webServiceNamespace,
字符串方法动词,
字符串methodName,
字典参数){
常量字符串soapTemplate=
@"
{3}
";
var req=(HttpWebRequest)WebRequest.Create(webserviceURL);
req.ContentType=“text/xml”;/“application/soap+xml;”;
请求方法=“POST”;
字符串参数text;
if(parameters!=null&¶meters.Count>0)
{
var sb=新的StringBuilder();
foreach(参数中的变量oneParameter)
sb.AppendFormat(“{1}\r\n”,oneParameter.Key,oneParameter.Value);
参数text=sb.ToString();
}
其他的
{
参数text=“”;
}
string soapText=string.Format(soapTemplate,
methodVerb、methodName、webServiceNamespace、parametersText);
WriteLine(“对:{0}的SOAP调用”,WebServiceURL);
Console.WriteLine(soapText);
使用(Stream stm=await req.GetRequestStreamAsync())
{
使用(var stmw=newstreamwriter(stm))
{
stmw.Write(soapText);
}
}
var responseHttpStatusCode=HttpStatusCode.未使用;
字符串responseText=null;
使用(var response=(HttpWebResponse)req.GetResponseAsync().Result){
responseHttpStatusCode=response.StatusCode;
if(responseHttpStatusCode==HttpStatusCode.OK)
{
int contentLength=(int)response.contentLength;
如果(contentLength>0)
{
int readBytes=0;
int bytesToRead=contentLength;
字节[]结果字节=新字节[contentLength];
使用(var responseStream=response.GetResponseStream())
{
while(bytesToRead>0)
{
//Read可能返回0到10之间的任何值。
int actualbytes Read=responseStream.Read(resultBytes,readBytes,bytesToRead);
//到达文件的末尾。
如果(实际字节数==0)
打破
readBytes+=实际字节读取;
bytesToRead-=实际字节数;
}
responseText=Encoding.UTF8.GetString(结果字节);
//responseText=Encoding.ASCII.GetString(结果字节);
}
}
}
}
返回响应文本;
//返回响应TTPStatuscode;
}

我能够通过使用WSDL.exe生成类(与传统WCF add服务引用的功能相同)、发送请求(就像WCF一样)、添加消息检查器以及在BeforeSendRequest方法中捕获生成的消息对象,然后取消WCF web请求来实现这一点(因为此请求无论如何都会失败,因为dotnet Core不支持它)。这将生成必要的对象供您发送

然后我创建了一个dotnet core HttpClient对象和相应的处理程序,并发送“WCF生成的”请求对象

如果这不够清楚,可能会得到一些代码示例