selfhost wcf service : how to pass stream variables

风流意气都作罢 提交于 2019-12-11 04:16:09

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/24015787/selfhost-wcf-service-how-to-pass-stream-variables

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