0

I'm attempting to create a web service which can accept files and will not be hosted on IIS (I plan on running this as a standalone service). I found an example of how to do this here: https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-create-a-basic-wcf-web-http-service

Using the above example I was able to get things up and running and everything works great until I try to hand it a "Larger" file at which time I get the 413 error telling me my submission is to big. I've done some searching and found out that there is a buffer and/or max submission size variable which needs to be modified to allow larger uploads and this is done in the App.config file and/or the web.config file. My problem is I'm not familiar with the structure of these files and the way I've created my project there is no Web.config file and I don't know where the necessary code should go in the App.config file. Here's what I have so far.

WCF Service Contract

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebGet(UriTemplate = "/{profile}/GetFileIfExists/{fileName}", ResponseFormat = WebMessageFormat.Json)]
    Stream GetFileIfExists(string fileName, string profile);

    [OperationContract]
    [WebInvoke(UriTemplate = "/{profile}/ReceiveFile",Method = "POST",BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    string ReceiveFile(string profile, Stream ORU);
}

Here's where the "Server/Service" host is started

ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
host.Open();
cf = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:9000");
cf.Endpoint.Behaviors.Add(new WebHttpBehavior());

And here's what is currently in the App.config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
    </startup>
    <runtime>
       <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
              <assemblyIdentity name="WebMatrix.Data" publicKeyToken="31bf3856ad364e35" culture="neutral" />
              <bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" />
          </dependentAssembly>
       </assemblyBinding>
    </runtime>
</configuration>

Do I need to create a Web.config or can I put the necessary pieces in App.config.. if so, where do I put them in the file. I've tried putting in the below code just under the "" begin tag with no luck.. but I'm sure I'm missing something obvious.

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding maxBufferSize="2147483647" 
             maxReceivedMessageSize="2147483647">
      <readerQuotas maxStringContentLength="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>
</system.serviceModel>
jrlambs
  • 101
  • 9
  • 1
    If it help, you can [set the value on the binding in your code](https://stackoverflow.com/questions/2457408/how-to-set-the-maxreceivedmessagesize-programatically-when-using-a-wcf-client) – stuartd Oct 05 '17 at 11:06
  • Now I'm embarrassed I didn't figure that out myself! If you submit that as an answer I'll accept it.. otherwise I'll just mark this solved. – jrlambs Oct 05 '17 at 11:48
  • It would just be a link-only answer, so I wouldn't want to post that. Glad to help. – stuartd Oct 05 '17 at 12:17

1 Answers1

0

As per a comment from stuartd, I ended up accomplishing this by not messing with the XML files at all. Instead I just set the setting on the binding directly in the code... which he instructed me to do using the following article.

my code above changed to this:

host = new WebServiceHost(typeof(Service), new Uri("http://0.0.0.0:9000/"));
try
{
    var binding = new WebHttpBinding();
    binding.MaxReceivedMessageSize = Int32.MaxValue;
    binding.MaxBufferSize = Int32.MaxValue;
    ep = host.AddServiceEndpoint(typeof(IService), binding, "");
    host.Open();
    cf = new ChannelFactory<IService>(binding, "http://localhost:9000");
    cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
    Log("Webservice started an listening on " + "http://0.0.0.0:9000/");
}    
jrlambs
  • 101
  • 9