How to resolve handshake with cloud mlab mongodb when connection type is SSL?

How to resolve handshake with cloud mlab mongodb when connection type is SSL?

Override the “UpdateMongoDriverSettingsProcessor” pipeline.

 

Sample code :

 

public class UpdateMongoDriverSettings: UpdateMongoDriverSettingsProcessor

{

/// <summary>

/// This pipeline is override for conection delay or handshake with cloud mlab mongodb when connection type is SSL

/// </summary>

/// <param name=”args”></param>

public override void UpdateSettings(UpdateMongoDriverSettingsArgs args)

{

Assert.ArgumentNotNull(args, nameof(args));

Int32 defaultValue = 120;

 

Int32 socketTime = String.IsNullOrEmpty(string.Format(CultureInfo.InvariantCulture,Settings.GetSetting(“MongoDB.SocketTimeout”))) ? defaultValue : Int32.Parse(Settings.GetSetting(“MongoDB.SocketTimeout”), CultureInfo.InvariantCulture);

Int32 ConnectTimeout = String.IsNullOrEmpty(Settings.GetSetting(“MongoDB.SocketTimeout”)) ? defaultValue : Int32.Parse(Settings.GetSetting(“MongoDB.ConnectTimeout”),CultureInfo.InvariantCulture);

Int32 WaitQueueTimeout = String.IsNullOrEmpty(Settings.GetSetting(“MongoDB.SocketTimeout”)) ? defaultValue : Int32.Parse(Settings.GetSetting(“MongoDB.WaitQueueTimeout”),CultureInfo.InvariantCulture);

 

if (args != null)

{

args.MongoSettings.SocketTimeout = new TimeSpan(0, 0, 0, socketTime);

args.MongoSettings.ConnectTimeout = new TimeSpan(0, 0, 0, ConnectTimeout);

args.MongoSettings.WaitQueueTimeout = new TimeSpan(0, 0, 0, WaitQueueTimeout);

}

Log.Info(String.Format(CultureInfo.InvariantCulture,”UpdateMongoDriverSettingsArgs changed”), this);

}

 

}

 

 

 

<configuration xmlns:patch=”www.sitecore.net/xmlconfig”>

<sitecore>

<pipelines>

<updateMongoDriverSettings>

<processor type=”MogoDbExtension.UpdateMongoDriverSettings, Assembly Name” />

</updateMongoDriverSettings>

</pipelines>

 

<settings>

<setting name=”MongoDB.SocketTimeout” value=”120″ />

<setting name=”MongoDB.ConnectTimeout” value=”120″ />

<setting name=”MongoDB.WaitQueueTimeout” value=”120″ />

</settings>

</sitecore>

</configuration>

 

Advertisements

How to do Configuration patching in Sitecore

In Real development many time we find lot of scenario where we have to patch the configuration. In Sietcore there is one thumb rule that all type of patching configuration file will add under “\App_Config\include” folder.

All given below patch section will add only under <sitecore> tag in config file. Sitecore have custom module which dynamically added all configuration at runtime.

Following are some requirements and examples.

  • Merging the config files: -A

    s example in Sitecore all setting will exist in Sitecore.config file under Include folder.

As example if you want to create a switcher for any logic that you can enable and disable the execution of code from configuration.

Create one new config File name as “z.SitecoreSetting.config” and add setting in this file as follows:

<setting name=”SwitchOnOff” value=”true” /> 

Note: At run time this setting will be add in Sitecore.config file.

  • How to add or update attribute of any configuration: – We can add the new attribute or update the existing attribute value also.

As example you got the requirement from Client that language will not append in Web URL.

Following are the step to implement this:

Create one new config File name as “z.SitecoreLinkManger.config” and add setting in this file as follows:

You have to add  “patch:attribute” tag containg the name of existing attribute and new value.

“<configuration  xmlns:patch=”http://www.sitecore.net/xmlconfig/&#8221; xmlns:set=”http://www.sitecore.net/xmlconfig/set/”&gt;

<sitecore>

<linkManager defaultProvider=”sitecore”>

<providers>

<add name=”sitecore”>

<patch:attribute name=”languageEmbedding”>never</patch:attribute>

</add>

</providers>

</linkManager>

</sitecore>

</configuration>

 

  • How to insert the config element before or after specific configuration?

In this requirement we have to add out custom pipeline, events or etc. Please see the below httpbegin request pipeline example.

Patch:before  Example

<configuration xmlns:patch=”http://www.sitecore.net/xmlconfig/”&gt;

<sitecore>

<pipelines>

<httpRequestBegin>

<processor type=”Your custom class name”,   DLL Name”      patch:before=”processor[@type=’Sitecore.Pipelines.HttpRequest.LayoutResolver, Sitecore.Kernel’]”/>

</httpRequestBegin>

</pipelines>

</sitecore>

</configuration>

 

patch:after Sitecore Example:-

 

<!– Sitecore.config –>

<test>

<processor type=”test1″ />

<processor type=”test2″ />

