使用休息服务wcf通过邮递员上传大文件

时间:2017-07-17 05:23:59

标签: wcf postman

我正在开发.net mvc的网站。我在我的web项目中创建了一个wcf服务。我正在尝试通过此服务上传文件。我无法通过此上传大文件。我的代码在这里。请帮帮我。

我的服务界面

[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "AddDiscount", RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        Result AddDiscount([MessageParameter(Name = "data")] Stream data);

这里有数据接收图像和其他参数。

public Result AddDiscount(Stream data)
        {
            Result objResult = new Result();
            TBL_DISCOUNT_MASTER objDiscount = new TBL_DISCOUNT_MASTER();

            long? PostedDiscountId;

            try
            {
                StreamReader reader = new StreamReader(data);
                var str = reader.ReadToEnd();
                objDiscount = JsonConvert.DeserializeObject<TBL_DISCOUNT_MASTER>(str);


                Image dd = objCamel.byteArrayToImage(objDiscount.DISCOUNT_LOGO);
                string FileName = string.Concat(PostedDiscountId, "_logo.jpg");
                var path = Path.Combine(HostingEnvironment.MapPath("~/Content/Images/Discount"), FileName);
                dd.Save(path);

                dd = objCamel.byteArrayToImage(objDiscount.DISCOUNT_IMAGE);
                FileName = string.Concat(PostedDiscountId, "_background.jpg");
                path = Path.Combine(HostingEnvironment.MapPath("~/Content/Images/Discount"), FileName);
                dd.Save(path);

                dd = objQRCode.GetQRCode(PostedDiscountId.ToString()); ;
                FileName = string.Concat(PostedDiscountId, "_qr.jpg");
                path = Path.Combine(HostingEnvironment.MapPath("~/Content/Images/Discount"), FileName);
                dd.Save(path);

                objResult.SUCCESS = 1;
                objResult.MESSAGE = "Thank you for adding a discount offer. Please wait for admin approval.";


                return objResult;
            }
            catch (Exception ex)
            {
                objResult.SUCCESS = 2;
                objResult.MESSAGE = "Failed - " + ex.Message;
                return objResult;
            }

        }

我的web.config

<bindings>
      <basicHttpBinding>
          <binding receiveTimeout="00:10:00" sendTimeout="00:10:00" name="httpsBinding" allowCookies="true" maxBufferPoolSize="2147483647"
              maxReceivedMessageSize="2147483647" transferMode="Streamed">
              <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                  maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
              <security mode="TransportCredentialOnly">
                  <transport clientCredentialType="Basic" />
              </security>
          </binding>
          <binding name="Default">
              <security mode="Transport" />
          </binding>
      </basicHttpBinding>
  </bindings>
<services>
  <service name="Zahhab.Services.Service1" behaviorConfiguration="Default">
    <endpoint address="ZahhabService" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="Zahhab.Services.IService1"/>

  </service>
</services>

2 个答案:

答案 0 :(得分:0)

默认情况下,WCF允许您上传最大29,3 MB的文件大小。您可以通过在web.config中更改以下内容来修改它。

<system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="40960000" />  <!-- allow up to 40 mb -->
      </requestFiltering>
    </security>
</system.webServer>

答案 1 :(得分:0)

最后我找到了解决方案。我刚刚在web.config文件中添加了以下部分。完整的配置是:

<system.serviceModel>
      <bindings>
          <basicHttpBinding>
              <binding receiveTimeout="00:10:00" sendTimeout="00:10:00" name="httpsBinding" allowCookies="true" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Streamed">
                  <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                  <security mode="TransportCredentialOnly">
                      <transport clientCredentialType="Basic" />
                  </security>
              </binding>
          </basicHttpBinding>
          <webHttpBinding>
            <binding transferMode="Streamed" name="webBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="0" receiveTimeout="01:30:00" sendTimeout="01:30:00">
              <readerQuotas maxDepth="2147483647" maxArrayLength="2147483647" maxStringContentLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
            </binding>
          </webHttpBinding>
      </bindings>
    <services>
      <service name="my service name" behaviorConfiguration="Default">
        <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="my contract" />
      </service>
    </services>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="Default">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>