Uploaded image for project: 'Couchbase Documentation'
  1. Couchbase Documentation
  2. DOC-12148

Couchbase Lite Dotnet Sample Code is doesnt run out of the box

    XMLWordPrintable

Details

    • Task
    • Resolution: Unresolved
    • Major
    • None
    • None
    • couchbase-lite
    • None

    Description

      The dotnet SDK for Couchbase Lite doesnt work out of the box, it will exit out of the program before the replication has completed. It instead has a comment suggesting that you should wait on the replication, instead of having a sample of how to wait. When a customer is just trying it out quickly they may not realise they have to do this.

      The sample code that can replace it is shown below for this page:

      using System;
      using Couchbase.Lite;
      using Couchbase.Lite.Logging;
      using Couchbase.Lite.Query;
      using Couchbase.Lite.Sync;
       
       
       
       
      // Get the database (and create it if it doesn't exist)
      var database = new Database("mydb");
      var collection = database.GetDefaultCollection();
       
       
      Database.Log.Console.Domains=LogDomain.All;
      Database.Log.Console.Level=LogLevel.Verbose;
       
      // Create a new document (i.e. a record) in the database
      var id = default(string);
      using var createdDoc = new MutableDocument();
      createdDoc.SetFloat("version", 2.0f)
      .SetString("type","SDK");
       
      // Save it to the database
      collection.Save(createdDoc);
      id = createdDoc.Id;
       
      // Update a document
      using var doc = collection.GetDocument(id);
      using var mutableDoc = doc.ToMutable();
      createdDoc.SetString("language", "C#");
      collection.Save(createdDoc);
       
      using var docAgain = collection.GetDocument(id);
      Console.WriteLine($"Document ID :: \{docAgain.Id}");
      Console.WriteLine($"Learning \{docAgain.GetString("language")}");
       
       
      // Create a query to fetch documents of type SDK
      // i.e. SELECT * FROM database WHERE type = "SDK"
      using var query = QueryBuilder.Select(SelectResult.All())
      .From(DataSource.Collection(collection))
      .Where(Expression.Property("type").EqualTo(Expression.String("SDK")));
       
      // Run the query
      var result = query.Execute();
      Console.WriteLine($"Number of rows :: \{result.AllResults().Count}");
       
      // Create replicator to push and pull changes to and from the cloud
      var targetEndpoint = new URLEndpoint(new Uri("ws://localhost:4984/traveldb"));
      var replConfig = new ReplicatorConfiguration(targetEndpoint);
      replConfig.AddCollection(database.GetDefaultCollection());
       
      // Add authentication
      stringpassword="passwordstring";
      System.Security.SecureStringsecureString=newSystem.Security.SecureString();
      for(inti=0;i<password.Length;i++)
      {
      secureString.AppendChar(password[i]);
      }
      replConfig.Authenticator = new BasicAuthenticator("sgwuser1",secureString);
       
      // Create replicator (make sure to add an instance or static variable
      // named _Replicator)
      var replicator = new Replicator(replConfig);
      replicator.AddChangeListener((sender, args) =>
      {
      if(args.Status.Error!=null){
      Console.WriteLine($"Error :: \{args.Status.Error}");
      }
      });
       
      replicator.Start();
       
       
      while (true)
      {
      if(replicator.Status.Activity==ReplicatorActivityLevel.Idle||replicator.Status.Activity==ReplicatorActivityLevel.Stopped)
      break;
      }
       
      replicator.Stop();
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            elliot.hunter Elliot Hunter
            felix.seanor Felix Seanor
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes

                PagerDuty