locked
How to invoke WCF service? RRS feed

  • Question

  • I'm making owin middleware that needs to be able to invoke WCF Service. Is there any possibility to invoke WCF service with string/stream containing soap envelope without invoking any web requests? If it's not possible, then is it possible to make DataContract that'd be able to deserialize string containing soap envelope so I could invoke it manually? Service I have to implement is defined like this:

        [ServiceContract(Namespace = "http://schemas.microsoft.com/sharepoint/remoteapp/")]
        public interface IRemoteEventService
        {
            [OperationContract]
            SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties);
            [OperationContract(IsOneWay = true)]
            void ProcessOneWayEvent(SPRemoteEventProperties properties);
        }

    • Edited by Fiński Thursday, July 3, 2014 3:13 PM
    Thursday, July 3, 2014 3:09 PM

Answers

  • Hi,

    If you want to invoke the WCF Service with the soap envelope, maybe you can use the following:

    public class Program
    {
        public static void Main(string[] args)
        {
           
            string endpoint = "http://.....service.svc";
    
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
            request.ContentType = "text/xml"; // or application/soap+xml for SOAP 1.2
            request.Method = "POST";
            request.KeepAlive = false;
    
            //In case you have a proxy to resolve the server name also add these lines
            var proxyServer = new WebProxy("XX.XX.XX.XX", 1234);
            proxyServer.Credentials = CredentialCache.DefaultCredentials; // or username + password
            request.Proxy = proxyServer;
    
            // you can read these from files
            string payload = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">
                                   <soapenv:Header/>
                                   <soapenv:Body>
                                      <tem:Add>
                                         <tem:a>1</tem:a>
                                         <tem:b>2</tem:b>
                                      </tem:Add>
                                   </soapenv:Body>
                                </soapenv:Envelope>";
    
            byte[] byteArray = Encoding.UTF8.GetBytes(payload);
            request.ContentLength = byteArray.Length;
    
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(byteArray, 0, byteArray.Length);
            requestStream.Close();
    
            HttpWebResponse response = null;
            try
            {
                 response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                 response = (HttpWebResponse)ex.Response;
            }
    
            Console.WriteLine(string.Format("HTTP/{0} {1} {2}\n", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription));
    
            // you can write this to files
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            Console.WriteLine(reader.ReadToEnd());
    
            // cleanp
            reader.Close();
            requestStream.Close();
            responseStream.Close();
            response.Close();
        }
    }

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Friday, July 4, 2014 7:48 AM
  • The easiest way turned out to be using WebRequest to trigger remote owin service from WCF service (it's not perfect, but works and it's possible).
    • Marked as answer by Fiński Wednesday, July 9, 2014 12:42 AM
    Wednesday, July 9, 2014 12:42 AM

All replies

  • Hi,

    If you want to invoke the WCF Service with the soap envelope, maybe you can use the following:

    public class Program
    {
        public static void Main(string[] args)
        {
           
            string endpoint = "http://.....service.svc";
    
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
            request.ContentType = "text/xml"; // or application/soap+xml for SOAP 1.2
            request.Method = "POST";
            request.KeepAlive = false;
    
            //In case you have a proxy to resolve the server name also add these lines
            var proxyServer = new WebProxy("XX.XX.XX.XX", 1234);
            proxyServer.Credentials = CredentialCache.DefaultCredentials; // or username + password
            request.Proxy = proxyServer;
    
            // you can read these from files
            string payload = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">
                                   <soapenv:Header/>
                                   <soapenv:Body>
                                      <tem:Add>
                                         <tem:a>1</tem:a>
                                         <tem:b>2</tem:b>
                                      </tem:Add>
                                   </soapenv:Body>
                                </soapenv:Envelope>";
    
            byte[] byteArray = Encoding.UTF8.GetBytes(payload);
            request.ContentLength = byteArray.Length;
    
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(byteArray, 0, byteArray.Length);
            requestStream.Close();
    
            HttpWebResponse response = null;
            try
            {
                 response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                 response = (HttpWebResponse)ex.Response;
            }
    
            Console.WriteLine(string.Format("HTTP/{0} {1} {2}\n", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription));
    
            // you can write this to files
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            Console.WriteLine(reader.ReadToEnd());
    
            // cleanp
            reader.Close();
            requestStream.Close();
            responseStream.Close();
            response.Close();
        }
    }

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Friday, July 4, 2014 7:48 AM
  • That's a solution I can't use - served svc file is apparently hosted in the different app domain, so I can't use any data collected by owin middleware.

    I was thinking of using XmlSerializer to get an array of parameters and then DataContractSerializer to deserialize predefined types.

    What's the risk of that approach - what can I miss in terms of wcf configuration, authentication and overall operations behavior (are there any additional values I should be setting before invoking svc method directly)? For simplicity assume that I'm just mocking it for test cases.

    Friday, July 4, 2014 8:21 AM
  • The easiest way turned out to be using WebRequest to trigger remote owin service from WCF service (it's not perfect, but works and it's possible).
    • Marked as answer by Fiński Wednesday, July 9, 2014 12:42 AM
    Wednesday, July 9, 2014 12:42 AM