【问题标题】:selfhost wcf service : how to pass stream variablesselfhost wcf 服务:如何传递流变量
【发布时间】:2014-06-03 12:55:33
【问题描述】:

我在将流类型变量传递给自托管休息服务时遇到问题。

这是我在客户端应用程序中的代码

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

这最终会调用自托管服务中的“postGeneralXMLDocument”方法。

这里的“Stream strInput”没有携带预期的内容。

[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)
        {

        }
    }

而且界面是这样的..

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

    }
}

我指的是以下 URL 来构建它

self host wcf rest service

如果有人能帮我解决这个问题,那就太好了

感谢和问候 塞巴斯蒂安

【问题讨论】:

  • 什么意思>"Stream strInput" 没有携带预期的内容。
  • 顺便说一句,您的主要方法中有很多不好的做法,请在此处查看 => msdn.microsoft.com/en-us//library/yh598w02.aspx
  • 问题出在我的代码本身。我已经解决了。对造成的不便表示歉意。感谢您提供信息。

标签: c# wcf self-hosting


【解决方案1】:

可能您没有正确配置 wcf。 WCF 有 4 种传输类型,默认的一种是不支持流式传输的。

请查看以上链接了解更多信息。阅读后您应该能够让您的项目工作。

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

【讨论】:

  • 问题出在我的代码本身。我已经解决了。很抱歉给您带来不便。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多