Invoking WCF service methods without having a proxy

落爺英雄遲暮 提交于 2019-12-01 23:48:32

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.

Larry

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!