0

根据要求,我需要一个休息 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>

我正在努力解决这个错误。任何人都对此错误有任何想法。提前致谢。

4

1 回答 1

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-操作必须有一个单一参数,其类型是stream.aspx

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

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

  2. 您没有使用 WebEndpoint 或 WebServiceHost;

您还可以通过在绑定的 TransferMode 中选择 Buffered 选项来解决此问题。但这似乎违背了 Streaming 的全部目的。

问题是:你的服务主机是WebServiceHost吗?

于 2012-11-17T02:22:30.357 回答