<processor type=”test3″ />

</test>

<!– Patch file –>

<test>

<processor type=”testA” patch:after = “processor[@type=’test2′]”/>

</test>

The configuration used at runtime is:

<test>  <processor type=”test1″ />

<processor type=”test2″ /><processor type=”testA” />  <processor type=”test3″ />

</test>

<processor type=”test2″ />

<processor type=”testA” />

<processor type=”test3″ />

</test>

Use Handlebar template to bind JSON from Sitecore MVC Controller

In Sitecore MVC as we all know how to bind data synchronously by using inbuilt View and controller rendering but now  we will see in this article how we can bind the Sitecore Item data as json to view using handlebar template framework in asynchronous call.

Following are the steps we have to follow:

  1. Add handlebar framework reference to your layout. Handlebar file is hosted on cdn also it is up to you to direct reference from cdn or download and add to your solution

https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.10/handlebars.amd.js

  1. Create MVC controller inheriting the MVC/Sitecore MVc base controller.
  2. Add action named as example GetArticle() to controller class and return type would ben JSON result set by using Json() method
  3. Create one repository class which bind the model.
  4. Register route for controller override the Sitecore MVC pipeline.
  5. Implement AJAX call and bind with the handlebar template

Controller Class:

public class ArticleController : Controller

{

private readonly IArticleRepository _articleRepository;

public ArticleController () : this(new ArticlerRepository ())

{

}

public AccountsController(IArticleRepository articleRepository)

{

this. _articleRepository = articleRepository;

}

 

[HttpGet]

public JsonResult GetArticle ()

{

List<Article> lstArticle=     this. _articleRepository.GetArticle();

return Json(lstArticle, JsonRequestBehavior.AllowGet);

}

}

Model Class:

public class Article

{

public  string Url { get; set; }

public string Title { get; set; }

public string ShortDescription { get; set; }

}

Repository class interface: Create interface for repository class

interface IArticleRepository

{

string List<Article> GetArticle();

}

Repository class:

public class ArticlerRepository : IArticleRepository    {

/// <summary>

/// Get article sitecore

/// </summary>

/// <returns></returns>

 

public List<Article> GetArticle()
{

List<Article> lstArticle = new List<Article>();
Item aticleRootItem = Mvc.Presentation.RenderingContext.Current.Rendering.Item;

if (aticleRootItem != null && aticleRootItem.HasChildren)
{
foreach (Item childrenItem in aticleRootItem.Children)
{
Article objArticle = new Article();
if (childrenItem.IsDerived(Templates.Article.ID))
{

objArticle.Url = childrenItem.Fields[“Url”]!=null?childrenItem.Fields[“Url”].Value : string.Empty;
objArticle.Title= childrenItem.Fields[“Title”] != null ? childrenItem.Fields[“Title”].Value : string.Empty;
objArticle.ShortDescription = childrenItem.Fields[“ShortDescription”] != null ? childrenItem.Fields[“ShortDescription”].Value : string.Empty;

lstArticle.Add(objArticle);
}
}
}

return lstArticle;
}

}

 

Register route for controller override the Sitecore MVC pipeline

<pipelines>
<initialize>
<processor type=”RegisterWebApiRoutes, dll name”
patch:before=”processor[@type=’Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc’]” />
</initialize>
</pipelines>

using Sitecore.Pipelines;
using System.Web.Mvc;
using System.Web.Routing;
/// <summary>
/// Register Routes for controller
/// </summary>
public class RegisterWebApiRoutes
{
public void Process(PipelineArgs args)
{
RouteTable.Routes.MapRoute(“Api”, “api/Article/{action}”, new
{
controller = “Article”
});
}
}

Bind article Data on UI using Handlebar framework: Create one Sitecore MVC rendering view named “Articlelst.cshtml”and this handlebar template for article list.

//Handle bar template will rendered articles on page load

*************************************************************************************

Handle Bar template definition for Article list

*************************************************************************************

“<” script id=”application-declined-template” type=”text/x-handlebars-template” “>”

{{#each lstArticle }}

{{{Title}}}

{{{Title}}}

{{{ShortDescription}}}

{{/each}}

“<” / script “>”

Add this JavaScript function to View on document ready function:

$(document).ready(function () {

function GetArticle() {

var url = ‘api/Article/GetArticle’;

$.ajax({

type: get,

contentType: ‘application/json; charset=UTF-8’,

url: url,

cache: false,

success: function (data, status) {

var articleData = JSON.parse($.trim(data.lstArticle));

var source = $(“#article-template”).html();

var template = Handlebars.compile(source);

$(‘.Article-content’).empty();

$(‘.Article-content’).append(template(articleData));

},

error: function (status) {

console.log(“error”, data);

}

});

}

});

 

 

Handlebar reference urls:

https://www.sitepoint.com/a-beginners-guide-to-handlebars/

http://javascriptissexy.com/handlebars-js-tutorial-learn-everything-about-handlebars-js-javascript-templating/

http://handlebarsjs.com/