4

浏览服务的 .svc 文件时出错。 InvalidOperationException:要使操作 MethodName 中的请求成为流,该操作必须具有类型为 Stream 的单个参数” 在服务合同中:

[OperationContract]
         [WebInvoke(RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,          
       BodyStyle = WebMessageBodyStyle.Bare,
      UriTemplate = "/upload?filename={filename}&objrid={objrid}")]
     string uploadfile(string filename, long objrid, Stream data);`

编译器不生成 .svc 文件。尽管此操作运行良好。但是该服务中的其他方法不起作用。我该如何解决这个问题?

在 web.config 中:服务标签:

  <service behaviorConfiguration="TransferServiceBehavior" name="namespace.Service">



    <endpoint address="/Service" behaviorConfiguration="webby" binding="webHttpBinding" contract="namespace.Icontract" />
<endpoint binding="basicHttpBinding" contract="namespace.Icontract" />

      </service>

行为配置:

 <behavior name="TransferServiceBehavior">
      <serviceMetadata httpGetEnabled="false"/>
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <serviceThrottling maxConcurrentCalls="500" maxConcurrentSessions="500" maxConcurrentInstances="500"/>
    </behavior>

绑定cinfig:

此操作工作正常,但同一服务合同中的其他操作不起作用:其他操作:

[OperationContract]
    [WebGet(RequestFormat = WebMessageFormat.Json,
      ResponseFormat = WebMessageFormat.Json,
      BodyStyle = WebMessageBodyStyle.WrappedRequest,
      UriTemplate = "/ProcessViewRequestMobile")]
     Stream ProcessDownload();

当我评论第一个操作时,第二个工作正常。我怎样才能使我的两种方法都起作用。我必须在同一个服务合同中添加这两种方法。提前致谢。

4

1 回答 1

1

Well, you are getting this error because the default WCF stream formatter does not support this. You are try. all within the same method. This is just not supported, even if it seemingly works somehow.

If this is not a REST endpoint, you can work around this issue by using MessageContract wrapping the multiple parameters into one class.

If this is a REST endpoint or a similar custom endpoint, this programming model is actually supported because REST and some other scenarios have their own formatter which handles multiple parameters correctly. However, if you continue to see this issue, then please make sure your WebHttpBehavior is set up correctly. You can also workaround this issue by choosing Buffered option in the TransferMode on the binding. But that seems to be defeating the whole purpose of the Streaming.

于 2012-04-26T15:42:26.017 回答