سؤال

I have an issue with passing a stream type variable to a self hosted rest service.

Here is my code in client application

static void Main(string[] args)
        {
            ChannelFactory<IService> cf = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:8000");
            cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
            IService channel = cf.CreateChannel();
            string strQuery = "<?xml version=\"1.0\"?><wql host='192.168.1.115' username='domain\\sebastian' password='password' Type='powershell'><query id='0.' ns='root\\cimv2' devicetype='powershell'><![CDATA[select CSName from Win32_OperatingSystem]]></query></wql>";
            byte[] byteArray = Encoding.UTF8.GetBytes(strQuery);
            MemoryStream stream = new MemoryStream(byteArray);
            XmlDocument ResultSet = new XmlDocument();
            ResultSet = channel.postGeneralXMLDocument(stream);
            Console.Read();
        }

This eventually invokes the method "postGeneralXMLDocument" in self hosted service.

Here the "Stream strInput" doesn't carries the expected content.

[XmlSerializerFormat]
    public XmlDocument postGeneralXMLDocument(Stream strInput)
    {
        try
        {
            StreamReader sr = new StreamReader(strInput);
            String strRequest = sr.ReadToEnd();
            sr.Dispose();
            NameValueCollection qs = HttpUtility.ParseQueryString(strRequest);
            strQuery = qs["strQuery"];
        //Do something
        }
         catch (Exception Ex)
        {

        }
    }

And the interface is like this..

    [ServiceContract]
public interface IService
    {
        [XmlSerializerFormat]
        [OperationContract]
        [WebInvoke]
        XmlDocument postGeneralXMLDocument(Stream strInput);

    }
}

I was referring the following URL to build this

self host wcf rest service

If someone could help me to solve this, that would be great

Thanks & regards Sebastian

هل كانت مفيدة؟

المحلول

Probably you did not configure wcf correctly. WCF has 4 types of transfer and the default one is not streaming friendly.

Please check links above for more information. You should be able to get your project working after reading.

http://net-daylight.blogspot.com/2011/12/streaming-in-wcf.html http://bartwullems.blogspot.com/2011/01/streaming-files-over-wcf.html

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top