WCF服务:的app.config与属性或二者的混合物 [英] WCF service: app.config versus attributes or a mixture of both

查看:255
本文介绍了WCF服务:的app.config与属性或二者的混合物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WCF应用程序,我们有属性的的ServiceContract:

In a WCF application we have a servicecontract with attributes:

namespace We.Work {
    [ServiceContract(Namespace = "We", Name = "IWork", SessionMode = SessionMode.NotAllowed)]
        public interface IWork

标记了ServiceContract与属性的实现:

an implementation of the servicecontract with attributes:

namespace We.Work {
    [ServiceBehavior(Name = "Work", Namespace = "We",
           IncludeExceptionDetailInFaults = true,
           InstanceContextMode = InstanceContextMode.PerCall,
           ConcurrencyMode = ConcurrencyMode.Multiple,
           ReleaseServiceInstanceOnTransactionComplete = false
        )]
        public class WorkImplementation : IWork

一个ServiceHost的(窗口服务或开发控制台应用程序)

a servicehost (windows service or console application for development)

namespace We.Host {
// ....
        workServiceHost = new ServiceHost(typeof(We.Work.WorkImplementation));
        workServiceHost.Faulted += new EventHandler(Host_Faulted);
        workServiceHost.Open();

和最后但并非最不重要一个app.config:

and last but not least an app.config:

<service behaviorConfiguration="WorkServiceBehaviour" name="We.Work.WorkImplementation">
        <endpoint behaviorConfiguration="WorkEndPointBehaviour" binding="wsHttpBinding" bindingConfiguration="WorkWsHttpBindingConfig" name="WorkEndPoint" contract="We.Work.IWork"/>
        <host> <baseAddresses> <add baseAddress="http://.../Work.svc"/>            </baseAddresses> </host>
      </service>

<behaviors>
      <endpointBehaviors>
        <behavior name="WorkEndPointBehaviour">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="WorkServiceBehaviour">
          <serviceDebug httpHelpPageEnabled="true" httpsHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceMetadata/>
          <serviceThrottling maxConcurrentCalls="25" maxConcurrentSessions="25" maxConcurrentInstances="25"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

问题:
是否有可能混合的app.config和属性,配置已precendence,什么是一个好的做法呢?

Questions: Is it possible to mix app.config and attributes, which configuration has precendence, what is a good practice?

例如。确实的ServiceContract(SessionMode = SessionMode.NotAllowed)prevent的wsHttpBinding使用会话?

E.g. does ServiceContract(SessionMode = SessionMode.NotAllowed) prevent wsHttpBinding from using sessions?

[回答:我如何能确保在app.config中的设置真正应用 - 完全限定名工作]

[Answered: how can I be sure the settings in app.config are really applied -- the fully qualified name works.]

推荐答案

在配置文件中的服务名称属性必须是服务类的完全限定名称WCF自动拿起配置。

The service name attribute in the config file must be the fully-qualified name of the service class for WCF to pick up the configuration automatically.

有可能混合配置和code。然而,没有precedence本身。当你实例化的ServiceHost WCF会读取配置文件。如果你想设置code附加属性时,将覆盖东西已经存在。

It is possible to mix config and code. However, there is no precedence as such. WCF will read the config file when you instantiate the ServiceHost. If you want to set additional properties in code, they will overwrite what's already there.

最佳做法是完全取决于你。配置文件元素的目的是去耦的服务配置和实施,其可以是或可以不是在部署一个考虑因素。

Best practice is entirely up to you. The purpose of the config file elements is to decouple the service configuration and implementation, which may or may not be a consideration in your deployment.

更新

属性服务类code是一个不同的故事。一些属性的目的是为了让开发者说:我要求的配置是一致的与此属性,或者作为设计我的服务将无法运行。因此,虽然属性实际上不会覆盖配置,WCF将检查配置与属性一致,拒绝,如果他们不尽一致,以启动该服务。

Attributes on the service class code are a different story. The purpose of some attributes are to let the developer say "I demand config that is consistent with this attribute, or my service won't run as designed". Therefore, although attributes won't actually override config, WCF will check that config is consistent with attributes and refuse to start the service if they are not consistent.

这篇关于WCF服务:的app.config与属性或二者的混合物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