如何使用基于LOB Adapter SDK的设计时接口

时间:2014-06-30 19:17:30

标签: wcf-lob-adapter

我正在尝试构建基于Web的GUI来使用基于LOB Adapter SDK的自定义连接器。 特别是,我想使用IMetadataResolverHandler界面浏览元数据。

我有两个问题:

尝试实例化自定义适配器时会出现第一个问题。我的计划是获取IConnectionFactory接口的实例,通过该接口,我可以获得一个新的IConnection并连接到目标LOB系统。

由于Adapter基类中最有趣的方法受到保护,我似乎只能使用反射成功(请参阅下面的示例代码)。

尝试从目标系统浏览元数据时发生第二个问题。 IMetadataResolverHandler接口上的方法Browse需要一个MetadataLookup对象的实例,我不知道如何获取。

请参阅下面的示例代码:

    static void Main(string[] args)
    {
        var extension = new SqlAdapterBindingElementExtensionElement();
        var adapter = (Adapter) Activator.CreateInstance(extension.BindingElementType);

        var isHandlerSupportedMethodInfo = adapter.GetType().GetMethod("IsHandlerSupported", BindingFlags.NonPublic | BindingFlags.Instance);
        var buildConnectionUri = adapter.GetType().GetMethod("BuildConnectionUri", BindingFlags.NonPublic | BindingFlags.Instance);
        var buildConnectionFactory = adapter.GetType().GetMethod("BuildConnectionFactory", BindingFlags.NonPublic | BindingFlags.Instance);

        if (isHandlerSupportedMethodInfo == null || buildConnectionUri == null || buildConnectionFactory == null)
        {
            Console.WriteLine("Not a LOB adapter.");
            Environment.Exit(1);
        }

        var isHandlerSupportedTHandler = isHandlerSupportedMethodInfo.MakeGenericMethod(typeof(IMetadataResolverHandler));
        var isMetadataBrowseSupported = (bool)isHandlerSupportedTHandler.Invoke(adapter, new object[] { });

        if (!isMetadataBrowseSupported)
        {
            Console.WriteLine("Metadata retrieval not supported.");
            Environment.Exit(1);
        }

        var bindingElement = (SqlAdapterBindingElement)adapter;
        bindingElement.AcceptCredentialsInUri = false;
        bindingElement.InboundOperationType = InboundOperation.TypedPolling;
        bindingElement.PolledDataAvailableStatement = "EXEC [dbo].[usp_IsDataAvailable]";
        bindingElement.PollingStatement = "EXEC [dbo].[usp_SelectAvailableData]";
        bindingElement.PollingIntervalInSeconds = 10;

        var binding = new CustomBinding();
        binding.Elements.Add(adapter);

        var parameters = new BindingParameterCollection();

        var context = new BindingContext(binding, parameters);

        var credentials = new ClientCredentials();
        credentials.UserName.UserName = "username";
        credentials.UserName.Password = "password";

        var address = (ConnectionUri) buildConnectionUri.Invoke(adapter, new []{ new Uri("mssql://azure.database.windows.net//SampleDb?InboundId=uniqueId")});

        var connectionFactory = (IConnectionFactory)buildConnectionFactory.Invoke(adapter, new object[] { address, credentials, context });
        var connection = connectionFactory.CreateConnection();

        connection.Open(TimeSpan.MaxValue);

        MetadataLookup lookup = null; // ??
        var browser = connection.BuildHandler<IMetadataBrowseHandler>(lookup);

        connection.Close(TimeSpan.MaxValue);
    }

1 个答案:

答案 0 :(得分:1)

回答我自己的问题,我通过检查“消费适配器服务”向导的代码来解决这个问题。关键是使用IMetadataRetrievalContract接口,在内部使用最多三个LOB-SDK接口实现,特别是IMetadataResolverHandler

以下是无反射的代码:

var extension = new SqlAdapterBindingElementExtensionElement();
var adapter = (Adapter) Activator.CreateInstance(extension.BindingElementType);

var bindingElement = (SqlAdapterBindingElement)adapter;
bindingElement.AcceptCredentialsInUri = false;
bindingElement.InboundOperationType = InboundOperation.TypedPolling;
bindingElement.PolledDataAvailableStatement = "EXEC [dbo].[usp_IsDataAvailable]";
bindingElement.PollingStatement = "EXEC [dbo].[usp_SelectAvailableData]";
bindingElement.PollingIntervalInSeconds = 10;

var binding = new CustomBinding();
binding.Elements.Add(adapter);

const string endpoint = "mssql://azure.database.windows.net//SampleDb?InboundId=unique";
var factory = new ChannelFactory<IMetadataRetrievalContract>(binding, new EndpointAddress(new Uri(endpoint)));
factory.Credentials.UserName.UserName = "username";
factory.Credentials.UserName.Password = "password";

factory.Open();
var channel = factory.CreateChannel();
((IChannel)channel).Open();

var metadata = channel.Browse(MetadataRetrievalNode.Root.DisplayName, 0, Int32.MaxValue);

((IChannel) channel).Close();
factory.Close();