Click or drag to resize
Service Key Injection Sample

This code sample is written in C# based on .NET Framework 4.5 and WCF. It demonstrates how to inject the service key into the HTTP header of each QMS API web service call.

Certificate: Both the URL for connecting to the API and the content of the App.config file differs slightly if a certificate is used so please read the sample carefully.

Installing the certificate is beyond the scope of this sample.

To build and run this example

  1. Create a new C# Console Application project and name it MyTestApplication

  2. Make sure you have the QlikView Management Service running

  3. Right-click the References node in the Solution Explorer and select Add Service Reference.

  4. Enter the QMS API WSDL address http://<server-name>:4799/QMS/Service and click Go.

    Certificate: If the QlikView Management Service has been configured to use certificate security, you must use https instead, that is: https://<server-name>:4799/QMS/Service

  5. Enter QMSAPI as the name for the service reference namespace.

  6. [Optionally, but required for this example] Click on Advanced, select System.Collections.Generic.List for Collection Type, and click OK.

  7. Click OK to have Visual Studio automatically generate code for communication with the QMS API web service.

  8. Right-click the References node in the Solution Explorer and select Add Reference.

  9. In the References Manager window, select Assembly and then Framework in the menu to the left.

  10. In the list, select the checkbox next to System.Configuration and click OK.

  11. Replace everything in Program.cs with the following code:

    C#
    using System;
    using System.Collections.Generic;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Configuration;
    using System.ServiceModel.Description;
    using System.ServiceModel.Dispatcher;
    using MyTestApplication.QMSAPI;
    
    namespace MyTestApplication
    {
        class ServiceKeyBehaviorExtensionElement : BehaviorExtensionElement
        {
            public override Type BehaviorType
            {
                get { return typeof(ServiceKeyEndpointBehavior); }
            }
    
            protected override object CreateBehavior()
            {
                return new ServiceKeyEndpointBehavior();
            }
        }
    
        class ServiceKeyEndpointBehavior : IEndpointBehavior
        {
            public void Validate(ServiceEndpoint endpoint) { }
    
            public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { }
    
            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }
    
            public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
            {
                clientRuntime.MessageInspectors.Add(new ServiceKeyClientMessageInspector());
            }
        }
    
        class ServiceKeyClientMessageInspector : IClientMessageInspector
        {
            private const string SERVICE_KEY_HTTP_HEADER = "X-Service-Key";
    
            public static string ServiceKey { get; set; }
    
            public object BeforeSendRequest(ref Message request, IClientChannel channel)
            {
                object httpRequestMessageObject;
                if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
                {
                    HttpRequestMessageProperty httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
                    if (httpRequestMessage != null)
                    {
                        httpRequestMessage.Headers[SERVICE_KEY_HTTP_HEADER] = (ServiceKey ?? string.Empty);
                    }
                    else
                    {
                        httpRequestMessage = new HttpRequestMessageProperty();
                        httpRequestMessage.Headers.Add(SERVICE_KEY_HTTP_HEADER, (ServiceKey ?? string.Empty));
                        request.Properties[HttpRequestMessageProperty.Name] = httpRequestMessage;
                    }
                }
                else
                {
                    HttpRequestMessageProperty httpRequestMessage = new HttpRequestMessageProperty();
                    httpRequestMessage.Headers.Add(SERVICE_KEY_HTTP_HEADER, (ServiceKey ?? string.Empty));
                    request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
                }
                return null;
            }
    
            public void AfterReceiveReply(ref Message reply, object correlationState) { }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    // create a QMS API client
                    IQMS apiClient = new QMSClient();
                    //If you use IQMS2, do as follows:
                    //IQMS2 apiClient = new QMS2Client();
    
                    //If you want to connect to a server different from the one used when creating the service reference,
                    //do as follows:                                
                    // 
                    //NTLM only (default installation)
                    //IQMS apiClient = new QMSClient("BasicHttpBinding_IQMS", "http://remotehost:4799/QMS/Service");
                    // or
                    //IQMS2 apiClient = new QMSClient("BasicHttpBinding_IQMS2", "http://remotehost:4799/QMS/Service");
                    //Certificate security
                    //IQMS apiClient = new QMSClient("WSHttpBinding_IQMS", "https://remotehost:4799/QMS/Service");
                    // or
                    //IQMS2 apiClient = new QMS2Client("WSHttpBinding_IQMS2", "https://remotehost:4799/QMS/Service");
    
    
                    // retrieve a time limited service key
                    ServiceKeyClientMessageInspector.ServiceKey = apiClient.GetTimeLimitedServiceKey();
                    // get a list of QVS services
                    List<ServiceInfo> qvsServices = apiClient.GetServices(ServiceTypes.QlikViewServer);
                    if (qvsServices.Count > 0)
                    {
                        // retrieve folder settings for the first QVS in the list
                        QVSSettings qvsSettings = apiClient.GetQVSSettings(qvsServices[0].ID, QVSSettingsScope.Folders);
                        // add a new mount
                        qvsSettings.Folders.UserDocumentMounts.Add(new QVSMount() { Browsable = false, Path = @"\\unc\some", Name = "MyMount" });
                        // save settings
                        apiClient.SaveQVSSettings(qvsSettings);
                        Console.WriteLine("Settings saved. New mount added.");
                    }
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine("An exception occurred: " + ex.Message);
                }
                // wait for user to press any key
                Console.ReadLine();
            }
        }
    }
  12. Open the App.config file in your C# project, for example by double-clicking on it in Solution Explorer.

  13. Depending on whether certificate security is used or not, add one of the following XML snippets to your app.config file directly within the <system.serviceModel> tag:

    C# - Without certificates
    <extensions>
        <behaviorExtensions>
            <add name="serviceKeyBehavior" type="MyTestApplication.ServiceKeyBehaviorExtensionElement, MyTestApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
        </behaviorExtensions>
    </extensions>
    <behaviors>
        <endpointBehaviors>
            <behavior name="ServiceKeyEndpointBehavior">
                <serviceKeyBehavior/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    C# - With certificates
    <extensions>
        <behaviorExtensions>
            <add name="serviceKeyBehavior" type="MyTestApplication.ServiceKeyBehaviorExtensionElement, MyTestApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
        </behaviorExtensions>
    </extensions>
    <behaviors>
        <endpointBehaviors>
            <behavior name="ServiceKeyEndpointBehavior">
                <serviceKeyBehavior/>
                <clientCredentials>
                    <clientCertificate findValue="CN=QVProxy" x509FindType="FindBySubjectDistinguishedName" storeLocation="LocalMachine" storeName="My" />
                    <serviceCertificate>
                        <authentication certificateValidationMode="ChainTrust" revocationMode="NoCheck"/>
                    </serviceCertificate>              
                </clientCredentials>            
            </behavior>
        </endpointBehaviors>
    </behaviors>
  14. Add the following XML attribute to the bottom <endpoint ... /> tag:

    behaviorConfiguration="ServiceKeyEndpointBehavior"
  15. Build and run the solution.

Here are links to examples of complete App.config files: