很多时候,适配器使用者被要求将凭据传递到目标业务线系统。 为了实现此功能,您需要提供一个 WCF 服务行为。
以下示例代码演示如何派生服务行为。 它将从适配器使用者获取的凭据委托给适配器。 然后,适配器必须使用业务线协议对凭据进行身份验证。 对凭据进行身份验证后,该服务可以开始侦听业务线应用程序中的传入事件。
/// <summary>
/// This class derives from a WCF service behavior. It is used in the inbound scenario
/// for the Inbound Service to pass the line-of-business credentials to the adapter using
/// WCF ClientCredentials class.
/// </summary>
public class InboundClientCredentialsServiceBehavior : ClientCredentials, IServiceBehavior
{
public InboundClientCredentialsServiceBehavior() { }
public InboundClientCredentialsServiceBehavior(InboundClientCredentialsServiceBehavior other)
: base(other)
{
}
#region IServiceBehavior Members
public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
bindingParameters.Add(this);
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
}
public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
}
#endregion
protected override ClientCredentials CloneCore()
{
return new InboundClientCredentialsServiceBehavior(this);
}
}
以下示例代码演示如何使用服务行为将凭据传递给适配器。
// Create a ServiceHost for the EchoServiceImpl type
// and use the base address from app.config
ServiceHost host = new ServiceHost(typeof(EchoServiceImpl));
// Set service behavior to pass the line-of-business credentials.
InboundClientCredentialsServiceBehavior credentialsBehavior = new InboundClientCredentialsServiceBehavior();
credentialsBehavior.UserName.UserName = "username";
credentialsBehavior.UserName.Password = "****";
host.Description.Behaviors.Add(credentialsBehavior);
// Open the ServiceHost to start listening for messages
host.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.ReadLine();
// Close the ServiceHost
host.Close();