WCF service: app.config versus attributes or a mixture of both

前端 未结 1 1202
名媛妹妹
名媛妹妹 2020-12-06 21:06

In a WCF application we have a servicecontract with attributes:

namespace We.Work {
    [ServiceContract(Namespace = \"We\", Name = \"IWork\", SessionMode =          


        
相关标签:
1条回答
  • 2020-12-06 21:27

    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.

    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.

    UPDATE

    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.

    0 讨论(0)
提交回复
热议问题