REST服务 - 发布图像(流内容)和文件名 - 错误

时间:2012-11-16 06:02:56

标签: wcf rest post stream

根据要求,我需要一个休息WCF服务来从Iphone发布图像和文件名。我创建了Rest服务。但是,当我运行WCF服务时,我收到了以下错误

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.DataContractSerializerOperationBehavior
 contract: http://tempuri.org/:IService1 ----> System.InvalidOperationException: For request in operation PostImage to be a stream the operation must have a single parameter whose type is Stream.
   at System.ServiceModel.Dispatcher.StreamFormatter.ValidateAndGetStreamPart(MessageDescription messageDescription, Boolean isRequest, String operationName)
   at System.ServiceModel.Dispatcher.StreamFormatter.Create(MessageDescription messageDescription, String operationName, Boolean isRequest)
   at System.ServiceModel.Description.MessageContractExporter.ExportMessage(Int32 messageIndex, Object state)
   at System.ServiceModel.Description.MessageContractExporter.ExportMessageContract()
   at System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext contractContext)
   at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
   --- End of inner ExceptionDetail stack trace ---
   at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
   at System.ServiceModel.Description.WsdlExporter.CallExportContract(WsdlContractConversionContext contractContext)
   at System.ServiceModel.Description.WsdlExporter.ExportContract(ContractDescription contract)
   at System.ServiceModel.Description.WsdlExporter.ExportEndpoint(ServiceEndpoint endpoint, XmlQualifiedName wsdlServiceQName, BindingParameterCollection bindingParameters)
   at System.ServiceModel.Description.WsdlExporter.ExportEndpoints(IEnumerable`1 endpoints, XmlQualifiedName wsdlServiceQName, BindingParameterCollection bindingParameters)
   at System.ServiceModel.Description.ServiceMetadataBehavior.MetadataExtensionInitializer.GenerateMetadata()
   at System.ServiceModel.Description.ServiceMetadataExtension.EnsureInitialized()
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.InitializationData.InitializeFrom(ServiceMetadataExtension extension)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.GetInitData()
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.TryHandleDocumentationRequest(Message httpGetRequest, String[] queries, Message& replyMessage)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.ProcessHttpRequest(Message httpGetRequest)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.Get(Message message)
   at SyncInvokeGet(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

我使用了以下代码

[OperationContract]
   [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare,
                    UriTemplate = "PostImage/{filename}",
                    Method = "POST")]
  string PostImage(string filename, Stream obj);


  public string PostImage(string filename, Stream obj)
  {
  }

使用以下web.config

<system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingStreamed" crossDomainScriptAccessEnabled="true" transferMode="Streamed" maxBufferSize="2147483647"  maxReceivedMessageSize="2147483647" openTimeout="00:25:00" closeTimeout="00:25:00" sendTimeout="00:25:00" receiveTimeout="00:25:00"></binding>
      </webHttpBinding>
    </bindings>

    <services>
      <service name="Test.Service1" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" 
                  binding="webHttpBinding" 
      contract="Test.IService1" 
      behaviorConfiguration="webHttp"
      bindingConfiguration="webHttpBindingStreamed"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttp" >
          <webHttp automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>


    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

我正在努力解决这个错误。 任何人都对这个错误有所了解。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

此博客解释了为什么您的REST服务中可能会抛出此错误。 http://blogs.msdn.com/b/hongmeig/archive/2010/03/02/how-to-solve-system-invalidoperationexception-for-request-in-operation-test-to-be-a-stream-the-operation-must-have-a-single-parameter-whose-type-is-stream.aspx

[来自博客的片段] 如果这是一个REST端点,那么实际上支持这种编程模型,因为REST有自己的格式化程序,可以正确处理多个参数。但是,如果您继续看到此问题,请确保您的WebHttpBehavior设置正确。没有正确设置该行为的可能原因可能是:

  1. 您正在使用自定义行为,其端点没有与WCF合同匹配的正确合同名称;

  2. 您没有使用WebEndpoint或WebServiceHost;

  3. 您还可以通过在绑定的TransferMode中选择“缓冲”选项来解决此问题。但这似乎正在打败流媒体的整个目的。

    问题是:您的服务是否为WebServiceHost主机?