【问题标题】:Accepting large posts to WCF web service not hosted by IIS接受非 IIS 托管的 WCF Web 服务的大型帖子
【发布时间】:2017-10-05 11:03:37
【问题描述】:

我正在尝试创建一个可以接受文件并且不会托管在 IIS 上的 Web 服务(我计划将其作为独立服务运行)。我在这里找到了如何执行此操作的示例:https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-create-a-basic-wcf-web-http-service

使用上面的示例,我能够启动并运行一切,一切正常,直到我尝试将“更大”文件交给它,此时我收到 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>

【问题讨论】:

  • 现在我很尴尬,我自己没有弄清楚!如果您将其作为答案提交,我将接受它.. 否则我将标记为已解决。
  • 这只是一个链接的答案,所以我不想发布它。很高兴能提供帮助。

标签: c# .net web-services wcf


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多