2013-05-09 131 views
0

我們將一些web服務從asmx升級到了WCF,我需要在應用程序中更改對Web服務的調用,合約名稱和方法名稱和簽名是相同的。 有沒有簡單的方法可以從調用一個asmx Web服務到一個svc Web服務(WCF)?從asmx切換到svc web服務

 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服務,任何幫助表示讚賞。

謝謝。

回答

0

什麼是您使用的綁定?確保它是BasicHttpBinding - 那麼你可能不需要改變任何東西在客戶端