【问题标题】:Parsing a Stream and a Parameter to a WCF operation contract through JSON通过 JSON 将 Stream 和 Parameter 解析为 WCF 操作协定
【发布时间】:2011-12-21 10:15:41
【问题描述】:

我必须调用 WCF 服务来转换图像。由于图像是一个流(可能很大),我想要一个方法,在该方法中我可以将一个流以及几个参数(图像转换信息)发送到一个方法。如何定义操作合约的方法签名?请注意,我正在进行 JSON 调用并使用 Http post 方法发送图像。这样我就没有创建 .net 代理的奢侈了。
如何调用以下 WCF 方法?还是有更好的方法来做到这一点? 例如。

        [OperationContract]
        [WebInvoke(UriTemplate = "/MyOperation", Method = "POST", RequestFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
Public CompositeType1 MyOperation(Stream image,CompositeType2 param){

}

【问题讨论】:

    标签: .net wcf stream


    【解决方案1】:

    第一件事是当您将 Stream 作为参数之一时,您不能有其他参数来更改您的方法,如下所示:

    [WebInvoke]
    public CompositeType1 MyOperation(Stream image)
    {}
    

    现在,为了让您将图像和复合类型对象都传递给该方法是可以实现的,但您需要将请求作为多部分表单数据发布。当您将请求作为多部分表单数据发布时,您将需要服务器端的解析器来解析流以为您提取适当的内容,即从流中提取图像和复合类型对象。已经有一个现有的多部分表单解析器供您下载,但您必须对其进行自定义以满足您的要求。我想这是一项复杂的任务。多部分表单解析的链接是here

    关于多部分表单数据的一些信息可以找到here

    我想最简单的方法是将图像上传与操作分开并单独进行。

    【讨论】:

      【解决方案2】:

      Rajesh 的答案不正确 您可以通过 Stream 获得额外的参数

      试试这个:

      [OperationContract]
      [WebInvoke(Method = "POST", UriTemplate = "/{pram1}/{pram2}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
      string ProcessData(string pram1, string pram2, Stream zipFile);
      

      看看这个链接WCF UriTemplate and POST data on msdn

      【讨论】:

        【解决方案3】:

        使用此代码:

        ServiceContract]
            public interface IRestServiceImpl
            {
                [OperationContract]
                [System.ServiceModel.Web.WebInvoke(Method = "GET",ResponseFormat=System.ServiceModel.Web.WebMessageFormat.Xml, BodyStyle =System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, UriTemplate = "xml/{id}")]
                string XMLData(string id);
                [OperationContract]
                [System.ServiceModel.Web.WebInvoke(Method = "GET", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json, BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, UriTemplate = "json/{id}")]
                string JSONData(string id);
            }
        
        
        
        
        public class RestServiceImpl : IRestServiceImpl
            {
                #region IRestService Members
                public string XMLData(string id)
                {
                    return "You Request Porduct" + ":"+id;
        
                }
                public string JSONData(string id)
                {
                    return "Yor Request Product" +":"+ id;
                }
                #endregion
            }
        

        【讨论】:

          【解决方案4】:

          也许你可以使用一个小技巧。您可以使用参数 byte[] 而不是 stream,然后您可以使用以下代码转换为流:
          Stream stream = new MemoryStream(yourByteArray);

          【讨论】:

            猜你喜欢
            • 2019-07-16
            • 1970-01-01
            • 1970-01-01
            • 2015-03-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多