简体   繁体   中英

Switching from an asmx to a svc web service

We upgraded some web services from asmx to WCF, I need to change the call to the web service in an application, the contract name and method names and signatures are the same. Is there any easy way to go from calling a asmx web service to a svc web service (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;
    }

I tried to change the web service address in the .config file and it gave me the error :

(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'..

So in the CreateWebQuery method I changed the :

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

to

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

The web service call returned :

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

I'm not familiar with WCF services, any help is appreciated.

Thanks.

what is the Binding you have used? make sure it is BasicHttpBinding - then you may not have to change anything in client

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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