使用 ServiceRoute 时指定 WCF 绑定

发布于 2024-10-18 22:29:49 字数 448 浏览 1 评论 0原文

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

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

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

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

任何建议表示赞赏。

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-10-25 22:29:49

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

//in 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;
          }
        }
    }
}

You can make it by extending host factory.
By little modification of code bellow you can pass an additional parameter to WCFServiceHostFactory to set it from Global.asax
I have used the classes bellow to always set it to Int32.MaxValue

//in 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;
          }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文