User Tools

Site Tools


nebolintegration_tutorial_1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
nebolintegration_tutorial_1 [2021/08/11 21:37] nebolnebolintegration_tutorial_1 [2021/08/11 22:55] (current) – removed nebol
Line 1: Line 1:
-==== NebolIntegration Tutorial 1: Creating your first integration ==== 
  
-We will build a simple .NET Core Console application with SQL LocalDB 
- 
-==== NOT DONE! WORK IN PROGRESS! ==== 
- 
-Create a .NET Core 3.1 Console application 
- 
-We need a bunch of packages, first we get the Nebol packages: 
- 
-<code> 
-Install-Package NebolIntegration 
-Install-Package NebolIntegration.EntityFramework 
-</code> 
- 
-And then the additional ones: 
-<code> 
-Install-Package SharpRepository.EfCoreRepository # EF Core support for SharpRepository 
-Install-Package Microsoft.EntityFrameworkCore.SqlServer # MS SQL support for Entity Framework 
-Install-Package SharpRepository.IocMicrosoft.DependencyInjection # MS dependency injection support for SharpRepository 
-Install-Package NewtonSoft.Json # needed for some JSON converting later 
-</code> 
- 
-SharpRepository.EfCoreRepository is needed for EF Core support for SharpRepository 
-Microsoft.EntityFrameworkCore.SqlServer is needed for MS SQL support for Entity Framework 
-SharpRepository.IocMicrosoft.DependencyInjection is needed for MS dependency injection support for SharpRepository 
-NewtonSoft.Json is needed for some JSON converting later on 
- 
-The SharpRepository.EfCoreRepository installation will add a default settings file to your project named repository.efCore.json file, it's for the configuration of SharpRepository. Set the "Copy to Output Directory" property of the file to "Copy if newer". 
- 
-Replace the dbContextType line with: 
- 
-<code> 
-"dbContextType": "NebolIntegration.EntityFramework.NebolIntegrationDbContext, NebolIntegration.EntityFramework" 
-</code> 
- 
-The connection string will look something like this for SQL LocalDB: 
-<code> 
-"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=integration;AttachDBFilename=E:\NebolIntegrationSample.mdf"; 
-</code> 
- 
-You can add it to the repository.efCore.json file or use DI to add it in code in a way you prefer. 
- 
-Now let's get coding.  
- 
-First, the actual integration. We'll use a public webservice that just returns a UUID. Very simple. 
- 
-We create a class derived from SystemIntegration. We supply it with a few parameters that specifies how the integration is managed, and we override the ProcessRequest() and ProcessResponse() methods. 
-The ProcessRequest() method is called when a new task is found in the queue. It does what needs to be done, in this case call a simple webservice. The ProcessResponse() handles the response recieved from the webservice, in this case it just outputs the UUID that is fetched. 
- 
-<code> 
-public class FetchedUUID 
-{ 
- public string UUID { get; set; } 
-} 
- 
-public class FetchUUID : SystemIntegration 
-{ 
- // this name is used to uniquely identify this integration 
- public static readonly new string TechnicalName = "FETCH_UUID"; 
- 
- public FetchUUID(ISharpRepositoryConfiguration config)  
- : base(config, TechnicalName) 
- { 
- Name = "UUID Fetcher"; // friendly name 
- Description = "Fetches a new UUID from httpbin.org"; 
- RetryScheme = "30sx3";  
- CleaningScheme = "?"; // ignore for now 
- Direction = DirectionEnum.Outgoing; 
- ExpectedSecondsToComplete = 10;  
- PollingIntervalSeconds = 60 * 3; // three minutes 
- ReferenceDescription = string.Empty; // no reference in this integration 
- 
- InitiateIntegration(); 
- } 
- 
- public override IntegrationResponse ProcessRequest(IntegrationQueue iqueue) 
- { 
- IntegrationResponse resp = new IntegrationResponse(); 
- 
- // any exception will be caught by caller so this is fine 
- using (var wc = new System.Net.WebClient()) 
- resp.Response = wc.DownloadString("https://httpbin.org/uuid"); 
- 
- resp.Status =  StatusEnum.Success; 
- 
- return (resp); 
- } 
- 
- public override IntegrationResponse ProcessResponse(IntegrationQueue iqueue, IntegrationResponse response) 
- { 
- var fuuid = JsonConvert.DeserializeObject<FetchedUUID>(response.Response); 
- 
- Console.WriteLine("UUID: " + fuuid.UUID); 
- 
- return (response); 
- } 
-} 
-</code> 
- 
- 
-Now let's set everything up. 
- 
-<code> 
-static void SetupIntegration() 
-{ 
- var config = new ConfigurationBuilder() 
- .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) 
- .AddJsonFile("repository.efCore.json") 
- .Build(); 
- 
- var section = config.GetSection("sharpRepository"); 
- ISharpRepositoryConfiguration sharpConfig = RepositoryFactory.BuildSharpRepositoryConfiguation(section); 
- 
- // roundabout way to get connection string from the config section 
- var connection_string = sharpConfig.Repositories.Where(item => item.Name.Equals("efCore")).First() 
- .Attributes.Where(item => item.Key.Equals("connectionString")).First().Value; 
- 
- // create an instance of the DB context so we can create the schema (if it doesn't exist) 
- var optionsBuilder = new DbContextOptionsBuilder<NebolIntegrationDbContext>(); 
- optionsBuilder.UseSqlServer(connection_string); 
- using (var db = new NebolIntegrationDbContext(optionsBuilder.Options)) 
- db.Database.EnsureCreated(); 
- 
- // setup dependency injection 
- services = new ServiceCollection(); 
- services.AddDbContext<NebolIntegrationDbContext>(options => options.UseSqlServer(connection_string), ServiceLifetime.Transient); 
- services.UseSharpRepository(section); 
- var serviceProvider = services.BuildServiceProvider(true); 
- 
- // create integration engine 
- engine = new Engine(sharpConfig); 
- 
- // create instance of our integration class 
- var fetchuuid = new FetchUUID(sharpConfig); 
- 
- // add it to the engine 
- engine.SystemIntegrations.Add(fetchuuid); 
-} 
-</code> 
- 
-NebolIntegration takes advantage of NebolLogger, so we will set that up as well. In this case we'll just use the color-coded console logger: 
- 
-<code> 
-protected static void InitLoggingFramework() 
-{ 
- Destination cc = new Destination_ColorConsole(); 
- Logger.Instance.Destinations.Add(cc); 
-} 
-</code> 
- 
-Now let's start the integration engine: 
-<code> 
-engine.StartIntegrations(); 
-</code> 
- 
-And we'll add a basic console loop: 
-<code> 
-string command; 
-Console.WriteLine("Press ENTER to submit request to integration engine. Q to quit."); 
-do 
-{ 
- command = Console.ReadLine(); 
- 
- if (command.ToLower() != "q") 
- engine.Submit(FetchUUID.TechnicalName); 
- 
-while (command.ToLower() != "q"); 
-</code> 
- 
-(In a real-world scenario, the submit "engine.Submit(FetchUUID.TechnicalName)" will likely be made in a different application, on a different computer, for example a webserver where the user clicks a button on a webpage, but for the sake of simplicity we're doing it here in the same sample application.) 
- 
-Finally, the cleanup: 
- 
-<code> 
-engine.StopIntegrations(); 
-</code> 
- 
-This will shutdown the engine NICELY. It will not abort a running integration job. If you have multiple integrations it will signal them all that it's time to shutdown then wait nicely until they are all done. 
- 
-When run, the program should output this: 
- 
-{{:nebolintegration_sample_1_console1.png?nolink|}} 
-(include logger & output) 
- 
-As instructed, press ENTER to submit a request to the engine.  
- 
-{{:nebolintegration_sample_1_console2.png?nolink|}} 
- 
-Congrats! A call was made to the webservice and the responce processed. Your integration is up and running! 
- 
-Next: [[NebolIntegration_Tutorial_2]] - So what happened? 
nebolintegration_tutorial_1.1628710673.txt.gz · Last modified: 2021/08/11 21:37 by nebol