简体   繁体   中英

Accepting large posts to WCF web service not hosted by IIS

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://docs.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>

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/");
}    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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