使用RestSharp将文件上传到WCF

时间:2019-05-21 05:09:20

标签: c# wcf restsharp

我已经搜索过了,但是没有找到答案。 我需要使用RestSharp将文件(图像或PDF)上传到WCF REST服务。 我已经使用过AddFile()方法,但是没有找到该服务(我在该方法的第一行添加了一个断点),并且返回的响应为空字符串。

我尝试了byte []和Stream。 Web服务是:

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json)]
void NotesAttachment(Stream input);

请提供有关该服务以及如何从客户端调用它的示例。

在VS 2015上使用C#4.5.1。

1 个答案:

答案 0 :(得分:0)

请参考我的示例
IServcie1.cs

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Task UploadStream(Stream stream);

Service1.svc.cs

public async Task UploadStream(Stream stream)
        {
            using (stream)
            {
                using (var file = File.Create(Path.Combine(HostingEnvironment.MapPath("~/Uploads"), Guid.NewGuid().ToString() + ".png")))
                {
                    await stream.CopyToAsync(file);
                }
            }
        }

Web.config

      <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="httpbinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <security mode="None">
          </security>
          <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147473647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
        </binding>
        <binding name="mybinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <security mode="Transport">
            <transport clientCredentialType="None"></transport>
          </security>
          <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147473647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <!--http and https are all supported.-->
      <add binding="webHttpBinding" scheme="http" bindingConfiguration="httpbinding" />
      <add binding="webHttpBinding" scheme="https" bindingConfiguration="mybinding"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

客户致电示例。以邮递员为例。
enter image description here 随时让我知道是否有什么可以帮助您的。