简体   繁体   English

如何在WCF Rest Service中将额外信息与数据流一起传递

[英]how to pass extra information along with data Stream in WCF Rest Service

What i wanted is to upload a file from client end to the server with the filename and the string using wcf service.But if i pass the filename i solution gets build without error but when i run the service i get the following error.But if the method contain only stream as parameter it works fine.I have googled but could find no luck.below are my contrac and config file: 我想要的是使用wcf service将文件名和字符串从客户端上传到服务器的文件。但是,如果我传递文件名,则解决方案会生成无错误,但是当我运行服务时,会出现以下错误。该方法只包含流作为参数,它工作正常。我已经用谷歌搜索但找不到运气。以下是我的控制和配置文件:

in contract file 在合同文件中

[WebInvoke(UriTemplate = "UploadFile/filename={filename}" ,BodyStyle = WebMessageBodyStyle.Bare)]
        void UploadFile(Stream fileContents,string filename);

config file: 配置文件:

<?xml version="1.0"?>
<configuration>
<system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1" behaviorConfiguration="ServiceBehaviour">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/"/>
          </baseAddresses>
        </host>
        <endpoint address="soap" binding="wsHttpBinding" contract="WcfServiceLibrary1.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="rest" binding="webHttpBinding" contract="WcfServiceLibrary1.IService1" behaviorConfiguration="restEndpointBehavior">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
      <behavior name="restEndpointBehavior">
        <webHttp/>
      </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

error: 错误:

System.InvalidOperationException: For request in operation UploadFile 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.Dispatcher.OperationFormatter..ctor(OperationDescription description, Boolean isRpc, Boolean isEncoded)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter..ctor(OperationDescription description, DataContractFormatAttribute dataContractFormatAttribute, DataContractSerializerOperationBehavior serializerFactory)
   at System.ServiceModel.Description.DataContractSerializerOperationBehavior.GetFormatter(OperationDescription operation, Boolean& formatRequest, Boolean& formatReply, Boolean isProxy)
   at System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
   at System.ServiceModel.Description.DispatcherBuilder.BindOperations(ContractDescription contract, ClientRuntime proxy, DispatchRuntime dispatch)
   at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)
   at System.ServiceModel.ServiceHostBase.InitializeRuntime()
   at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)

Is there a way to solve this problem.. 有没有办法解决这个问题。

Thanks in advance. 提前致谢。

If you have a Stream parameter in your operation contract you cannot have additional parameters. 如果操作合同中有Stream参数,则不能有其他参数。 If you want to add additional information you can add message headers. 如果要添加其他信息,则可以添加消息标题。

Wrap your channel in a OperationContextScope to add them on the client 将您的频道包装在OperationContextScope中,以将其添加到客户端

using (new OperationContextScope((IContextChannel)channel))
{
MessageHeader customMessageHeader = MessageHeader.CreateHeader(<name>, <namespace>, <value>);
OperationContext.Current.OutgoingMessageHeaders.Add(customMessageHeader);
}

You can read it on the server: 您可以在服务器上阅读它:

OperationContext.Current.IncomingMessageHeaders.GetHeader<Type>(<name>, <namespace>);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM