예제 #1
0
        /// <summary>
        /// Initializes the notification service client.
        /// </summary>
        private void InitializeClient(string addressText)
        {
            IPAddress ipAddress;

            if (!IPAddress.TryParse(addressText, out ipAddress))
            {
                Messages.Text += "Invalid Machine Address entered.";
                return;
            }
            var callbackClass = new NotificationServiceCallback();

            callbackClass.NotificationReceived += OnNotificationReceived;
            var notificationServiceBinding = new NetTcpBinding(SecurityMode.Message);

            notificationServiceBinding.ReceiveTimeout = TimeSpan.FromSeconds(10);
            notificationServiceBinding.SendTimeout    = TimeSpan.FromSeconds(10);

            AddressHeader          header  = AddressHeader.CreateAddressHeader("machine-connect", "hurco.com", "machine-connect");
            EndpointAddressBuilder builder = new EndpointAddressBuilder(new EndpointAddress("net.tcp://" + ipAddress + "/NotificationService"));

            builder.Headers.Add(header);
            builder.Identity = EndpointIdentity.CreateDnsIdentity("machine-connect.hurco.com");

            notificationServiceClient = new NotificationServiceClient(new InstanceContext(callbackClass), notificationServiceBinding, builder.ToEndpointAddress()); //create local service

            ab = false;
            if (notificationServiceClient.ClientCredentials == null)
            {
                Messages.Text += "Invalid client. Please try again.";
                return;
            }

            ////turn on security
            notificationServiceBinding.Security.Mode = SecurityMode.Message;
            notificationServiceBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
            ///turn on security


            notificationServiceClient.ClientCredentials.UserName.UserName = "******"; // your VendorID (1234)
            notificationServiceClient.ClientCredentials.UserName.Password = "******";
            HeartbeatTimer = new Timer(PingWinmax, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
            try
            {
                notificationServiceClient.Open();
            }
            catch (Exception e)
            {
                Messages.Text += "Failed to Connect:\n" + e.Message + "\n" + e.StackTrace;
            }
        }