Opc.Ua.WcfChannelBase.Initialize C# (CSharp) Method

Initialize() public method

Initializes a secure channel with the endpoint identified by the URL.
Thrown if any communication error occurs.
public Initialize ( Uri url, TransportChannelSettings settings ) : void
url System.Uri The URL for the endpoint.
settings TransportChannelSettings The settings to use when creating the channel.
return void
        public void Initialize(
            Uri url,
            TransportChannelSettings settings)
        {
            if (m_wcfBypassChannel != null)
            {
                m_wcfBypassChannel.Initialize(url, settings);
                return;
            }

            throw new NotSupportedException("WCF channels must be configured when they are constructed.");
        }

Usage Example

コード例 #1
0
        /// <summary>
        /// Creates a new UA-binary transport channel if requested. Null otherwise.
        /// </summary>
        /// <param name="configuration">The application configuration.</param>
        /// <param name="description">The description for the endpoint.</param>
        /// <param name="endpointConfiguration">The configuration to use with the endpoint.</param>
        /// <param name="clientCertificate">The client certificate.</param>
        /// <param name="messageContext">The message context to use when serializing the messages.</param>
        /// <returns></returns>
        public static ITransportChannel CreateUaBinaryChannel(
            ApplicationConfiguration configuration,
            EndpointDescription description,
            EndpointConfiguration endpointConfiguration,
            X509Certificate2 clientCertificate,
            ServiceMessageContext messageContext)
        {
            // check if the server if configured to use the ANSI C stack.
            bool useUaTcp = description.EndpointUrl.StartsWith(Utils.UriSchemeOpcTcp);
            bool useHttps = description.EndpointUrl.StartsWith(Utils.UriSchemeHttps);

            #if !SILVERLIGHT
            bool useAnsiCStack = false;
            #else
            useHttps = description.EndpointUrl.StartsWith(Utils.UriSchemeHttp);
            #endif

            switch (description.TransportProfileUri)
            {
                case Profiles.UaTcpTransport:
                {
                    useUaTcp = true;

                    #if !SILVERLIGHT
                    if (configuration != null)
                    {
                        useAnsiCStack = configuration.UseNativeStack;
                    }
                    #endif

                    break;
                }

                case Profiles.HttpsXmlTransport:
                case Profiles.HttpsBinaryTransport:
                case Profiles.HttpsXmlOrBinaryTransport:
                {
                    useHttps = true;
                    break;
                }
            }

            #if !SILVERLIGHT
            // check for a WCF channel.
            if (!useUaTcp && !useHttps)
            {
                // binary channels only need the base class.
                if (endpointConfiguration.UseBinaryEncoding)
                {
                    Uri endpointUrl = new Uri(description.EndpointUrl);
                    BindingFactory bindingFactory = BindingFactory.Create(configuration, messageContext);
                    Binding binding = bindingFactory.Create(endpointUrl.Scheme, description, endpointConfiguration);

                    WcfChannelBase<IChannelBase> wcfChannel = new WcfChannelBase<IChannelBase>();

                    // create regular binding.
                    if (configuration != null)
                    {
                        wcfChannel.Initialize(
                            configuration,
                            description,
                            endpointConfiguration,
                            binding,
                            clientCertificate,
                            null);
                    }

                    // create no-security discovery binding.
                    else
                    {
                        wcfChannel.Initialize(
                            description,
                            endpointConfiguration,
                            binding,
                            null);
                    }

                    return wcfChannel;
                }

                return null;
            }
            #endif

            // initialize the channel which will be created with the server.
            ITransportChannel channel = null;

            // create a UA-TCP channel.
            TransportChannelSettings settings = new TransportChannelSettings();

            settings.Description = description;
            settings.Configuration = endpointConfiguration;
            settings.ClientCertificate = clientCertificate;

            if (description.ServerCertificate != null && description.ServerCertificate.Length > 0)
            {
                settings.ServerCertificate = Utils.ParseCertificateBlob(description.ServerCertificate);
            }
            
            #if !SILVERLIGHT
            if (configuration != null)
            {
                settings.CertificateValidator = configuration.CertificateValidator.GetChannelValidator();
            }
            #endif

            settings.NamespaceUris = messageContext.NamespaceUris;
            settings.Factory = messageContext.Factory;

            if (useUaTcp)
            {
                #if !SILVERLIGHT
                Type type = null;

                if (useAnsiCStack)
                {
                    type = Type.GetType("Opc.Ua.NativeStack.NativeStackChannel,Opc.Ua.NativeStackWrapper");
                }

                if (useAnsiCStack && type != null)
                {
                    channel = (ITransportChannel)Activator.CreateInstance(type);
                }
                else
                {
                    channel = new Opc.Ua.Bindings.TcpTransportChannel();
                }
                #endif
            }

            else if (useHttps)
            {
                channel = new Opc.Ua.Bindings.HttpsTransportChannel();
            }

            channel.Initialize(new Uri(description.EndpointUrl), settings);
            channel.Open();

            return channel;
        }