与属性或两者的混合

与属性或两者的混合

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

问题描述

在 WCF 应用程序中,我们有一个带有属性的服务契约:

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

一个服务主机(用于开发的 Windows 服务或控制台应用程序)

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 和属性,哪个配置优先,什么是好的做法?

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

例如ServiceContract(SessionMode = SessionMode.NotAllowed) 是否阻止 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.

可以混合配置和代码.但是,没有这样的优先级.当您实例化 ServiceHost 时,WCF 将读取配置文件.如果您想在代码中设置其他属性,它们将覆盖已有的内容.

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.

更新

服务类代码上的属性是另一回事.某些属性的目的是让开发人员说我需要与此属性一致的配置,否则我的服务将无法按设计运行".因此,虽然属性实际上不会覆盖配置,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 与属性或两者的混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:51