开发者

Invoking WCF service methods without having a proxy

开发者 https://www.devze.com 2023-03-17 16:53 出处:网络
Is there开发者_运维技巧 anyway I can invoke a WCF service without adding service reference or even having a proxy at all.You can invoke the service using a HttpWebRequest. Example below:-

Is there开发者_运维技巧 anyway I can invoke a WCF service without adding service reference or even having a proxy at all.


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;
}


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消