【问题标题】:How to consume third party webservice through Wcf service SOAP with out service reference如何在没有服务引用的情况下通过 Wcf 服务 SOAP 使用第三方 Web 服务
【发布时间】:2015-06-25 17:14:39
【问题描述】:

我正在尝试在 WCF 服务中调用第三方 Web 服务。

我可以通过 WCF 服务在 (URL) 中添加服务引用来调用该服务,但是在这样做的同时,我从人们那里听说这不是正确的做法,当我用谷歌搜索这个时,通过channelfactory 我们可以调用服务。

但问题是它说合同 dll 应该在客户端和服务器之间共享,我没有得到。

任何机构都可以提供示例来调用服务而不添加服务引用。 http://www.codeproject.com/Tips/558163/Difference-between-Proxy-and-Channel-Factory-in-WC

如果我们在项目中添加服务引用会出现什么问题,有什么陷阱?

我确定我只需要调用 SOAP 服务

谢谢

【问题讨论】:

    标签: wcf soap wcf-binding


    【解决方案1】:

    您可以使用 HttpWebRequest 创建 Web 请求,然后传递描述整个 SOAP 请求的 XML 文档并获取响应。下面的代码是我在我的一个应用程序中使用的。我已经从中删除了一些特定于我的应用程序的部分,但你会明白的。

    public static string ProcessRequestSOAP()
            {
                string result = "";
                try
                {
                    XmlDocument soapEnvelop = new XmlDocument();
                    soapEnvelop.LoadXml(SOAPXML_Request); // This is the SOAP xml document that is generated when calling web service. You have to manually create this.
    
                    string url = webServiceURL; // External Web Service URL
                    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                    webRequest.ContentType = "text/xml;charset=\"utf-8\"";
                    webRequest.Accept = "text/xml";
                    webRequest.Method = "POST";
    
                    webRequest.Credentials = new NetworkCredential(userName,password); // Username/password to call web service if required
    
    
                    using (Stream stream = webRequest.GetRequestStream())
                    {
                        soapEnvelop.Save(stream);
                    }
    
                    //get response from server
                    WebResponse response = webRequest.GetResponse();
    
                    //read response stream
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        if (reader != null)
                        {
                            result = System.Xml.Linq.XDocument.Load(reader).ToString();
                        }
                        else
                        {
                            result = null;
                        }
                    }
                }
                catch (SoapException ex)
                {
                    result = new ExceptionXElement(ex, false).ToString();
                }
                catch (Exception ex)
                {
                    result = new ExceptionXElement(ex, false).ToString();
                }
                return result;
    
            }
    

    【讨论】:

    • 太迟回复了,我正在尝试这种方法,但是如何生成 SOAPXML_Request?你能帮我解决这个问题吗
    • @Chiranjeevi SOAPXML_Request 只是一个 XML 文档。那是由整个 SOAP 消息组成的输入 XML。当您从应用程序调用 Web 服务时,它会生成 XML SOAP 作为请求。您可以使用 Fiddler 等工具拦截它。如果您想使用我指定的技术,您需要构建这个 XML 文档。您可以使用普通的 XML 文档命名空间来创建它,就像生成任何其他 XML 文档一样。
    猜你喜欢
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    相关资源
    最近更新 更多