using System.Reactive.Linq;
using System.Threading.Tasks;
using Workstation.ServiceModel.Ua;
using Workstation.ServiceModel.Ua.Channels;
public static async Task Main()
var clientDescription = new ApplicationDescription
ApplicationName = "Workstation.UaClient.FeatureTests",
ApplicationUri = $"urn:{System.Net.Dns.GetHostName()}:Workstation.UaClient.FeatureTests",
ApplicationType = ApplicationType.Client
var channel = new UaTcpSessionChannel(
"opc.tcp://opcua.rocks:4840",
SecurityPolicyUris.None);
await channel.OpenAsync();
Console.WriteLine($"Opened session with endpoint '{channel.RemoteEndpoint.EndpointUrl}'.");
Console.WriteLine($"SecurityPolicy: '{channel.RemoteEndpoint.SecurityPolicyUri}'.");
Console.WriteLine($"SecurityMode: '{channel.RemoteEndpoint.SecurityMode}'.");
Console.WriteLine($"UserIdentityToken: '{channel.UserIdentity}'.");
var req = new CreateSubscriptionRequest
RequestedPublishingInterval = 1000.0,
RequestedMaxKeepAliveCount = 30,
RequestedLifetimeCount = 30 * 3,
PublishingEnabled = true,
var res = await channel.CreateSubscriptionAsync(req);
var id = res.SubscriptionId;
Console.WriteLine($"Created subscription '{id}'.");
var req2 = new CreateMonitoredItemsRequest
TimestampsToReturn = TimestampsToReturn.Both,
ItemsToCreate = new MonitoredItemCreateRequest[]
new MonitoredItemCreateRequest
ItemToMonitor= new ReadValueId{ AttributeId= AttributeIds.Value, NodeId= NodeId.Parse(VariableIds.Server_ServerStatus_CurrentTime)},
MonitoringMode= MonitoringMode.Reporting,
RequestedParameters= new MonitoringParameters{ ClientHandle= 42, QueueSize= 2, DiscardOldest= true, SamplingInterval= 1000.0},
var res2 = await channel.CreateMonitoredItemsAsync(req2);
Console.WriteLine("Subscribe to PublishResponse stream for 5s.");
.Where(pr => pr.SubscriptionId == id)
var dcns = pr.NotificationMessage.NotificationData.OfType<DataChangeNotification>();
foreach (var dcn in dcns)
foreach (var min in dcn.MonitoredItems)
Console.WriteLine($"sub: {pr.SubscriptionId}; handle: {min.ClientHandle}; value: {min.Value}");
ex => Console.WriteLine("Exception in publish response handler: {0}", ex.GetBaseException().Message)
Console.WriteLine($"\nClosing session '{channel.SessionId}'.");
await channel.CloseAsync();
await channel.AbortAsync();
Console.WriteLine(ex.Message);