使用ServiceRoute时指定WCF绑定

Joh*_*ohn 6 wcf maxstringcontentlength routetable

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

var factory = new DefaultServiceHostFactory();
RouteTable.Routes.Add(new ServiceRoute("XXXEndPoint", factory, IXXXEndPoint)));
Run Code Online (Sandbox Code Playgroud)

这一切都很好,但我还需要更改阅读器配额设置的MaxStringContentLength属性.似乎使用默认值8192,无论我是否尝试更改此设置,我想这是来自DefaultServiceModel?

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

任何建议赞赏.

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

谢谢

小智 13

您可以通过扩展主机工厂来实现.通过稍微修改代码,您可以将其他参数传递给WCFServiceHostFactory以从Global.asax设置它我已经使用了以下类来始终将其设置为Int32.MaxValue

//在Global.asax中

routes.Add(new ServiceRoute(routePrefix,
            new WCFServiceHostFactory(),
            serviceType));
Run Code Online (Sandbox Code Playgroud)

//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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

//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;
          }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)