【问题标题】:Upload File to WCF using RestSharp使用 RestSharp 将文件上传到 WCF
【发布时间】:2019-10-07 10:39:53
【问题描述】:

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

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

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

我需要有关服务以及如何从客户端调用它的示例。

在 VS 2015 上使用 C# 4.5.1。

【问题讨论】:

    标签: c# wcf restsharp


    【解决方案1】:

    请参考我的示例
    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>
    

    客户端调用示例。以邮递员为例。
    如果有什么可以帮助的,请随时告诉我。

    【讨论】:

    • 您好,感谢您的回复,我收到“实体太大”错误。我添加了 maxAllowedContentLength ,maxBufferPoolSize,maxStringContentLength,maxArrayLength,maxBytesPerRead 但仍然有错误
    • 这是因为默认的 MaxReceivedMessageSize 属性为 65536 字节,约 64kb。 docs.microsoft.com/en-us/dotnet/api/…你可以增加属性值以免遇到错误。参考我上面修改的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    相关资源
    最近更新 更多