6

I am currently registering a WCF service using the following code:

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

This is all well and good however I also need to change the MaxStringContentLength property of the reader quota settings. It appears that the default value of 8192 is used, regardless of my attempts to change this and I guess this is from the DefaultServiceModel?

Are there any suitable hooks to override this setting on the DefaultServiceModel, or should I be deriving my own service host/model classes, or am I going about this the wrong way?

Any suggestions appreciated.

EDIT: Please note that the configuration of the binding must be performed programatically (not via configuration files).

Thanks

4

1 に答える 1

13

ホストファクトリーを拡張することで作れます。次のコードを少し変更することで、追加のパラメーターを 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;
          }
        }
    }
}
于 2011-05-31T13:00:00.567 に答える