Ger*_*ard 4 wcf attributes app-config servicehost
在WCF应用程序中,我们有一个带有属性的servicecontract:
namespace We.Work {
[ServiceContract(Namespace = "We", Name = "IWork", SessionMode = SessionMode.NotAllowed)]
public interface IWork
Run Code Online (Sandbox Code Playgroud)
具有属性的servicecontract的实现:
namespace We.Work {
[ServiceBehavior(Name = "Work", Namespace = "We",
IncludeExceptionDetailInFaults = true,
InstanceContextMode = InstanceContextMode.PerCall,
ConcurrencyMode = ConcurrencyMode.Multiple,
ReleaseServiceInstanceOnTransactionComplete = false
)]
public class WorkImplementation : IWork
Run Code Online (Sandbox Code Playgroud)
servicehost(用于开发的Windows服务或控制台应用程序)
namespace We.Host {
// ....
workServiceHost = new ServiceHost(typeof(We.Work.WorkImplementation));
workServiceHost.Faulted += new EventHandler(Host_Faulted);
workServiceHost.Open();
Run Code Online (Sandbox Code Playgroud)
最后但并非最不重要的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>
Run Code Online (Sandbox Code Playgroud)
问题:是否可以混合app.config和属性,哪种配置有优先权,什么是好的做法?
例如,ServiceContract(SessionMode = SessionMode.NotAllowed)阻止wsHttpBinding使用会话吗?
[已回答:如何确保app.config中的设置真正应用 - 完全限定名称有效.]
配置文件中的服务名称属性必须是服务类的完全限定名称,以便WCF自动获取配置.
可以混合配置和代码.但是,没有优先权.当您实例化时,WCF将读取配置文件ServiceHost
.如果要在代码中设置其他属性,它们将覆盖已存在的属性.
最佳实践完全取决于您.配置文件元素的目的是解耦服务配置和实现,这可能是您部署中的考虑因素,也可能不是.
UPDATE
服务类代码的属性是另一回事.某些属性的目的是让开发人员说"我要求配置与此属性一致,否则我的服务将无法按设计运行".因此,尽管属性实际上不会覆盖配置,但WCF将检查配置是否与属性一致,并且如果它们不一致则拒绝启动服务.
归档时间: |
|
查看次数: |
1394 次 |
最近记录: |