从asmx切换到svc web服务

本文关键字:svc web 服务 asmx | 更新日期: 2023-09-27 18:12:58

我们将一些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服务不太熟悉,希望你能帮助我。

谢谢。

从asmx切换到svc web服务

您使用的是什么绑定?确保它是BasicHttpBinding -然后你可能不需要改变任何在客户端