Invoking WCF service methods without having a proxy

前端 未结 3 1449
醉梦人生
醉梦人生 2021-01-23 02:31

Is there anyway I can invoke a WCF service without adding service reference or even having a proxy at all.

相关标签:
3条回答
  • 2021-01-23 02:46

    If you asks this, it means you might be interested in Dynamic Proxy Generation.

    Please have a look at this article. Several points might need to be discussed, but the idea is in here.

    This question might also help.

    0 讨论(0)
  • 2021-01-23 02:54

    Brief answer: No

    WCF is based on the very fundamental principle of having a proxy between the client and the service being called. You cannot "get around" this.

    You have your choice of creating a proxy using Add Service Reference, or creating it in code - but you need a proxy - no way around that.

    0 讨论(0)
  • 2021-01-23 03:10

    You can invoke the service using a HttpWebRequest. Example below:-

    private static XDocument CallSoapServiceInternal(string uri, string soapAction, string contentType, XDocument reqXml)
    {
        var req = (HttpWebRequest)WebRequest.Create(uri);
        req.ContentType = contentType;
        req.Method = "POST";
        req.Headers.Add("SOAPAction", soapAction);
        req.Credentials = CredentialCache.DefaultCredentials;
        req.Timeout = 20000;
        //req.Timeout = System.Threading.Timeout.Infinite;
    
        using (var reqStream = req.GetRequestStream())
        {
            reqXml.Save(reqStream);
        }
    
        string respStr;
    
        try
        {
            using (var resp = (HttpWebResponse)req.GetResponse())
            {
                using (var rdr = new StreamReader(resp.GetResponseStream()))
                {
                    respStr = rdr.ReadToEnd();
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error getting service response.", ex);
        }
    
        Console.WriteLine(respStr);
        Assert.IsTrue(respStr.Length > 0, "Nothing returned");
    
        var respXml = XDocument.Parse(respStr);
        return respXml;
    }
    
    0 讨论(0)
提交回复
热议问题