接受大的帖子到WCF的Web服务不是由IIS托管
问题描述:
我试图创建一个可以接受文件,并不会托管在IIS上的Web服务(我计划将其作为独立服务运行)。我找到了一个如何在这里执行此操作的示例:https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-create-a-basic-wcf-web-http-service接受大的帖子到WCF的Web服务不是由IIS托管
使用上述示例,我能够启动并运行所有内容,并且一切正常,直到我尝试将它放在“较大”文件中, 413错误告诉我,我的提交是大的。我做了一些搜索,发现有一个缓冲区和/或最大提交大小变量需要修改,以允许更大的上传,这是在App.config文件和/或web.config文件中完成的。我的问题是我不熟悉这些文件的结构,以及我创建项目的方式,没有Web.config文件,我不知道必须的代码应该放在App.config文件中。这是迄今为止我所拥有的。
WCF服务合同
[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);
}
这里就是 “服务器/服务” 主机启动
ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
host.Open();
cf = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:9000");
cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
这里还有什么是目前在App.config文件。
<?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>
我需要创建一个Web.config或者我可以把需要的组件中的App.config ..如果是这样,我在哪里把他们的文件中。我已经试过把下面的代码放在“”开始标记下面,但没有运气......但我确信我错过了一些明显的东西。
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
答
根据stuartd的评论,我最终完成了这个任务,而不是搞乱XML文件。相反,我只是直接在代码中设置绑定的设置......他指示我使用以下的article。
我上面的代码改成这样:
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/");
}
如果帮助,您可以设定[在代码中绑定的值(https://stackoverflow.com/questions/2457408/how-to -set-the-maxreceivedmessagesize-programatically-when-using-a-wcf-client) – stuartd
现在我很尴尬,我没有弄清楚自己!如果您将该答案作为答案提交,我会接受它。否则,我将标记解决方案。 – jrlambs
这只是一个链接的答案,所以我不想发布。乐意效劳。 – stuartd