2017-05-31 60 views
0

我想用C#代碼創建服務行爲,但我不知道如何。用代碼編寫服務行爲

這裏是行爲的配置:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> 
    </startup> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ValidateUser"> 
      <serviceCredentials> 
      <userNameAuthentication userNamePasswordValidationMode="Custom" 
      customUserNamePasswordValidatorType="ConsoleApplication1.CustomUserNameValidator"/> 
      </serviceCredentials> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

回答

1

嘗試這樣:

VAR srvCredentials =新ServiceCredentials(); srvCredentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom; (NameOfYourServiceHost).Description.Behaviors.Add(srvCredentials);

ServiceCredentials類位於「System.ServiceModel.Description」中。

我希望這有助於!