private static void Configure(string profile, bool ignoreCertificateErrors, bool noProtection) { // read configuration values var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile("appsettings.json", false) .AddJsonFile($"{profile}.json", false); var configurationRoot = builder.Build(); // configure logging _loggerFactory = new LoggerFactory(); _loggerFactory.AddConsole(configurationRoot.GetSection("Logging")); _logger = _loggerFactory.CreateLogger("TestClient"); // configure caching var distributedCache = DistributedCacheFactory.Create(); // set up address registry var addressRegistrySettings = new AddressRegistrySettings(); configurationRoot.GetSection("AddressRegistrySettings").Bind(addressRegistrySettings); addressRegistrySettings.WcfConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var addressRegistry = new AddressRegistry(addressRegistrySettings, distributedCache); // set up collaboration registry var collaborationProtocolRegistrySettings = new CollaborationProtocolRegistrySettings(); configurationRoot.GetSection("CollaborationProtocolRegistrySettings").Bind(collaborationProtocolRegistrySettings); collaborationProtocolRegistrySettings.WcfConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var collaborationProtocolRegistry = new CollaborationProtocolRegistry(collaborationProtocolRegistrySettings, distributedCache, addressRegistry); _clientSettings = new ClientSettings(); configurationRoot.GetSection("ClientSettings").Bind(_clientSettings); // set up messaging var messagingSettings = new MessagingSettings(); configurationRoot.GetSection("MessagingSettings").Bind(messagingSettings); messagingSettings.IgnoreCertificateErrorOnSend = ignoreCertificateErrors; messagingSettings.LogPayload = true; if (noProtection) { _messagingClient = new MessagingClient(messagingSettings, collaborationProtocolRegistry, addressRegistry, null, null, new NoMessageProtection()); } else { _messagingClient = new MessagingClient(messagingSettings, collaborationProtocolRegistry, addressRegistry); } }
internal static AddressRegistryMock GetDefaultAddressRegistryMock() { var settings = new AddressRegistrySettings() { UserName = "******", Password = "******", EndpointName = "BasicHttpBinding_ICommunicationPartyService", WcfConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None), CachingInterval = TimeSpan.FromSeconds(5) }; var memoryCache = new MemoryCache(new MemoryCacheOptions()); var distributedCache = new MemoryDistributedCache(memoryCache); var registry = new AddressRegistryMock(settings, distributedCache); registry.SetupFindCommunicationPartyDetails(i => { if (i < 0) { throw new FaultException(new FaultReason("Her-ID expected to an integer of positive value.")); } var file = Path.Combine("Files", $"CommunicationDetails_{i}.xml"); return(File.Exists(file) == false ? null : XElement.Load(file)); }); registry.SetupGetCertificateDetailsForEncryption(i => { if (i < 0) { throw new FaultException(new FaultReason("Her-ID expected to an integer of positive value.")); } var file = Path.Combine("Files", $"GetCertificateDetailsForEncryption_{i}.xml"); return(File.Exists(file) == false ? null : XElement.Load(file)); }); registry.SetupGetCertificateDetailsForValidatingSignature(i => { if (i < 0) { throw new FaultException(new FaultReason("Her-ID expected to an integer of positive value.")); } var file = Path.Combine("Files", $"GetCertificateDetailsForValidatingSignature_{i}.xml"); return(File.Exists(file) == false ? null : XElement.Load(file)); }); return(registry); }
internal void SetupInternal(int otherHerId) { var addressRegistrySettings = new AddressRegistrySettings() { UserName = "******", Password = "******", EndpointName = "SomeEndpointName", WcfConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None), CachingInterval = TimeSpan.FromSeconds(5) }; var collaborationRegistrySettings = new CollaborationProtocolRegistrySettings() { UserName = "******", Password = "******", EndpointName = "SomeEndpointName", WcfConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None), CachingInterval = TimeSpan.FromSeconds(5) }; LoggerFactory = new LoggerFactory(); MockLoggerProvider = new MockLoggerProvider(null); LoggerFactory.AddProvider(MockLoggerProvider); //LoggerFactory.AddDebug(); //Logger = new LoggerMock(LoggerFactory.CreateLogger<BaseTest>()); Logger = LoggerFactory.CreateLogger <BaseTest>(); var memoryCache = new MemoryCache(new MemoryCacheOptions()); var distributedCache = new MemoryDistributedCache(memoryCache); AddressRegistry = new AddressRegistryMock(addressRegistrySettings, distributedCache); AddressRegistry.SetupFindCommunicationPartyDetails(i => { var file = Path.Combine("Files", $"CommunicationDetails_{i}.xml"); return(File.Exists(file) == false ? null : XElement.Load(file)); }); AddressRegistry.SetupGetCertificateDetailsForEncryption(i => { var path = Path.Combine("Files", $"GetCertificateDetailsForEncryption_{i}.xml"); return(File.Exists(path) == false ? null : XElement.Load(path)); }); AddressRegistry.SetupGetCertificateDetailsForValidatingSignature(i => { var path = Path.Combine("Files", $"GetCertificateDetailsForValidatingSignature_{i}.xml"); return(File.Exists(path) == false ? null : XElement.Load(path)); }); CollaborationRegistry = new CollaborationProtocolRegistryMock(collaborationRegistrySettings, distributedCache, AddressRegistry); CollaborationRegistry.SetupFindProtocolForCounterparty(i => { var file = Path.Combine("Files", $"CPP_{i}.xml"); return(File.Exists(file) == false ? null : File.ReadAllText(file)); }); CollaborationRegistry.SetupFindAgreementForCounterparty(i => { var file = Path.Combine("Files", $"CPA_{i}.xml"); return(File.Exists(file) == false ? null : File.ReadAllText(file)); }); CollaborationRegistry.SetupFindAgreementById(i => { var file = Path.Combine("Files", $"CPA_{i:D}.xml"); return(File.Exists(file) == false ? null : File.ReadAllText(file)); }); Settings = new MessagingSettings() { MyHerId = MockFactory.HelsenorgeHerId, SigningCertificate = new CertificateSettings() { Certificate = TestCertificates.HelsenorgePrivateSigntature }, DecryptionCertificate = new CertificateSettings() { Certificate = TestCertificates.HelsenorgePrivateEncryption } }; Settings.ServiceBus.ConnectionString = "connection string"; Settings.ServiceBus.Synchronous.ReplyQueueMapping.Add(Environment.MachineName.ToLower(), "RepliesGoHere"); // make things easier by only having one processing task per queue Settings.ServiceBus.Asynchronous.ProcessingTasks = 1; Settings.ServiceBus.Synchronous.ProcessingTasks = 1; Settings.ServiceBus.Error.ProcessingTasks = 1; MockFactory = new MockFactory(otherHerId); CertificateValidator = new MockCertificateValidator(); Client = new MessagingClient(Settings, CollaborationRegistry, AddressRegistry) { DefaultMessageProtection = new NoMessageProtection(), // disable protection for most tests DefaultCertificateValidator = CertificateValidator }; Client.ServiceBus.RegisterAlternateMessagingFactory(MockFactory); Server = new MessagingServer(Settings, Logger, LoggerFactory, CollaborationRegistry, AddressRegistry) { DefaultMessageProtection = new NoMessageProtection(), // disable protection for most tests DefaultCertificateValidator = CertificateValidator }; Server.ServiceBus.RegisterAlternateMessagingFactory(MockFactory); }
private static void Configure(string profile) { // read configuration values var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile("appsettings.json", false) .AddJsonFile($"{profile}.json", false); var configurationRoot = builder.Build(); // configure logging _loggerFactory = new LoggerFactory(); _loggerFactory.AddConsole(configurationRoot.GetSection("Logging")); _loggerFactory.AddNLog(); LogManager.Configuration = new XmlLoggingConfiguration("nlog.config", true); _logger = _loggerFactory.CreateLogger("TestServer"); // configure caching var distributedCache = DistributedCacheFactory.Create(); // set up address registry var addressRegistrySettings = new AddressRegistrySettings(); configurationRoot.GetSection("AddressRegistrySettings").Bind(addressRegistrySettings); addressRegistrySettings.WcfConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var addressRegistry = new AddressRegistry(addressRegistrySettings, distributedCache); // set up collaboration registry var collaborationProtocolRegistrySettings = new CollaborationProtocolRegistrySettings(); configurationRoot.GetSection("CollaborationProtocolRegistrySettings").Bind(collaborationProtocolRegistrySettings); collaborationProtocolRegistrySettings.WcfConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var collaborationProtocolRegistry = new CollaborationProtocolRegistry(collaborationProtocolRegistrySettings, distributedCache, addressRegistry); _serverSettings = new ServerSettings(); configurationRoot.GetSection("ServerSettings").Bind(_serverSettings); // set up messaging var messagingSettings = new MessagingSettings(); configurationRoot.GetSection("MessagingSettings").Bind(messagingSettings); messagingSettings.ServiceBus.Synchronous.ReplyQueueMapping.Add(Environment.MachineName, "DUMMY"); // we just need a value, it will never be used messagingSettings.LogPayload = true; _messagingServer = new MessagingServer(messagingSettings, _logger, _loggerFactory, collaborationProtocolRegistry, addressRegistry); _messagingServer.RegisterAsynchronousMessageReceivedStartingCallback((m) => { MappedDiagnosticsLogicalContext.Set("correlationId", m.MessageId); }); _messagingServer.RegisterAsynchronousMessageReceivedCallback((m) => { if (m.Payload.ToString().Contains("ThrowException")) { throw new InvalidOperationException(); } var path = Path.Combine(_serverSettings.DestinationDirectory, "Asynchronous"); if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } var fileName = Path.Combine(path, m.MessageId + ".xml"); using (var sw = File.CreateText(fileName)) { m.Payload.Save(sw); } }); _messagingServer.RegisterAsynchronousMessageReceivedCompletedCallback((m) => { MappedDiagnosticsLogicalContext.Set("correlationId", m.MessageId); }); _messagingServer.RegisterSynchronousMessageReceivedStartingCallback((m) => { MappedDiagnosticsLogicalContext.Set("correlationId", string.Empty);// reset correlation id }); _messagingServer.RegisterSynchronousMessageReceivedCallback((m) => { var path = Path.Combine(_serverSettings.DestinationDirectory, "Synchronous"); if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } var fileName = Path.Combine(path, m.MessageId + ".xml"); using (var sw = File.CreateText(fileName)) { m.Payload.Save(sw); } return(new XDocument(new XElement("DummyResponse"))); }); _messagingServer.RegisterSynchronousMessageReceivedCompletedCallback((m) => { MappedDiagnosticsLogicalContext.Set("correlationId", string.Empty); // reset correlation id }); }
/// <summary> /// Constructor /// </summary> /// <param name="settings"></param> /// <param name="cache"></param> public AddressRegistryMock( AddressRegistrySettings settings, IDistributedCache cache) : base(settings, cache) { }