繁体   English   中英

从asmx切换到svc Web服务

[英]Switching from an asmx to a svc web service

我们将一些Web服务从asmx升级到WCF,我需要在应用程序中更改对Web服务的调用,合同名称,方法名称和签名相同。 从调用asmx Web服务到svc Web服务(WCF),有没有简便的方法?

     internal XmlDocument ServiceCall()
     {
        WebResponse reponseWeb = null;
        string strReponse = string.Empty;

        HttpWebRequest webRequest = this.CreateWebQuery();

        XmlDocument soapEnvelopeXml = this.CreerEnveloppeSoap();

        using (Stream stream = webRequest.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }

        XmlDocument xmlSoapRequest = new XmlDocument();

        try
        {
            reponseWeb = webRequest.GetResponse();
        }
        catch (System.Exception ex)
        {
            throw ex;
        }

        Stream str = reponseWeb.GetResponseStream();
        StreamReader sr = new StreamReader(str);

        xmlSoapRequest.Load(sr);
        return xmlSoapRequest;
     }

    private HttpWebRequest CreateWebQuery()
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(this.UrlServiceWeb);
        webRequest.Headers.Add("SOAPAction", "\"" + this.UrlHost + this.WCFNameContrat + "/" + this.MethodeWeb + "\"");
        webRequest.ContentType = "application/soap+xml; charset=utf-8";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }

    private XmlDocument CreerEnveloppeSoap()
    {
        XmlDocument soapEnvelop = new XmlDocument();

        string appelMethode = "<" + this.MethodeWeb + " xmlns=" + "\"" + this.UrlHote + this.WCFNomContrat + "\"" + ">";

        string strParametres = string.Empty;
        foreach (Parametre param in this.Parametres)
        {
            strParametres = strParametres + "<" + param.Nom + ">" + param.Valeur + "</" + param.Nom + ">";
        }

        appelMethode = appelMethode + strParametres + "</" + this.MethodeWeb + ">";

        StringBuilder sb = new StringBuilder(_enveloppeSoap);
        sb.Insert(sb.ToString().IndexOf("</soap12:Body>"), appelMethode);

        // Get XML
        soapEnvelop.LoadXml(sb.ToString());
        return soapEnvelop;
    }

我试图更改.config文件中的Web服务地址,但出现了以下错误:

(415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..

因此,在CreateWebQuery方法中,我更改了:

 webRequest.ContentType = "application/soap+xml; charset=utf-8" 

 webRequest.ContentType from "text/xml;charset=utf-8"

Web服务调用返回:

The remote server returned an error: (400) Bad Request.

我对WCF服务不熟悉,不胜感激。

谢谢。

您使用的绑定是什么? 确保它是BasicHttpBinding-那么您可能不必在客户端中进行任何更改

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM