【问题标题】:Specifying a WCF binding when using ServiceRoute使用 ServiceRoute 时指定 WCF 绑定
【发布时间】:2026-02-14 02:05:01
【问题描述】:

我目前正在使用以下代码注册 WCF 服务:

var factory = new DefaultServiceHostFactory();
RouteTable.Routes.Add(new ServiceRoute("XXXEndPoint", factory, IXXXEndPoint)));

这一切都很好,但是我还需要更改阅读器配额设置的 MaxStringContentLength 属性。似乎使用了默认值 8192,不管我是否尝试更改它,我猜这是来自 DefaultServiceModel?

是否有任何合适的钩子来覆盖 DefaultServiceModel 上的此设置,或者我应该派生自己的服务主机/模型类,还是我这样做的方式错误?

任何建议表示赞赏。

编辑:请注意,绑定的配置必须以编程方式执行(而不是通过配置文件)。

谢谢

【问题讨论】:

  • 您可以在此服务所在的虚拟目录的web.config 中配置您的服务及其绑定和参数
  • 嗨@mar​​c_s。请参阅编辑重新。程序化配置。谢谢
  • @John 你如何让 routegine 忽略服务 url?
  • 请问为什么这个问题被标记为 castle-Windsor?

标签: wcf routetable maxstringcontentlength


【解决方案1】:

您可以通过扩展主机工厂来实现。 通过对下面的代码稍加修改,您可以将附加参数传递给 WCFServiceHostFactory 以从 Global.asax 进行设置 我使用下面的类始终将其设置为 Int32.MaxValue

//在 Global.asax 中

routes.Add(new ServiceRoute(routePrefix,
            new WCFServiceHostFactory(),
            serviceType));

//WCFServiceHostFactory.cs

namespace MyServices.MyService
{
    public class WCFServiceHostFactory:WebServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            WCFServiceHost host = new WCFServiceHost(serviceType, baseAddresses);

            return host;
        }
    }
}

//WCFServiceHost.cs

namespace MyServices.MyService
{
    public class WCFServiceHost:WebServiceHost
    {
        public WCFServiceHost(): base ()
        {
        }

        public WCFServiceHost (object singletonInstance, params Uri [] baseAddresses)
          : base (singletonInstance, baseAddresses)
        {
        }

        public WCFServiceHost(Type serviceType, params Uri[] baseAddresses)
          : base (serviceType, baseAddresses)
        {
        }



        protected override void OnOpening ()
        {
          base.OnOpening ();

          foreach (Uri baseAddress in BaseAddresses) {
            bool found = false;
            foreach (ServiceEndpoint se in Description.Endpoints)
                if (se.Address.Uri == baseAddress)
                {
                    found = true;
                    ((WebHttpBinding)se.Binding).ReaderQuotas.MaxStringContentLength = Int32.MaxValue;//here you set it and also set it below
                }
            if (!found) {
              if (ImplementedContracts.Count > 1)
                throw new InvalidOperationException ("Service '"+ Description.ServiceType.Name + "' implements multiple ServiceContract types, and no endpoints are defined in the configuration file. WebServiceHost can set up default endpoints, but only if the service implements only a single ServiceContract. Either change the service to only implement a single ServiceContract, or else define endpoints for the service explicitly in the configuration file. When more than one contract is implemented, must add base address endpoint manually");
              var  enumerator = ImplementedContracts.Values.GetEnumerator ();
              enumerator.MoveNext ();
              Type contractType = enumerator.Current.ContractType;
              WebHttpBinding binding = new WebHttpBinding();
              binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue; //here also
          AddServiceEndpoint (contractType, binding, baseAddress);
            }

          }

          foreach (ServiceEndpoint se in Description.Endpoints)
            if (se.Behaviors.Find<WebHttpBehavior> () == null)
              se.Behaviors.Add (new WebHttpBehavior ());

          // disable help page.
          ServiceDebugBehavior serviceDebugBehavior = Description.Behaviors.Find<ServiceDebugBehavior> ();
          if (serviceDebugBehavior != null) {
            serviceDebugBehavior.HttpHelpPageEnabled = false;
            serviceDebugBehavior.HttpsHelpPageEnabled = false;
          }
        }
    }
}

【讨论】:

  • 赞成,因为这是我在互联网上能找到的唯一一个说明如何做到这一点的文档。
  • 您好得到以下错误:'WCFServiceHostFactory.CreateServiceHost(Type, Uri[])': 找不到合适的方法来覆盖