BizTalk Automated migration

Microsoft has recently released BizTalk 2020 with lots of new features and enhancements.

There are many reasons to Migrate and can be broadly summarized as below –

  1. Avail Microsoft Support
    • BizTalk 2016 Mainstream Support Ends – 1st November 2022
    • All other previous versions like 2013R2, 2013 or 2010 etc. are long out of mainstream support
    • Upgrade to latest software version of OS, VS, SQL & BizTalk
  2. Enhance BizTalk Environment
    • For better infrastructure, performance, topology – HA/DR
  3. Avail New features
    • BizTalk 2020 is loaded with new features of Hybrid Integration, Improvised Security (TLS1.2, GDPR compliance), Better Monitoring etc.
    • Feature packs – Get new features faster, without waiting for new versions
    • New Features for Developers and Administrators
  4. Move to Cloud
    • Ability to run on Azure VM as IaaS

Microsoft has always attempted to make migrations as seamless and easy as possible by providing various options like –

  1. In-Place Upgrade – Only possible for N-1 version
  2. BizTalk Server Migration Tool – Which performs as-is migration by exporting artifacts from previous environment to new environment.

In-Place Upgrade is more like installing Cumulative Updates or Feature Pack. It requires minimum efforts and is mostly very seamless but has many limitations like only possible for N-1 version, rollback is tough.

BizTalk Server Migration Tool is provided by MSIT and is extremely helpful for as-is migration. On high level it exports MSI, Binding Files etc. from old environment and imports in the new environment. So all the dlls still point to previous version and nothing is changed.

For as-is migration, it definitely saves lots of time and efforts but has limitations like we can’t use new features or enhancements.

Therefore, many organisations prefer to use traditional method of code migration, which allows code upgrade, enhancement and possibility of modernization. But, this process is definitely very lengthy, time taking and resource intensive.

As a BizTalk Developer, Migration operations can be broadly categorized as below –

  1. Migrate to Latest Visual Studio – Open each solution in Visual Studio, by default it will upgrade solution to VS. It will also change the dotnet framework for all BizTalk projects.
  2. DotNet Framework Change – You may have to change DotNet Framework for Class Library Projects like Pipeline Components, Helper/Components, Custom Functoids, Custom Adapters etc. as they aren’t changed with VS Upgrade.
  3. Add BTDF or other Deployment Project (Optional)
  4. Rebuild Solution – Review failures and fix them.
  5. E2E Testing – End to end testing with all the parties involved in integration.

Performing all these operations for hundreds and thousands of applications is extremely tedious, time taking and resource intensive work. Every year companies spend thousands of man-hours just to perform these migration activities.

We attempted to reduce the efforts for developers by automating most of the routine tasks and only focus on things which really require human intervention.

We created a tool which solves top 4 problems and performs them as One-Click Operation.

Operations which can be Automated By Tool –

  1. Upgrade Solutions to Latest Visual Studio
  2. Change .Net Framework for all Projects
  3. Add BTDF Project to solution, with proper changes to Deployment.btdfproj file
  4. Rebuild Solution
  5. Detailed Logging of each operation for quick access to successful and failed solutions

Proposed Steps –

  1. “Browse for Folder with all applications”
  2. Get List of all the Solutions in that folder.
  3. Choose from Migration Operations –
    • Upgrade – This will upgrade to latest visual studio version.
    • Add BTDF Project – Adds BTDF Template project will all the files
    • Build – Performs Rebuild of all the solutions/projects
    • Change .Net Framework to Target Version – Select target Dotnet framework from the dropdown. All the projects will be migrated to this version, especially helpful for helper libraries like pipeline component, custom functoids, adapters and other helper components
  4. Detailed logging can be performed for each operation.
  5. You can also create folders and generate/move outputs to different folders like – “UpgradeSuccessful”, “UpgradeFailed”, “BuildSuccessful” and “BuildFailed”

This will allow developers to only focus on applications for which Build Operation failed.

Detailed Description of each operation –

Migrate Operation –

This operation is similar to manually Opening each solution in Visual Studio which performs

a) Upgrade to latest VS and

b) Change of .Net framework (to default version) for BizTalk projects (only BizTalk Projects(.btproj) not class library projects(.csproj)).

It also creates Backup folder with migrated projects and log file – “UpgradeLog.htm” next to the solution file. You can also choose to delete the backup file, to save space if you already have another backup.

To perform this operation from tool you can use visual studio command line arguments –

devenv {SolutionFile|ProjectFile} /Upgrade [/Out OutputFilename]

Refer –

https://docs.microsoft.com/en-us/visualstudio/ide/reference/upgrade-devenv-exe?view=vs-2019

Build Operation –

Similarly, Build operation can also be performed by VS command line arguments –

devenv SolutionName /Rebuild [SolnConfigName [/Project ProjName [/ProjectConfig ProjConfigName]] [/Out OutputFilename]]

Refer –

https://docs.microsoft.com/en-us/visualstudio/ide/reference/rebuild-devenv-exe?view=vs-2019

Most of the errors in this step is related to missing reference of common libraries or helper projects/dlls.

Change DotNet Framework –

You can allow users to choose target framework from a dropdown and later upgrade all projects in solution to this version. These values can be fetched from App.config file.

Your tool can get the list of all the projects referred/present in Solution (Solution file) and later change their .Net framework for each of them.

Regular VS Upgrade option performs .Net framework changes only to BizTalk projects and that too only to default version.

Note – For BizTalk 2020 we found a bug with .Net Framework 4.7.2, so we decided to migrate all the projects to 4.8 version. This tool was extremely helpful in mass framework change.

It’s also extremely helpful for helper class libraries like pipeline components, custom functoids, custom adapters or C# Helper/Component projects which are not upgraded in Upgrade process.

Dotnet Framework can be upgraded just by updating the project files.

  1. In the project file, locate the entry for the target Framework version. Ex -if your project is designed to use the .NET Framework 4.5, locate <TargetFrameworkVersion> v4.5 </ TargetFrameworkVersion > in the <PropertyGroup Label = “Globals” > element of the < Project > element.

If the<TargetFrameworkVersion> element isn’t present, your project doesn’t use the.NET Framework and no change is required.

2. Change the value to the Framework version you want, such as v4.7.2 or v4.8.

You can also use Regex pattern replace –

string regexPattern = “<TargetFrameworkVersion>*.*</TargetFrameworkVersion>”;

3. Save the changes and close the editor.

Refer –https://docs.microsoft.com/en-us/cpp/build/how-to-modify-the-target-framework-and-platform-toolset?view=vs-2019

Add BTDF Project –

In this step you can add a template of BTDF project with Deployment folder and all other files –

  • Deployment.btdfproj
  • InstallWizard.xml
  • License.rtf
  • PortBindingsMaster.xml
  • SettingsFileGenerator.xml
  • UnInstallWizard.xml

You can also update the Deployment.btdfproj file with many routine information like –

  • ProjectName – Solution File
  • Include Options for
    • Schemas
    • Maps/Transforms
    • Orchestrations
    • PipelineComponents
    • Helpers – Components
    • And many other options

These options can be read and populated by fetching project names or file’s extension from solution file.

For ex – If Solution contains projects with name as “Schemas” or files with extension “.xsd” then make IncludeSchemas as True.

Similarly, for Maps, Orchestrations, PipelineComponents etc. can be deduced either from name( if naming convention is followed for BizTalk projects) or file extensions like .btm for maps, .odx for orchestrations, .btp for pipelines etc.

Sample BTDF Template -
    <!-- Deploy schemas -->
    <IncludeSchemas>True</IncludeSchemas>
    <!-- Deploy orchestrations -->
    <IncludeOrchestrations>True</IncludeOrchestrations>
    <!-- Deploy maps -->
    <IncludeTransforms>True</IncludeTransforms>

Many sections like below which are mostly constant can be updated from App.Config file.

    <!-- Deploy custom functoids -->
    <IncludeCustomFunctoids>False</IncludeCustomFunctoids>
    <!-- Deploy BRE -->
    <IncludeVocabAndRules>False</IncludeVocabAndRules>
    <!-- Deploy IIS virtual directories -->
    <IncludeVirtualDirectories>False</IncludeVirtualDirectories>
    <!-- Deploy NUnit tests -->
    <IncludeDeploymentTest>False</IncludeDeploymentTest>
    <!-- Deploy Log4Net configuration file named $(ProjectName).log4net -->
    <Includelog4net>False</Includelog4net>

This tool has helped us in saving thousands of hours monotonous, repetitive and boring work.

Hope it helps you as well.

Feel free to get in touch in case of any query. Thanks.

Contact Me:- 

@Gmail@Facebook , @Twitter, @LinkedIn @MSDNTechnet, @My Personal Blog 

Related Article –

PLANNING MIGRATION TO BIZTALK 2020 – WHY AND HOW?

https://prashantbiztalkblogs.wordpress.com/2020/08/28/planning-migration-to-biztalk-2020-why-and-how/

Planning Migration to Biztalk 2020 – Why and how?

As BizTalk 2020 is out most of the customers have either started migration to planning to migrate.

In this short blog, I will try to give a broader picture of planning BizTalk Migration and answer most basic questions.

So, let’s start with the most mammoth question first –

Question 1 – Why to Migrate?

There are many reasons to Migrate and can be broadly summarized as below –

  1. Avail Microsoft Support
    • BizTalk 2016 Mainstream Support Ends – 1st November 2022
    • All other previous versions like 2013R2, 2013 or 2010 etc. are long out of mainstream support
    • Upgrade to latest software version of OS, VS, SQL & BizTalk
  2. Enhance BizTalk Environment
    • For better infrastructure, performance, topology – HA/DR
  3. New features
    • BizTalk 2020 is loaded with new features of Hybrid Integration, Improvised Security (TLS1.2, GDPR compliance), Better Monitoring etc.
    • Feature packs – Get new features faster, without waiting for new versions
    • New Features for Developers and Administrators
  4. Move to Cloud
    • Ability to run on Azure VM as IaaS

Question 2 – What are my Migration Options?

a) Migrate to Azure –

Migration from BizTalk to Logic Apps/Azure Integration Services –

FeaturesBizTalk ServerAzureCapability/Description
Message RoutingBizTalk MessageBoxAzure Service Bus Queues & Topics, Event GridUse Topics and subscriptions, Event-driven approach
SchemasBizTalk SchemasLogic Apps along with Integration AccountSchemas can be uploaded to Integration Accounts or converted to JSON Schema and used
MappingBizTalk Mapper, XSLT, C#Azure Integration AccountUpload map(XSL file) to integration account
PipelinesBizTalk Pipeline and pipeline componentsLogic Apps along with Function AppLogic App for workflow and Function App for pipeline component logic
AdaptersOut-of-the-box adapters (application and protocol)Logic AppConnectors
OrchestrationBizTalk OrchestrationLogic AppDefine Workflow definition
RulesBizTalk Rule Engine (BRE)Function AppCreate custom Azure Functions to perform BRE like operations
BAMBizTalk Activity MonitoringLogic App, OMSTracked-properties, OMS portal. Specially Log Analytics for Logic Apps is very helpful
EDI Parties, Aggreement, PartnersBizTalk EDIAzure B2B integration using Logic Apps and Integration AccountUse Integration accounts to manage EDI Parties, Aggreements, Partners, upload certificates, maps, schemas etc.
API (REST, SOAP)Bindings in adapters (WCF)API ManagementAPI Management offers OpenAPI Swagger, WSDL and many other API related functionalities. API Logic can be written in Logic Apps and Function App
OperationsBizTalk Admin ConsoleAzure Portal, OMS, ARM-templates, PowerShell, CLIManaging Azure Services
MonitoringBizTalk Health MonitorAzure Monitor, Application InsightsAuzre OMS offers complete monitoring solution. Can also use Serverless360.
ArchivingBizTalk databases (Archive)Azure Storage, Application Insights, Log AnalysisLogging & Tracking

Refer below articles for more detail –

Migration BizTalk to Azure

https://www.serverless360.com/blog/migrating-biztalk-to-azure

b) Migrate to BizTalk Server 2020 –

  • In-Place Upgrade
  • Migrate to New Environment
    • As-Is Migration
    • BizTalk Server Migration Tool
  • Migrate and Improve
    • Traditional Migration of every solution
    • Possible enhancement of code, removing/replacing deprecated items

Let’s understand these options in detail –

In-Place Upgrade

Upgrading of the existing BizTalk Server 2016 Infrastructure to BizTalk Server 2020(In House Upgrade).

In-Place Upgrade Pros & Cons –

Advantages –

  • Extremely minimal efforts and time to upgrade
  • One click upgrade of BizTalk within the existing infrastructure.

Disadvantages –

  • Can only be done with N-1 Version
  • High Risk of Failure – Big stack to upgrade, must be compatible
    • OS, SQL, VS, BizTalk, 3rd Party softwares etc.
  • As-Is Upgrade – No change to infrastructure or code.
  • Code changes later will require upgrade to new VS, rebuild etc.
  • This approach is only suitable for small businesses with simpler integration

Migration – Parallel New Setup

Advantages –

  • Migration can be done in phases
  • Can scale up infrastructure
  • No downtime as we will have parallel environments
  • Can do the enhancements like replace deprecated features, code enhancements etc.

Disadvantages –

  • Requires a lot of efforts as all the applications need to be rebuilt, deployed and tested
  • Time taking, costly and lots of monotonous activities

Question 3 – I chose Migration to parallel environment. Can it be automated?

Option 1 – Migration Options – BizTalk Migration Tool(MSIT)

Microsoft’s internal team has provided a tool which assists heavily in as-is migration.

Refer https://docs.microsoft.com/en-in/archive/blogs/biztalk_server_team_blog/announcing-biztalk-server-migration-tool

Migration Tool Features –

Traditional Migration –

High Level Steps involved –

  • Recreate BizTalk Servers 2020 environment with desired (supported) Hardware & Software’s.
  • Application Level Migration
    • Uprade to Latest Visual Studio
    • Change of .Net Framework
    • Incorporate references and 3rd Party Softwares
    • Rebuild Code
    • End to End Testing
  • Possible Enhancements if any

Question 4 – Can we automate BizTalk Traditional Migration?

Yes, you can create One click Upgrade & Migration of thousands of BizTalk Solutions following below steps –

Major Operations to Automate –

  • Upgrade to Latest Visual Studio using Command line –

devenv {SolutionFile|ProjectFile} /Upgrade [/Out OutputFilename]

  • Rebuild Solutions – Use VS command line –

devenv SolutionName /Rebuild [SolnConfigName [/Project ProjName [/ProjectConfig ProjConfigName]] [/Out OutputFilename]]

  • Change DotNet Framework Version for all Projects – Specially helpful for class libraries – Pipeline components, custom functoids, custom adapters, Helpers

Replace TargetFrameworkVersion section in project files (.btpproj) with target framework

<TargetFrameworkVersion>DotNetTargetFramework</TargetFrameworkVersion>

  • Add BTDF Project or BizTalk Deployment project to solution

Use a template Deployment Folder with all the required files, update it with proper values at runtime

Refer below article for detailed implementation –

BIZTALK AUTOMATED MIGRATION

https://prashantbiztalkblogs.wordpress.com/2020/08/28/biztalk-automated-migration/

Question 5 – What’s New in BizTalk 2020?

I’m sure many of you must be already aware of the new features, so I’m bringing this topic in the end. It’s better to refer MS Official articles for more detail. I will just provide a brief summary.

Refer – What’s New in BizTalk Server 2020

https://docs.microsoft.com/en-us/biztalk/install-and-config-guides/whats-new-in-biztalk-server-2020

Support for newer platforms –

  • Visual Studio 2019
  • Windows Server 2019, Windows Server 2016, Windows 10
  • SQL Server 2019, SQL Server 2017, SQL Server 2016 SP2
  • DotNet Framework 4.7.2 or above
  • Office 2019, Office 2016

Hardware and Software Requirements for BizTalk Server 2020

New Office 365 Adapters – SMTP & POP3 Adapters are deprecated and replaced by Office 365 Adapters
– Adapter for Mail – Send and receive messages using Office 365 e-mail
– Adapter for Schedule – Set and update appointments using Office 365 schedules
– Adapter for Contact – Define people and groups using Office 365 contacts

Note – A new BizTalk TMS service will run in BizTalk application server, which will be responsible to get token for every office 365 account and cache it.

Better Integration with Azure – Hybrid Integration
– Logic Apps Adapter
– Service Bus Adapter
– Event Hubs Adapter
– Integration with API Management
– Publish Orchestration/Schema endpoints via APIM
– Publish APIs to external, partner & internal developers to unlock the potential of their data and services
– Adapter for Blob Storage
– Read and write binary files in Azure Blob Storage

Deployment and Administration Enhancements –

  • Design and Deploy with VSTS
    • Aplication Lifecycle Management to deploy and update BizTalk Server instances using Visual Studio Team System (VSTS)
  • Backup to Blob Storage
    • Backup BizTalk Server database job log files to Azure Blob Storage
  • Advanced Scheduling
    • Set up recurrence on BizTalk Server receive locations with greater precision using time zones, months, weeks, and days

Security Enhancements –

  • Transport Layer Security 1.2
    • Securely deploy BizTalk Server using TLS 1.2 authentication and encryption
  • SQL Server Encrypted Data
    • Read and write to SQL Server always encrypted columns using BizTalk Adapter for SQL Server

Enhanced Administration

  • REST APIs
    • Manage BizTalk environments using standard RESTful Web services APIs. Can create BizTalk 360 like portal yourself.
  • Read-only Operator Role
    • Facilitate dev ops model, provide production stamp without ability to update anything
  • Group Managed Service Accounts
    • Extend windows GMSA support to BizTalk operations and services
    • No need to change passwords anymore with GMSA
  • Audit Log
    • Making BizTalk further secure by maintaining audit trails of all management operation – One of the most important feature for auditing

Enhanced Analytics and Reporting

  • Application Insights
    • Send BizTalk tracking data to Application Insights for extensible analytics – understand Trends on usage, performance, custom business metrics
  • Event Hubs
    • Send BizTalk Server tracking data to Event Hubs
  • Power BI
    • Analyze & report BizTalk tracking data using Power BI. Use operational data templates for visualizations and dashboards

Few Other Important Enhancements –

  • XSLT 3.0 Support
    • Out of box wiring to work with Saxon XSLT3.0
  • SQL Availability Group support for BAM DTS Package via SSIS Catalog
    • Helpful for HA of BAM DBs
  • Partially disabled receive locations
    • Allows Fault Tolerance
  • Throughput improvements for Dynamic Send Ports with Ordered Delivery
  • Support for SSO Affiliate applications in SFTP adapter

Deprecated Features

ProgramStatusReplacement
POP3 and SMTP adaptersDeprecatedOffice 365 adapters
Support for ACS authentication in adaptersRemovedSAS authentication
SOAP adapterDeprecatedWCF-BasicHttp Adapter
Old SQL adapterRemovedWCF-SQL Adapter
BPEL supportDeprecatedNone
JDE OneWorld adapterDeprecatedNone
OWC redistRemovedNone
BAM PortalDeprecatedNone
WCF-NetTcpRelay adapterDeprecatedNone
SamplesRemovedRemoved from BizTalk Server installation.

Hope this article helps you in you BizTalk Migration Planning.

Feel free to contact me in case of any query. Thanks.

Contact Me:- 

@Gmail@Facebook , @Twitter, @LinkedIn @MSDNTechnet, @My Personal Blog 



PROGRAMMATICALLY Change service account’s password for Windows Services, iis app pools and scheduled tasks – C#

Nowadays it’s becoming a very common practice to frequently (half-yearly or quarterly) change password for all AD Service Accounts. It’s done as part of password compliance security guidelines, specially post GDPR implementations.

Earlier for BizTalk environments we use to have “Never Expire Password” for service account and used it to configure BizTalk Services (like Host Instances, ENTSSO & RuleEngineUpdateService), IIS App Pools or SQL Server Services. We also used BizTalk Service Account to configure multiple custom utilities for maintenance purpose as Windows Scheduled Tasks or as Windows Service.

Changing password manually for all these services, scheduled tasks and App Pools is very cumbersome and risky task. If we fail miss or mistype password even at one place, it may cause account lock issue and bring complete environment at halt.

Note – This problem can be addressed in BizTalk 2020 by using Group Managed Service Accounts as they don’t require Passwords.

Refer – What’s New in BizTalk Server 2020

Group Managed Service AccountsExtend windows GMSA support to BizTalk operations and services.

Using Group Managed Service Account

To address this issue in, I have created Windows Application (works for all the version of BizTalk)

It takes three inputs –

  1. Servers (Comma Separated) => List of Servers where change is required. Ex – In a multi-server BizTalk Server, provide name of all the BizTalk App Servers
  2. Service Account => AD Account for which password needs to be changed
  3. New Password

Along with a Checkbox to select features which require change –

  1. Windows Services – Includes BizTalk Host Instances, ENTSSO, RuleEngineUpdateService, IIS and other custom services using this account
  2. IIS App Pools
  3. Scheduled Tasks – Configured with custom utilities

Along with a Checkbox to select features which require change –

  1. Windows Services– Includes BizTalk Host Instances, ENTSSO, RuleEngineUpdateService, IIS and other custom services using this account
  2. IIS App Pools
  3. Scheduled Tasks – Configured with custom utilities

Allowed Operations –

  1. Stop All Services – It acts as a “Pre-requisites/Prepare for change” step to bring down BizTalk Environment. It will STOP all the Host Instances – irrespective of service account, ENTSSO, RuleEngineUpdateService and IIS.
  2. Get List – It retrieves a list of all the services, app pools and scheduled task which are configured with given service account.
  3. Change Password – It changes password for all the services, app pools and scheduled task configured with given service account.
  4. START all Services – It acts as post change activity to bring BizTalk online by starting all the Host Instances, ENTSSO, RuleEngineUpdateService and IIS

You can choose to perform password change activity on all or any – Services, App Pools and Windows Scheduled Tasks.

Code Explanation –

  1. Windows Services – Getting and Updating password for the list of services configured for given service account.

I have used WMI Queries –

SELECT * FROM Win32_Service where StartName like '%<Service Account Name>%'

Sample Code to get the list –

string strWMINamespace = @"\\" + strServer + @"\root\cimv2";
                if (strServiceAccount.Contains('\\'))
                {
                    //If service account contains backslash it needs to be replaced by double backslash
                    strServiceAccount = strServiceAccount.Replace(@"\", @"\\");
                }
                //SELECT * FROM Win32_Service where StartName like '%<Service Account Name>%'
                //Yields BizTalk Host Instances and ENTSSO
                string strWMIQuery = @"SELECT * FROM Win32_Service where StartName like '%" + strServiceAccount + "%'";
                //Create EnumerationOptions and run wql query  
                EnumerationOptions enumOptions = new EnumerationOptions();
                enumOptions.ReturnImmediately = false;
                using (ManagementObjectSearcher searchObject = new ManagementObjectSearcher(strWMINamespace, strWMIQuery, enumOptions))
                {
                    //Enumerate through the result set and stop if not stopped and Change Password  
                    foreach (ManagementObject service in searchObject.Get())
                    {
                     //Iterate over list of services and perform operation
                     //Updating the Password, refer - https://morgantechspace.com/2015/03/csharp-change-service-account-username-and-password.html#wmi
                     //Creating account object
                     object[] accountParams = new object[11];
                     //Updating Username for testing
                     //accountParams[6] = testUserName;
                     accountParams[7] = strNewPassword;
                     uint returnCode = (uint)service.InvokeMethod("Change", accountParams);
                     if (returnCode == 0)
                       {
                         updateLogs("Password changed successfully for service - " + service["Name"] + "");
                       }
                    }
                }

2. IIS App Pools => Getting and updating password for list of App Pools configured using given service account.

 using (ServerManager IISServerManager = ServerManager.OpenRemote(strServer))
 {
   foreach (ApplicationPool appPool in IISServerManager.ApplicationPools)
    {
      if (appPool.ProcessModel.UserName.ToLower().Contains(strServiceAccount))
      {
         updateLogs("Attempting to change password for App Pool - " + appPool.Name);
         appPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
         //Updating Username for testing
         //appPool.ProcessModel.UserName = testUserName;
         appPool.ProcessModel.Password = strNewPassword;
         boolRequiresCommit = true;
       }
    } 
    if (boolRequiresCommit)
     {
         IISServerManager.CommitChanges();
         updateLogs("Password changed successfully for App Pools");
     }
 }

3. Windows Scheduled Tasks => Getting and updating password for list of Windows Scheduled Tasks configured using given service account.

Microsoft.Win32.TaskScheduler.TaskService taskService = new TaskService(strServer);
foreach (Microsoft.Win32.TaskScheduler.Task task 
in taskService.RootFolder.GetTasks().Where
                    (t => t.Definition.Principal.UserId.ToString().ToLower().Contains
                          (strServiceAccount.ToLower())))
{
     //Disabling each task if it's enabled
     if (task.State != TaskState.Disabled)
       {
         task.Stop();
         task.Enabled = false;
       }
string strRunAsCMD = @"/C schtasks.exe /CHANGE /S " + strServer + " /TN \"" + task.Name + "\" /RU " + strServiceAccount + " /RP " + strNewPassword + "";
    updateLogs("Attempting to change password for task - " + task.Name + "");
    executeCommand(strRunAsCMD);
    updateLogs("Password changed successfully for task - " + task.Name + "");
}
 
//Code to Execute command in Windows Command Prompt                
private void executeCommand(string strRunAsCMD)
{
    var processInfo = new ProcessStartInfo("cmd.exe", strRunAsCMD);
    processInfo.CreateNoWindow = true;
    processInfo.UseShellExecute = false;
    processInfo.RedirectStandardError = true;
    processInfo.RedirectStandardOutput = true;
    processInfo.Verb = "runas";

    var process = Process.Start(processInfo);

    string strProcessOutput = process.StandardOutput.ReadToEnd();
    string strProcessError = process.StandardError.ReadToEnd();

    string logMsg = "executeCommand - Command Execution Completed with Exit Code : " +               process.ExitCode.ToString() + Environment.NewLine;
   if (String.IsNullOrEmpty(strProcessError))
    {
      logMsg += "Process Output - " + strProcessOutput;
    }
    else
    {
       logMsg += "Process Error - " + strProcessError;
    }
    updateLogs(logMsg);

    process.WaitForExit();
    //Checking if the process execution failed
    if (process.ExitCode != 0)
    {
       updateLogs("executeCommand - Error occurred in execution." + Environment.NewLine + "Exit Code - " + process.ExitCode + Environment.NewLine +
                           "Process Error Output - " + strProcessError);
     }

 process.Close();
}

4. Starting/Stopping Host Instances

private async System.Threading.Tasks.Task stopOrStartAllHostInstances(HostAction hostAction)
        {
            try
            {
                //Create EnumerationOptions and run wql query  
                System.Management.EnumerationOptions enumOptions = new System.Management.EnumerationOptions();
                enumOptions.ReturnImmediately = false;

                //Search for all HostInstances of 'InProcess' type in the Biztalk namespace scope  
                System.Management.ManagementObjectSearcher searchObject =
                    new System.Management.ManagementObjectSearcher("root\\MicrosoftBizTalkServer",
                    "Select * from MSBTS_HostInstance Where HostType=1 And IsDisabled=False", enumOptions);

                //Enumerate through the result set and stop each HostInstance if it is running  
                foreach (ManagementObject inst in searchObject.Get())
                {
                    Task<string> task = new Task<string>(() => stopOrStartHostInstance(inst, hostAction));
                    task.Start();
                    updateLogs("Attempting to perform " + hostAction + " on HostInstance of Host: " + inst["HostName"] + " on Server: " + inst["RunningServer"] + "." + Environment.NewLine);
                    string status = await task;
                    updateLogs("Successfully performed " + hostAction + " action on " + inst["HostName"] + " on Server: " + inst["RunningServer"] + "." + Environment.NewLine + Environment.NewLine);
                }
            }
            catch (Exception excep)
            {
                string strExceptionMessage = "Exception occurred in function-stopOrStartAllHostInstances. Message - " + excep.Message + Environment.NewLine + "StackTrace - " + excep.StackTrace;
                throw new Exception(strExceptionMessage);
            }
        }

private string stopOrStartHostInstance(ManagementObject inst, HostAction hostAction)
        {
            try
            {
                if (hostAction == HostAction.STOP)
                {
                    //Check if ServiceState is 'Started'  
                    if (inst["ServiceState"].ToString() == "4")
                    { inst.InvokeMethod("Stop", null); }
                }
                if (hostAction == HostAction.START)
                {
                    if (inst["ServiceState"].ToString() == "1")//Status Stopped
                    {
                        inst.InvokeMethod("Start", null);
                    }
                }
                return "Success";
            }
            catch (Exception excep)
            {
                string strExceptionMessage = "Exception occurred in Function - stopOrStartHostInstance. Message - " + excep.Message + Environment.NewLine + "StackTrace - " + excep.StackTrace;
                throw new Exception(strExceptionMessage);
            }
        }

5. Starting/Stopping Services

private async System.Threading.Tasks.Task stopORStartAdditionalServices(string strServerNames, ServiceAction serviceAction)
        {
            try
            {
                foreach (string strServerName in strServerNames.Split(','))
                {
                    ServiceController[] scServices = ServiceController.GetServices(strServerName);
                    foreach (ServiceController service in scServices)
                    {
                        //Stopping/Starting IIS, RULEEngineUpdateService and ENTSSO services
                        if (service.ServiceName.ToUpper().Contains("W3SVC") || service.ServiceName.ToUpper().Contains("RULEENGINEUPDATESERVICE") || service.ServiceName.ToUpper().Contains("ENTSSO"))
                        {
                            Task<string> task = new Task<string>(() => stopOrStartService(service, serviceAction));
                            task.Start();
                            updateLogs("Attempting to " + serviceAction + " - " + service.DisplayName + " On Server - " + strServerName + Environment.NewLine);
                            string result = await task;
                            updateLogs("Successfully performed Action - " + serviceAction + " on Service - " + service.DisplayName + " On Server - " + strServerName + Environment.NewLine);
                        }
                    }
                }
            }
            catch (Exception excep)
            {
                string strException = "Exception Occurred in function - stopORStartAdditionalServices. Message - " + excep.Message + Environment.NewLine + "Stack Trace - " + excep.StackTrace + Environment.NewLine;
                throw new Exception(strException);
            }
        }

private string stopOrStartService(ServiceController service, ServiceAction serviceAction)
        {
            try
            {
                if ((serviceAction == ServiceAction.STOP) && (service.Status != ServiceControllerStatus.Stopped))
                {
                    //stopping the service 
                    service.Stop();
                }
                else if ((serviceAction == ServiceAction.START) && (service.Status == ServiceControllerStatus.Stopped))
                {
                    //Starting the services
                    service.Start();
                }
                return "Success";
            }
            catch (Exception excep)
            {
                string strException = "Exception Occurred in function - stopOrStartService. Message - " + excep.Message + Environment.NewLine + "Stack Trace - " + excep.StackTrace + Environment.NewLine;
                throw new Exception(strException);
            }
        }

Note – Post this password change activity we started getting error for BAM Portal

Configuration Error –

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Could not create Windows user token from the credentials specified in the config file. Error from the operating system ‘The user name or password is incorrect.’

This error is bound to come and is explained in detail in below article –

https://prashantbiztalkblogs.wordpress.com/2020/07/14/bam-portal-error-could-not-create-windows-user-token-from-the-credentials-specified-in-the-config-file-error-from-the-operating-system-the-user-name-or-password-is-incorrect/

Hope it’s helpful.

Download the complete code from here

Contact Me:- 

@Gmail@Facebook , @Twitter, @LinkedIn @MSDNTechnet, @My Personal Blog 

Monitoring BizTalk Resources programmatically using C#

Hi friends many times as a BizTalk Administrator we fall into situations when we need to constantly monitor BizTalk Servers resources and services like Host Instances Status, any custom services like IIS, CPU usage, available disk space etc.

So, in this blog I have created a console application which can be scheduled to run continuously using Windows Scheduler. It will constantly monitor all resources and attempt to correct it and later drop an email to configured group or user (only if there is an issue).

Note – Proactive Monitoring ensures early resolutions, thus solving upcoming big future problems.

Currently I have focused on monitoring of below items –

1) Check ENTSSO, IIS And Other Custom Services
2) Enumerate And Start All Host Instances
3) Check If Host is in Hung State – “Start Pending” or “Stop Pending”
4) Check Available Drive Space
5) Check CPU Reading
6) Check All Suspended and Custom Orchestration Suspended instance count
7) Check Spool Count, Parts Count and row count for other tables if it is more than the configured value
8) Check BizTalkMsgBox Table Index Fragmentation

In multi-server BizTalk environment this tool needs to be scheduled in only in one environment and provide all other server names of this group in configuration file – “ServerNames” as shown below. It will remotely query all the servers.

<add key=”ServerNames” value=”BTSServer1;BTSServer2″/>

1) Check ENTSSO IIS And Other CustomServices –

In this function we can monitor important Services like ENTSSO, IIS(World Wide Web Publishing Service – w3svc) or any other custom service. You just need to mention these services in config file as shown below –

<add key=”AdditionalServicesToBeMonitored” value=”ENTSSO;W3SVC;RTI”/>

I have used “ServiceController” class to fetch list of all the services, check its status and finally start the same.

ServiceController[] scServices = ServiceController.GetServices(strServerName);

foreach (ServiceController service in scServices)
 {
 if ((service.ServiceName.ToUpper().Contains(strServiceName)) && service.Status != ServiceControllerStatus.Running)
 {
service.Start();}
 }

2) Enumerate And Start Host Instances –

In this function we are enumerating all the In-Process Host Instances using WMI Query and starting them.

Sample code -

//Create EnumerationOptions and run wql query 
 EnumerationOptions enumOptions = new EnumerationOptions();
 enumOptions.ReturnImmediately = false;

//Search for all HostInstances of 'InProcess' type in the Biztalk namespace scope 
 ManagementObjectSearcher searchObject = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer", "Select * from MSBTS_HostInstance Where HostType=1 And IsDisabled=False", enumOptions);


//Enumerate through the result set and start each HostInstance if it is already stopped 
 foreach (ManagementObject inst in searchObject.Get())
 {
 //Check if ServiceState is 'Stopped' 
 if (inst["ServiceState"].ToString() == "1")
 { inst.InvokeMethod("Start", null); }}

3) Check If Host is in “Start Pending” or “Stop Pending” State

Many times BizTalk Host Instances go in Hung State like “Stop Pending” or “Start Pending” and if you leave these processes for days they won’t come up. So, the only option left is to Terminate(End Process Tree) using Task Manager and start the host again. I totally agree that it’s not the best way as the service will not exit gracefully but in production environment we are left with very little option.

Here also I am using WMI queries to find if the Host is in Hung State i.e. Start or Stop Pending state.

 //Search for all HostInstances of 'InProcess' type in the Biztalk namespace scope which is not Disabled and have Service State as 2(Start Pending) or 3(Stop Pending) 
 ManagementObjectSearcher searchObject = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer", "Select * from MSBTS_HostInstance Where HostType=1 And IsDisabled=False and (ServiceState = 2 or ServiceState = 3)", enumOptions);

After waiting for some configured milliseconds I am performing again a WMI Query to check if the Host Status is changed or not. Because this issue occurs very rarely and mostly “Start Pending” or “Stop Pending” state will last for very less time when you have performed Start or Stop of Host Instances.

//Query again to check if the status is still the same i.e. Stopped/Start Pending
 ManagementObjectSearcher searchObject1 = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer", "Select * from MSBTS_HostInstance Where HostName='" + inst["HostName"].ToString() + "' and (ServiceState = 2 or ServiceState = 3)", enumOptions);

Once if it’s confirmed that Host is hung we will go ahead and Terminate it forcibly along with all other processes with same parentID.

 var connectionOptions = new ConnectionOptions();
 ManagementScope scope = new ManagementScope(@"\\" + strServerName + @"\root\cimv2", connectionOptions);
 scope.Connect();
 //Get Process ID of the service. BizTalk Host Instance Name is always like BTSSvc$ like BTSSvc$BizTalkServerApplication
 var query = new SelectQuery("select ProcessID from Win32_Service where Name ='BTSSvc$" + strHostName + "'");
 using (var searcher = new ManagementObjectSearcher(scope, query))
 {
 foreach (ManagementObject obj in searcher.Get())
 {
 uint processId = (uint)obj["ProcessId"];
 //Hard Terminate(Terminate Related Processes) - Loop through all the processes on the machine and Kill it if it's Parent Process ID or ProcessID matches the Service Process ID
 var parentIdQuery = new SelectQuery("Select * from Win32_process");
 using (var vsearcher = new ManagementObjectSearcher(scope, parentIdQuery))
 { 
foreach (ManagementObject process in vsearcher.Get()) {
 //Checking if Parent Process ID or ProcessID is same as Service ProcessID
 if (processId == (uint)process["ParentProcessID"] || processId == (uint)process["ProcessID"])
 { process.InvokeMethod("Terminate", null); } } } } }

4) Check Available Drive Space – 

Here too I am using WMI query to check Available Disk Space. This query will be performed remotely on all the servers.

For remotely connecting WMI Query, namespace will be  –

string strNameSpace = @”\\” + strServerName + @”\root\cimv2″;

Query – select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3″, enumOptions

string strNameSpace = @"\\" + strServerName + @"\root\cimv2";
 //Get Fixed disk state. Check - http://www.csidata.com/custserv/onlinehelp/vbsdocs/vbs41.htm, DriveType = 2 - Fixed, Drive has fixed (nonremovable) media. This includes all hard drives, including hard drives that are removable. DriveType = 2 -Remote, Network drives. This includes drives shared anywhere on a network.
 ManagementObjectSearcher searchObject = new ManagementObjectSearcher(strNameSpace, "select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3", enumOptions);

5) Check CPU Reading – 

I am using Performance Counter – % Processor Time. Here we Sleep the thread to achieve average value for configured time.

PerformanceCounter totalProcessorTimeCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", strServerName);
 totalProcessorTimeCounter.NextValue();
 System.Threading.Thread.Sleep(intThreadSleepTimeInMilliSeconds);// seconds wait
 double CPUPercentage = totalProcessorTimeCounter.NextValue();
 if (CPUPercentage > doubleAcceptableCPUPercentage)

6) Check All Suspended and Custom Orchestration Suspended instance count

Here also I am using WMI query to fetch total suspended instances – ServiceClass=1(Orchestration) or ServiceClass=4(Messaging) and (ServiceStatus=4(Suspended (resumable)) or ServiceStatus=32(Suspended (non-resumable)) in the BizTalk namespace

//Search for all Service Instances for ServiceClass=1(Orchestration) or ServiceClass=4(Messaging) and (ServiceStatus=4(Suspended (resumable)) or ServiceStatus=32(Suspended (non-resumable)) in the Biztalk namespace scope 
 ManagementObjectSearcher searchObject = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer", "Select * from MSBTS_ServiceInstance Where (ServiceClass=1 or ServiceClass=4) and (ServiceStatus=4 or ServiceStatus=32)", enumOptions);

It can be configured to get Suspended Instance for individual Orchestrations as well, which can be configured in app.config file.

WQL Query – string.Format(“SELECT * FROM MSBTS_ServiceInstance WHERE (ServiceClass=1 or ServiceClass=4) and (ServiceStatus=4 or ServiceStatus=32) AND ServiceName like \”%{0}%\””, strOrchName);

 //Now checking for individual Orchestrations Only If Suspended Instance Count not zero
 int count = 1;
 foreach (string strOrchName in strOrchestrationNames.Split(';'))
 {
 string strWQL = string.Format("SELECT * FROM MSBTS_ServiceInstance WHERE (ServiceClass=1 or ServiceClass=4) and (ServiceStatus=4 or ServiceStatus=32) AND ServiceName like \"%{0}%\"", strOrchName);

7) Spool Count, Parts Count and other tables with Row Count if more than configured value –

Performing SQL Query on BizTalk Message box to get top 10 tables with row count more that the acceptable configured value. This information can be used to proactively prevent throttling and avoid slowness of BizTalk message processing.

//Checking top 10 tables with Row count more than intAcceptableMsgBoxRowCount
string queryString = “SELECT top 10 T.name TableName, I.Rows NumberOfRows FROM sys.tables T JOIN sys.sysindexes I ON T.OBJECT_ID = I.ID “
+ “WHERE indid IN (0,1) and I.Rows > ” + intAcceptableMsgBoxRowCount.ToString() + “ORDER BY I.Rows DESC,T.name”;
8) Check BizTalkMsgBox Table Index Fragmentation –

To check Fragmentation I am using below query. It checks if Fragmentation is more than the configured acceptable value. In case of high Fragmentation you will have to Rebuild Indexes to improve performance. Execute below stored procedures during down time.

1.bts_RebuildIndexes —-   for BizTalkMsgBoxDb database
2.dtasp_RebuildIndexes—  for BizTalkDTADb database

//Checking Index Fragmentation if the value is more than Acceptable Fragmentation Percentage, Selecting top 10 values
“SELECT top 10 dbtables.[name] as ‘Table’, dbindexes.[name] as ‘Index’, indexstats.avg_fragmentation_in_percent, indexstats.page_count

FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id] INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id] INNER JOIN sys.indexes AS dbindexes ON dbindexes.[object_id] = indexstats.[object_id] AND indexstats.index_id = dbindexes.index_id

WHERE indexstats.database_id = DB_ID()and avg_fragmentation_in_percent > ” + doubleAcceptableFragmentationPercentage.ToString() + ” “;

ORDER BY indexstats.avg_fragmentation_in_percent desc”;

 

Complete Solution Code can be found here

App.config file - 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <startup> 
 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
 </startup>
 <appSettings>
 <!--BizTalk Message Box Connection String-->
 <add key="BizTalkMsgBoxConnectionString" value="Data Source=WIN-4ACT86KFN44;Initial Catalog=BizTalkMsgBoxDb;Integrated Security=true;Pooling=false"/>
 
 <!--SMTP Details for Sending Emails-->
 <add key="IsEmailRequired" value="true"/>
 <add key="SMTPHost" value="smtp.gmail.com"/>
 <add key="FROMEmailID" value="youremailid@gmail.com"/>
 <add key="TOEmailID" value="toemailid@gmail.com,toemailid2@microsoft.com"/>
 <add key="SMTPServerPort" value="587"/>
 <!--<add key="Username" value="youremailid@gmail.com"/>
 <add key="Password" value="yourpassword"/>-->
 <add key="Subject" value="Monitoring BizTalk Resources"/>

<add key="ServerNames" value="WIN-4ACT86KFN44"/>
 <add key="AdditionalServicesToBeMonitored" value="ENTSSO;W3SVC;RTI"/>
 <add key ="AcceptableAvailableDriveFreeSpace" value="80"/>
 <add key="AcceptableTotalSuspendedInstanceCount" value="1"/>
 <add key="OrchestrationNames" value="ProcessCandidateAgeIfno;ProcessOrders;DummyOrchName"/>
 <add key="AcceptableSuspendedInstanceCountForAnOrch" value="1"/>
 <add key="AcceptableCPUPercentage" value="1"/>
 <add key="AcceptableMsgBoxRowCount" value="1"/> 
 <add key="AcceptableFragmentationPercentage" value="1"/>
 <add key ="ThreadSleepTimeInMilliSeconds" value="5000"/>
 </appSettings>
</configuration>

 

C# Code - 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Net.Mail;
using System.Configuration;
using System.ServiceProcess;
using System.IO;
using System.Diagnostics;
using System.Data.SqlClient;

namespace MonitoringBizTalkResources
{
 class MonitoringBizTalkResources
 {
 static void Main(string[] args)
 {
 #region Get Values from Config File

string strEmailBody = "";
 var appSettings = ConfigurationManager.AppSettings;

//Get Server Names for checking services
 string strServerNames = "";
 if (appSettings["ServerNames"] != null && !String.IsNullOrEmpty(appSettings["ServerNames"].ToString()))
 { strServerNames = Convert.ToString(appSettings["ServerNames"].ToString()); }

//Getting Additional Services To Be Monitored
 string strAdditionalServicesToBeMonitored = "";
 if (appSettings["AdditionalServicesToBeMonitored"] != null && !String.IsNullOrEmpty(appSettings["AdditionalServicesToBeMonitored"].ToString()))
 { strAdditionalServicesToBeMonitored = Convert.ToString(appSettings["AdditionalServicesToBeMonitored"].ToString()); }




//Getting connection string from Config File
 string strConnectionString = "";
 if (appSettings["BizTalkMsgBoxConnectionString"] != null && !String.IsNullOrEmpty(appSettings["BizTalkMsgBoxConnectionString"].ToString()))
 { strConnectionString = Convert.ToString(appSettings["BizTalkMsgBoxConnectionString"].ToString()); }

//If value of IsEmailRequired field is present in config file will populate the same else will keep it as default = false
 bool IsEmailRequired = false;
 if (appSettings["IsEmailRequired"] != null && !String.IsNullOrEmpty(appSettings["IsEmailRequired"].ToString()))
 { IsEmailRequired = Convert.ToBoolean(appSettings["IsEmailRequired"].ToString()); }

//Get ThreadSleepTimeInMilliSeconds
 int intThreadSleepTimeInMilliSeconds = 5000;
 if (appSettings["ThreadSleepTimeInMilliSeconds"] != null && !String.IsNullOrEmpty(appSettings["ThreadSleepTimeInMilliSeconds"].ToString()))
 { intThreadSleepTimeInMilliSeconds = Convert.ToInt32(appSettings["ThreadSleepTimeInMilliSeconds"].ToString()); }

//If value of AcceptableAvailableDriveSpace field is present in config file will populate the same else will keep it as default
 double doubleAcceptableFreeDriveSpace = 80;
 if (appSettings["AcceptableAvailableDriveFreeSpace"] != null && !String.IsNullOrEmpty(appSettings["AcceptableAvailableDriveFreeSpace"].ToString()))
 { doubleAcceptableFreeDriveSpace = Convert.ToDouble(appSettings["AcceptableAvailableDriveFreeSpace"].ToString()); }

//If value of AcceptableTotalSuspendedInstanceCount field is present in config file will populate the same else will keep it as default
 int intAcceptableTotalSuspendedInstanceCount = 10000;
 if (appSettings["AcceptableTotalSuspendedInstanceCount"] != null && !String.IsNullOrEmpty(appSettings["AcceptableTotalSuspendedInstanceCount"].ToString()))
 { intAcceptableTotalSuspendedInstanceCount = Convert.ToInt32(appSettings["AcceptableTotalSuspendedInstanceCount"].ToString()); }

//Get Orchestration Names from Config
 string strOrchestrationNames = "";
 if (appSettings["OrchestrationNames"] != null && !String.IsNullOrEmpty(appSettings["OrchestrationNames"].ToString()))
 { strOrchestrationNames = appSettings["OrchestrationNames"].ToString(); }

//Get AcceptableSuspendedInstanceCountForAnOrch from config
 int intAcceptableSuspendedInstanceCountForAnOrch = 100;
 if (appSettings["AcceptableSuspendedInstanceCountForAnOrch"] != null && !String.IsNullOrEmpty(appSettings["AcceptableSuspendedInstanceCountForAnOrch"].ToString()))
 { intAcceptableSuspendedInstanceCountForAnOrch = Convert.ToInt32(appSettings["AcceptableSuspendedInstanceCountForAnOrch"].ToString()); }

//Get AcceptableCPUPercentage from Config
 double doubleAcceptableCPUPercentage = 80;
 if (appSettings["AcceptableCPUPercentage"] != null && !String.IsNullOrEmpty(appSettings["AcceptableCPUPercentage"].ToString()))
 { doubleAcceptableCPUPercentage = Convert.ToDouble(appSettings["AcceptableCPUPercentage"].ToString()); }

//Get AcceptableFragmentationPercentage from Config
 double doubleAcceptableFragmentationPercentage = 50;
 if (appSettings["AcceptableFragmentationPercentage"] != null && !String.IsNullOrEmpty(appSettings["AcceptableFragmentationPercentage"].ToString()))
 { doubleAcceptableFragmentationPercentage = Convert.ToDouble(appSettings["AcceptableFragmentationPercentage"].ToString()); }

//Get Acceptable MsgBox RowCount for a table from config
 int intAcceptableMsgBoxRowCount = 30000;
 if (appSettings["AcceptableMsgBoxRowCount"] != null && !String.IsNullOrEmpty(appSettings["AcceptableMsgBoxRowCount"].ToString()))
 { intAcceptableMsgBoxRowCount = Convert.ToInt32(appSettings["AcceptableMsgBoxRowCount"].ToString()); }

#endregion

try
 {
 //Check if Enterprise Single Sign-On Service or IIS is stopped
 checkENTSSOIISAndCustomServices(ref strEmailBody, ref strServerNames, ref strAdditionalServicesToBeMonitored);

//Check all the Host Instances
 enumerateAndStartHostInstances(ref strEmailBody);

//Check if Host is in Start Pending or Stop Pending State
 checkIfHostStartorStopPendingState(ref strEmailBody, ref intThreadSleepTimeInMilliSeconds);

//Check Drive Free Space 
 checkAvailableDriveSpace(ref strEmailBody, ref strServerNames, ref doubleAcceptableFreeDriveSpace);

//Check CPU Reading
 checkCPUReading(ref strEmailBody, ref strServerNames, ref doubleAcceptableCPUPercentage, ref intThreadSleepTimeInMilliSeconds);

//Check All Suspended Instance Count
 checkAllSuspendedInstances(ref strEmailBody, ref intAcceptableTotalSuspendedInstanceCount, ref strOrchestrationNames, ref intAcceptableSuspendedInstanceCountForAnOrch);

//Check Spool Count, Parts Count and other tables with Row Count more than intAcceptableMsgBoxRowCount
 checkBizTalkMsgBoxTablesCount(ref strEmailBody, ref strConnectionString, ref intAcceptableMsgBoxRowCount);

//Check BizTalkMsgBox Table Index Fragmentation
 checkBizTalkMsgBoxTablesIndexFragmentation(ref strEmailBody, ref strConnectionString, ref doubleAcceptableFragmentationPercentage);

if (IsEmailRequired && !String.IsNullOrEmpty(strEmailBody))
 {
 string strHTMLStyle = "";
 strEmailBody = "<html>"+strHTMLStyle+"<body>" + strEmailBody + "</body></html>";
 sendEmail(strEmailBody);
 }
 }
 catch (Exception excep)
 {
 string strExceptionMessage = "MonitoringBizTalkResource - An exception occurred. " + excep.Message;
 if (!String.IsNullOrEmpty(strEmailBody))
 strExceptionMessage = strExceptionMessage + Environment.NewLine + "Operations performed so far - " + strEmailBody;
 System.Diagnostics.EventLog.WriteEntry("BizTalk Server", strExceptionMessage, System.Diagnostics.EventLogEntryType.Error);
 }
 }
 //Function to check if any Host is in Stop Pending or Start Pending State
 private static void checkIfHostStartorStopPendingState(ref string strEmailBody, ref int intThreadSleepTimeInMilliSeconds)
 {
 try
 {
 //Create EnumerationOptions and run wql query 
 EnumerationOptions enumOptions = new EnumerationOptions();
 enumOptions.ReturnImmediately = false;

//Search for all HostInstances of 'InProcess' type in the Biztalk namespace scope which is not Disabled and have Service State as 2(Start Pending) or 3(Stop Pending) 
 ManagementObjectSearcher searchObject = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer", "Select * from MSBTS_HostInstance Where HostType=1 And IsDisabled=False and (ServiceState = 2 or ServiceState = 3)", enumOptions);
 //ManagementObjectSearcher searchObject = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer", "Select * from MSBTS_HostInstance Where HostType=1 And IsDisabled=False and (ServiceState = 1 or ServiceState = 4)", enumOptions); // Test Query for Stopped(1) and Running(4) State

int count = 1;
 //Enumerate through the result set
 foreach (ManagementObject inst in searchObject.Get())
 {
 if (count == 1) // Wait only for the first time
 {
 System.Threading.Thread.Sleep(intThreadSleepTimeInMilliSeconds);// seconds wait
 }

//Query again to check if the status is still the same i.e. Stopped/Start Pending
 ManagementObjectSearcher searchObject1 = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer", "Select * from MSBTS_HostInstance Where HostName='" + inst["HostName"].ToString() + "' and (ServiceState = 2 or ServiceState = 3)", enumOptions);
 //ManagementObjectSearcher searchObject1 = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer", "Select * from MSBTS_HostInstance Where HostName='" + inst["HostName"].ToString() + "' and (ServiceState = 1 or ServiceState = 4)", enumOptions); //Test Query for Stopped(1) and Running(4) State
 foreach (ManagementObject inst1 in searchObject1.Get())
 {
 //Terminate the Host Instance
 terminateHungProcess(inst1["RunningServer"].ToString(), inst1["HostName"].ToString());

//Start the Host Instance
 inst1.InvokeMethod("Start", null);
 //Draft Email Subject
 if (count == 1)
 {
 strEmailBody = strEmailBody + "Below Host Instances were in Stopped/Start Pending state, Terminated the process and attempted to Start<br>";
 strEmailBody = strEmailBody + "<table><tr><th>Server Name</th><th>Host Instance Name</th></tr>";
 ++count;
 }
 strEmailBody = strEmailBody + "" + inst1["RunningServer"] + "" + inst1["HostName"] + "";
 }
 }
 if (count > 1)
 { strEmailBody = strEmailBody + "</table>All above mentioned Host Instances have been Started Successfully<br><br>"; }
 }
 catch (Exception excep)
 {
 System.Diagnostics.EventLog.WriteEntry("BizTalk Server", "Exception Occurred in enumerateAndStartHostInstances fuction call. " + excep.Message, System.Diagnostics.EventLogEntryType.Error);
 throw new Exception("Exception Occurred in enumerateAndStartHostInstances fuction call. " + excep.Message);
 }
 }

//Function to Terminate a Process in Hung State and also terminate all related processes(Having same ParentPID)
 private static void terminateHungProcess(string strServerName, string strHostName)
 {
 try
 {
 var connectionOptions = new ConnectionOptions();
 ManagementScope scope = new ManagementScope(@"\\" + strServerName + @"\root\cimv2", connectionOptions);
 scope.Connect();
 //Get Process ID of the service. BizTalk Host Instance Name is always like BTSSvc$<HostName> like BTSSvc$BizTalkServerApplication
 var query = new SelectQuery("select ProcessID from Win32_Service where Name ='BTSSvc$" + strHostName + "'");
 using (var searcher = new ManagementObjectSearcher(scope, query))
 {
 foreach (ManagementObject obj in searcher.Get())
 {
 uint processId = (uint)obj["ProcessId"];
 //Hard Terminate(Terminate Related Processes) - Loop through all the processes on the machine and Kill it if it's Parent Process ID or ProcessID matches the Service Process ID
 var parentIdQuery = new SelectQuery("Select * from Win32_process");
 using (var vsearcher = new ManagementObjectSearcher(scope, parentIdQuery))
 {
 foreach (ManagementObject process in vsearcher.Get())
 {
 //Checking if Parent Process ID or ProcessID is same as Service ProcessID
 if (processId == (uint)process["ParentProcessID"] || processId == (uint)process["ProcessID"])
 {
 process.InvokeMethod("Terminate", null);
 }
 }
 }
 }
 }
 }
 catch (Exception excep)
 {
 System.Diagnostics.EventLog.WriteEntry("BizTalk Server", "Exception Occurred in terminateHungProcess fuction call. " + excep.Message, System.Diagnostics.EventLogEntryType.Error);
 throw new Exception("Exception Occurred in terminateHungProcess fuction call. " + excep.Message);
 }
 }

//Function to check and start ENTSSO Service
 private static void checkENTSSOIISAndCustomServices(ref string strEmailBody, ref string strServerNames, ref string strAdditionalServicesToBeMonitored)
 {
 try
 {
 int count = 1;
 foreach (string strServerName in strServerNames.Split(';'))
 {
 ServiceController[] scServices = ServiceController.GetServices(strServerName);
 foreach (string strServiceName in strAdditionalServicesToBeMonitored.Split(';'))
 {
 foreach (ServiceController service in scServices)
 {
 if ((service.ServiceName.ToUpper().Contains(strServiceName)) && service.Status != ServiceControllerStatus.Running)
 {
 if (count == 1)
 {
 strEmailBody = strEmailBody + "Below Services are not running, attempting to start.<br>";
 strEmailBody = strEmailBody + "<table><tr><th>Server Name</th><th>Service Name</th></tr>";
 ++count;
 }
 strEmailBody = strEmailBody + "" + strServerName + "" + service.DisplayName + "";
 service.Start();
 }
 }
 }
 }
 if (count > 1)
 { strEmailBody = strEmailBody + "</table>All above mentioned Services have been Started Successfully<br><br>"; }
 }
 catch (Exception excep)
 {
 System.Diagnostics.EventLog.WriteEntry("BizTalk Server", "Exception Occurred in checkENTSSOIISAndCustomServices fuction call. " + excep.Message, System.Diagnostics.EventLogEntryType.Error);
 throw new Exception("Exception Occurred in checkENTSSOServiceAndIIS fuction call. " + excep.Message);
 }
 }

//Function to Enumerate all HostInstances of "InProcess" type and start them 
 private static void enumerateAndStartHostInstances(ref string strEmailBody)
 {
 try
 {
 //Create EnumerationOptions and run wql query 
 EnumerationOptions enumOptions = new EnumerationOptions();
 enumOptions.ReturnImmediately = false;

//Search for all HostInstances of 'InProcess' type in the Biztalk namespace scope 
 ManagementObjectSearcher searchObject = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer", "Select * from MSBTS_HostInstance Where HostType=1 And IsDisabled=False", enumOptions);
 int count = 1;
 //Enumerate through the result set and start each HostInstance if it is already stopped 
 foreach (ManagementObject inst in searchObject.Get())
 {
 //Check if ServiceState is 'Stopped' 
 if (inst["ServiceState"].ToString() == "1")
 {
 if (count == 1)
 {
 strEmailBody = strEmailBody + "Below Host Instances are not running, attempting to start.<br>";
 strEmailBody = strEmailBody + "<table><tr><th>Server Name</th><th>Host Instance Name</th></tr>";
 ++count;
 }
 strEmailBody = strEmailBody + "" + inst["RunningServer"] + "" + inst["HostName"] + "";
 inst.InvokeMethod("Start", null);
 }
 }
 if (count > 1)
 { strEmailBody = strEmailBody + "</table>All above mentioned Host Instances have been Started Successfully<br><br>"; }
 }
 catch (Exception excep)
 {
 System.Diagnostics.EventLog.WriteEntry("BizTalk Server", "Exception Occurred in enumerateAndStartHostInstances fuction call. " + excep.Message, System.Diagnostics.EventLogEntryType.Error);
 throw new Exception("Exception Occurred in enumerateAndStartHostInstances fuction call. " + excep.Message);
 }
 }

//Function to check available drive space
 private static void checkAvailableDriveSpace(ref string strEmailBody, ref string strServerNames, ref double doubleAcceptableFreeDriveSpace)
 {
 try
 {
 int count = 1;
 foreach (string strServerName in strServerNames.Split(';'))
 {
 //Connection credentials to the remote computer, not needed if the logged account has access
 ConnectionOptions oConn = new ConnectionOptions(); //oConn.Username = "DummyUserName";//oConn.Password = "*********"; 
 //Create EnumerationOptions and run wql query 
 EnumerationOptions enumOptions = new EnumerationOptions();
 enumOptions.ReturnImmediately = false;

string strNameSpace = @"\\" + strServerName + @"\root\cimv2";
 //Get Fixed disk state. Check - http://www.csidata.com/custserv/onlinehelp/vbsdocs/vbs41.htm, DriveType = 2 - Fixed, Drive has fixed (nonremovable) media. This includes all hard drives, including hard drives that are removable. DriveType = 2 -Remote, Network drives. This includes drives shared anywhere on a network.
 ManagementObjectSearcher searchObject = new ManagementObjectSearcher(strNameSpace, "select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3", enumOptions);
 //Enumerate through the result set 
 foreach (ManagementObject driveInfo in searchObject.Get())
 {
 double percentFreeSpace = 100 * (System.Convert.ToDouble(driveInfo["FreeSpace"]) / System.Convert.ToDouble(driveInfo["Size"]));
 if (percentFreeSpace < doubleAcceptableFreeDriveSpace)
 {
 if (count == 1)
 {
 strEmailBody = strEmailBody + "Available Free Space for below drives is less than acceptable value of " + doubleAcceptableFreeDriveSpace.ToString() + "%
";
 strEmailBody = strEmailBody + "<table><tr><th>Server Name</th><th>Drive Name</th><th>Total Size</th><th>Available Free Space</th><th>Percentage Free Space</th></tr>";
 ++count;
 }
 strEmailBody = strEmailBody + "" + strServerName + "" + driveInfo["Name"] + "" + driveInfo["Size"] + "" + driveInfo["FreeSpace"] + "" + percentFreeSpace + "%";
 }
 }
 }
 if (count > 1)
 { strEmailBody = strEmailBody + "</table><br><br>"; }
 }
 catch (Exception excep)
 {
 System.Diagnostics.EventLog.WriteEntry("BizTalk Server", "Exception Occurred in checkAvailableDriveSpace fuction call. " + excep.Message, System.Diagnostics.EventLogEntryType.Error);
 throw new Exception("Exception Occurred in checkAvailableDriveSpace fuction call. " + excep.Message);
 }
 }

//Function to check CPU Reading
 private static void checkCPUReading(ref string strEmailBody, ref string strServerNames, ref double doubleAcceptableCPUPercentage, ref int intThreadSleepTimeInMilliSeconds)
 {
 try
 {
 int count = 1;
 foreach (string strServerName in strServerNames.Split(';'))
 {
 PerformanceCounter totalProcessorTimeCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", strServerName);
 totalProcessorTimeCounter.NextValue();
 System.Threading.Thread.Sleep(intThreadSleepTimeInMilliSeconds);// seconds wait
 double CPUPercentage = totalProcessorTimeCounter.NextValue();
 if (CPUPercentage > doubleAcceptableCPUPercentage)
 {
 if (count == 1)
 {
 strEmailBody = strEmailBody + "CPU Usage on below servers is higher than the acceptable value of " + doubleAcceptableCPUPercentage.ToString() + "%
";
 strEmailBody = strEmailBody + "<table><tr><th>Server Name</th><th>CPU Percentage</th></tr>";
 ++count;
 }
 strEmailBody = strEmailBody + "" + strServerName + "" + CPUPercentage.ToString() + "%";
 }
 }
 if (count > 1)
 { strEmailBody = strEmailBody + "</table><br><br>"; }
 }
 catch (Exception excep)
 {
 System.Diagnostics.EventLog.WriteEntry("BizTalk Server", "Error Occurred in checkCPUReading function call. " + excep.Message, System.Diagnostics.EventLogEntryType.Error);
 throw new Exception("Error Occurred in checkCPUReading function call. " + excep.Message);
 }
 }

//Function to Check Suspended Instances
 private static void checkAllSuspendedInstances(ref string strEmailBody, ref int intAcceptableTotalSuspendedInstanceCount, ref string strOrchestrationNames, ref int intAcceptableSuspendedInstanceCountForAnOrch)
 {
 try
 {
 //Create EnumerationOptions and run wql query 
 EnumerationOptions enumOptions = new EnumerationOptions();
 enumOptions.ReturnImmediately = false;

//Search for all Service Instances for ServiceClass=1(Orchestration) or ServiceClass=4(Messaging) and (ServiceStatus=4(Suspended (resumable)) or ServiceStatus=32(Suspended (non-resumable)) in the Biztalk namespace scope 
 ManagementObjectSearcher searchObject = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer", "Select * from MSBTS_ServiceInstance Where (ServiceClass=1 or ServiceClass=4) and (ServiceStatus=4 or ServiceStatus=32)", enumOptions);
 if (searchObject.Get().Count > 0)
 {
 if (intAcceptableTotalSuspendedInstanceCount < searchObject.Get().Count)
 {
 strEmailBody = strEmailBody + "The total number of Suspended instances is higher than the acceptable value of " + intAcceptableTotalSuspendedInstanceCount.ToString() + "
";
 strEmailBody = strEmailBody + "<table><tr><th>Total Number of Suspended Instance</th></tr>";
 strEmailBody = strEmailBody + "" + searchObject.Get().Count.ToString() + "";
 strEmailBody = strEmailBody + "</table><br><br>";
 }

//Now checking for individual Orchestrations Only If Suspended Instance Count not zero
 int count = 1;
 foreach (string strOrchName in strOrchestrationNames.Split(';'))
 {
 string strWQL = string.Format("SELECT * FROM MSBTS_ServiceInstance WHERE (ServiceClass=1 or ServiceClass=4) and (ServiceStatus=4 or ServiceStatus=32) AND ServiceName like \"%{0}%\"", strOrchName);
 ManagementObjectSearcher searcherServiceInstance = new ManagementObjectSearcher(new ManagementScope("root\\MicrosoftBizTalkServer"), new WqlObjectQuery(strWQL), null);
 if (searcherServiceInstance.Get().Count > intAcceptableSuspendedInstanceCountForAnOrch)
 {
 if (count == 1)
 {
 strEmailBody = strEmailBody + "The total number of Suspended instances for below Orchestrations is higher than the acceptable value of " + intAcceptableSuspendedInstanceCountForAnOrch.ToString() + "
";
 strEmailBody = strEmailBody + "<table><tr><th>Orchestration Name</th><th>Number of Suspended Instances</th></tr>";
 ++count;
 }
 strEmailBody = strEmailBody + "" + strOrchName + "" + searcherServiceInstance.Get().Count.ToString() + "";
 }
 }
 if (count > 1)
 { strEmailBody = strEmailBody + "</table><br><br>"; }
 }
 }
 catch (Exception excep)
 {
 System.Diagnostics.EventLog.WriteEntry("BizTalk Server", "Error Occurred in checkAllSuspendedInstances function call. " + excep.Message, System.Diagnostics.EventLogEntryType.Error);
 throw new Exception("Error Occurred in checkAllSuspendedInstances function call. " + excep.Message);
 }
 }

//Function to check Fragmentation of BizTalkMsgBoxDb Table Indexes
 private static void checkBizTalkMsgBoxTablesIndexFragmentation(ref string strEmailBody, ref string strConnectionString, ref double doubleAcceptableFragmentationPercentage)
 {
 try
 {
 if (!String.IsNullOrEmpty(strConnectionString))
 {
 //Checking Index Fragmentation if the value is more than Acceptable Fragmentation Percentage, Selecting top 10 values
 string queryString = "SELECT top 10 dbtables.[name] as 'Table', dbindexes.[name] as 'Index', indexstats.avg_fragmentation_in_percent, indexstats.page_count ";
 queryString = queryString + "FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id] INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id] INNER JOIN sys.indexes AS dbindexes ON dbindexes.[object_id] = indexstats.[object_id] AND indexstats.index_id = dbindexes.index_id ";
 queryString = queryString + "WHERE indexstats.database_id = DB_ID()and avg_fragmentation_in_percent > " + doubleAcceptableFragmentationPercentage.ToString() + " ";
 queryString = queryString + "ORDER BY indexstats.avg_fragmentation_in_percent desc";
 using (SqlConnection connection = new SqlConnection(strConnectionString))
 {
 using (SqlCommand command = new SqlCommand(queryString, connection))
 {
 connection.Open();
 SqlDataReader reader = command.ExecuteReader();
 if (reader.HasRows)
 {
 strEmailBody = strEmailBody + "Fragmentation Percentage for few indexes is more than the Acceptable Value of " + doubleAcceptableFragmentationPercentage.ToString() + "% <br>";
 strEmailBody = strEmailBody + "<table><tr><th>Table Name</th><th>Index Name</th><th>Average Fragmentation in Percent</th><th>Page Count</th></tr>";
 while (reader.Read())
 {
 strEmailBody = strEmailBody + "<tr><td>" + reader["Table"] + "</td><td>" + reader["Index"] + "</td><td>" + reader["avg_fragmentation_in_percent"] + "</td><td>" + reader["page_count"] + "</td></tr>";
 }
 strEmailBody = strEmailBody + "</table>" + Environment.NewLine + "<br><br>";
 }
 }
 }
 }
 else throw new Exception("BizTalk Msg Box Connection String property is NULL");
 }
 catch (Exception excep)
 {
 System.Diagnostics.EventLog.WriteEntry("BizTalk Server", "Exception Occurred in checkBizTalkMsgBoxTablesIndexFragmentation fuction call. " + excep.Message, System.Diagnostics.EventLogEntryType.Error);
 throw new Exception("Exception Occurred in checkBizTalkMsgBoxTablesIndexFragmentation fuction call. " + excep.Message);
 }
 }

//Function to check Spool and Parts table Count and other tables with row count more than intAcceptableMsgBoxRowCount
 private static void checkBizTalkMsgBoxTablesCount(ref string strEmailBody, ref string strConnectionString, ref int intAcceptableMsgBoxRowCount)
 {
 try
 {
 int count = 1;
 if (!String.IsNullOrEmpty(strConnectionString))
 {
 //Checking top 10 tables with Row count more than intAcceptableMsgBoxRowCount
 string queryString = "SELECT top 10 T.name TableName, I.Rows NumberOfRows FROM sys.tables T JOIN sys.sysindexes I ON T.OBJECT_ID = I.ID "
 + "WHERE indid IN (0,1) and I.Rows > " + intAcceptableMsgBoxRowCount.ToString() + "ORDER BY I.Rows DESC,T.name";
 using (SqlConnection connection = new SqlConnection(strConnectionString))
 {
 using (SqlCommand command = new SqlCommand(queryString, connection))
 {
 connection.Open();
 SqlDataReader reader = command.ExecuteReader();
 if (reader.HasRows)
 {
 while (reader.Read())
 {
 if (count == 1)
 {
 strEmailBody = strEmailBody + "The Row Count of Below Tables is more than the acceptable value of " + intAcceptableMsgBoxRowCount + "
";
 strEmailBody = strEmailBody + "<table><tr><th>Table Name</th><th>Current Row Count(Exceeded Value)</th></tr>";
 ++count;
 }
 strEmailBody = strEmailBody + "<tr><td>" + reader["TableName"].ToString() + "</td><td>" + reader["NumberOfRows"] + "</td></tr>";
 }
 if (count > 1)
 strEmailBody = strEmailBody + "</table><br><br>";
 }
 }
 }
 }
 else throw new Exception("BizTalk Msg Box Connection String property is NULL");
 }
 catch (Exception excep)
 {
 System.Diagnostics.EventLog.WriteEntry("BizTalk Server", "Exception Occurred in checkBizTalkMsgBoxTablesCount fuction call. " + excep.Message, System.Diagnostics.EventLogEntryType.Error);
 throw new Exception("Exception Occurred in checkBizTalkMsgBoxTablesCount fuction call. " + excep.Message);
 }
 }

//Functoin to send email
 private static void sendEmail(string strEmailBody)
 {
 //Get SMTP details from config
 var appSettings = ConfigurationManager.AppSettings;

try
 {
 //if ((appSettings["SMTPHost"] != null) && (appSettings["FROMEmailID"] != null) && (appSettings["TOEmailID"] != null) && (appSettings["Subject"] != null) && (appSettings["SMTPServerPort"] != null) && (appSettings["Username"] != null) && (appSettings["Password"] != null))
 //{
 MailMessage mail = new MailMessage();
 SmtpClient SmtpServer = new SmtpClient(appSettings["SMTPHost"].ToString());
 mail.From = new MailAddress(appSettings["FROMEmailID"].ToString());
 mail.To.Add(appSettings["TOEmailID"].ToString());
 mail.Subject = appSettings["Subject"].ToString();
 mail.Body = strEmailBody;
 mail.IsBodyHtml = true;

SmtpServer.EnableSsl = true;
 //SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
 //SmtpServer.UseDefaultCredentials = false;
 SmtpServer.Port = System.Convert.ToInt32(appSettings["SMTPServerPort"]);
 //SmtpServer.Credentials = new System.Net.NetworkCredential(appSettings["Username"].ToString(), appSettings["Password"].ToString());

SmtpServer.Send(mail);
 System.Diagnostics.EventLog.WriteEntry("BizTalk Server", "Email Sent", System.Diagnostics.EventLogEntryType.SuccessAudit);
 //}
 //else
 //{
 // System.Diagnostics.EventLog.WriteEntry("BizTalk Server", "Error occurred while sending email - Some mandatory configuration values is missing. Please check if corresponding values are given in config file- SMTPHost, FROMEmailID, ToEmailID, Subject, SMTPServerPort, Username, Password");
 // throw new Exception("Error occurred while sending email - Some mandatory configuration values is missing. Please check if corresponding values are given in config file- SMTPHost, FROMEmailID, ToEmailID, Subject, SMTPServerPort, Username, Password");
 //}
 }
 catch (Exception expMailSend)
 {
 System.Diagnostics.EventLog.WriteEntry("BizTalk Server", expMailSend.Message, System.Diagnostics.EventLogEntryType.Error);
 throw new Exception("Error occurred while sending email - " + expMailSend.Message);
 }
 }
 }
}

Sample Email –

SampleEmail

Download the complete code from here

I will keep on adding new functionality so make sure you donwload the latest version of code.

Contact Me:- 

@Gmail@Facebook , @Twitter, @LinkedIn @MSDNTechnet, @My Personal Blog 

 

Error – “Microsoft .NET Framework 3.5 Service Pack 1 is required”- Specify an alternate source path

Hello Everyone.

Recently, I wanted to setup BizTalk 2013 R2 system and while installing SQL Server 2014 I ran into below error –

Error Message – “Microsoft .NET Framework 3.5 Service Pack 1 is required”.

Setup failed to find “Microsoft .NET Framework 3.5 Service Pack 1” and therefore we can’t proceed with the installation.

Setup failed to find "Microsoft .NET Framework 3.5 Service Pack 1" and therefore we can't proceed with the installation.

So, I went ahead and tried to install this Feature from Server Manager by following below steps –

  1. Open Server Manager and click on “Add roles and features”

Add Roles and Features Wizard

2. Select “Role-based or feature-based installation”

Add Roles and Features Wizard 2

3. Select appropriate server from the Server Pool

Add Roles and Features Wizard_ChooseServer

4. Select .Net Framework 3.5

Add Roles and Features Wizard_Select.NETFramework 3.5

5. Now you just need to confirm your selection and proceed with installation. But for me it showed below warning –

Warning – “Do you need to specify an alternate source path? One or more installation selections are missing source file on the destination server.”

Add Roles and Features Wizard_SpecifyAlternateSourcePath

If you ignore this warning and proceed with the installation, it will fail with below error.

Error Message – The source files could not be found. Try installing the roles, role services or features again in a new Add Roles and Features Wizard session, and on the Confirmation page of the wizard, click “Specify an alternate source path” to specify a valid location of the source files that are required for the installation. The location must be accessible by the computer account of the destination server.

FeatureInstallationFailed

So, the question against me was how to find and where to find an alternate source for .Net Framework 3.5 Feature. So, I spent lots of time on internet attempting to find same without any luck.

Finally I got a SQL expert who guided me with the solution, which is as below-

The alternate source path can be found in Windows Operating System media within below location –

<OS Media Drive>:\sources\sxs

Add Roles and Features Wizard_SpecifyAlternateSourcePath2

Simply say “OK” and proceed with the installation. It should be successful this time.

Add Roles and Features Wizard_FeatureInstallSuccessful

Later, proceed with SQL Installation, this time you will not receive the same error and installation will be smooth.

Hope this helps.

Contact Me:- 

@Gmail@Facebook , @Twitter, @LinkedIn @MSDNTechnet, @My Personal Blog 

BizTalk Server Migration from 2010 to 2013 R2- Why & How

This document is intended to give an overview of why & how migration from BizTalk Server 2010 to 2013 R2 can be accomplished. It also attempts to aggregate known issues or probable challenges which can erupt during this process.

Why to Migrate

Mainstream Lifecycle of BizTalk 2010 is going to end on 1/12/2016 post which Microsoft Support is limited and for extended support you will have to purchase Extended Support within 90 days of mainstream support end (which can be missed easily).

BizTalk Version Mainstream Support End Extended Support End
BizTalk Server 2010 1/12/2016 1/12/2021
BizTalk Server 2013 7/10/2018 7/11/2023
BizTalk Server 2013 R2 7/10/2018 7/11/2023

Read more about Lifecycle Information for Microsoft Server Products Support

There are lots of disadvantages of being in extended support:

  • Non-Security hotfix support will require extended hotfix agreement, purchased within 90 days of mainstream support ending
  • You incident support will be chargeable
  • You cannot make warranty claims
  • You cannot request design changes and feature requests

You probably do not want to run your business on limited support, so it’s better to upgrade if your current version is already in extended support or your mainstream support is coming.

Planning Migration

Microsoft always claims that the upgrade will be seamless and will not affect anything. However, there is always a big risk of something going wrong. Because the stack we are upgrading has lots of things like Operating System, .Net Framework, SQL, BizTalk etc. so we need to be super careful.

Upgrade vs. Migration

Two approaches can be considered:

· Upgrading of the existing BizTalk Server 2010 Infrastructure to BizTalk Server 2013 R2(In House Upgrade)

· Building a new BizTalk Server 2013 R2 Infrastructure to co-exist with the existing BizTalk Server 2010 Infrastructure to allow for gradual migration of BizTalk Applications

Benefits of In-House Upgrade

This allows you to upgrade BizTalk within the existing infrastructure. For ex- If you have a standalone BizTalk Server 2010 server, you can upgrade Operating System, SQL, BizTalk etc. just by clicking on the installer and proceeding with Upgrade option.

For more details refer In-house BizTalk Upgrade or Upgrading to BizTalk Server 2013 from BizTalk Server 2010-2009

UpgradeImage

Disadvantages of In-house upgrade

· As many softwares like Windows OS, SQL, Visual Studio, BizTalk etc. needs to be upgraded, there is higher risk of failure.

· Generally we would like to upgrade/enhance the hardware as well (better CPU, RAM, OS to 64bit etc.) which is not possible in this case.

This approach is only suitable for small businesses with simpler integration.

Migration

Here the BizTalk environment must be recreated on an operating system supported by BizTalk Server 2013 R2 and later all other upgraded applications needs to be deployed.

Steps to perform Migration

1) Recreate BizTalk Servers 2013 R2 environment with desired and supported Hardwares and Softwares.

BizTalk Server 2013 R2 Supported Softwares

Microsoft Windows Windows Server 2012 R2, Windows Server 2012, Windows 8.1, Windows 7 SP1.
Internet Information Services (IIS) The version that comes with the operating system. KB 224609 lists the versions.
Microsoft .NET Framework .NET Framework 4.5
Microsoft Visual Studio Visual Studio 2013
Microsoft SQL Server SQL Server 2014 or SQL Server 2012 SP1

Refer Hardware and Software Requirements for BizTalk Server 2013 and 2013 R2 for more details.

Follow How to install & Configure BizTalk Server 2013 R2 document for installing and configuring BizTalk.

2) Upgrade existing BizTalk Applications to latest version

a) Copy all the existing projects(folders) to newly created BizTalk 2013 R2 Servers

b) Open each solution one by one which needs to be migrated in Visual Studio 2013, this will automatically open a migration wizard. Make sure Target Framework is .Net Framework 4.5 for all projects. Finally a “Migration Report” will be generated, as shown in pic below. Go through the report to see if it has any error message.

MigrationPopUp

Migration Report

c) Ensure to change the .Net Framework versions for all related class libraries (helper class/dlls) manually to .NET 4.5 versions, by default Helper classes (Class Library) will not be upgraded.

HelperClass.NetFramework

d) Rebuild all the applications and fix any build errors.

e) Upgrade/Replace any obsolete method with their latest version. Ex- Use “System.Configuration.ConfigurationManager.GetSection” instead of “System.Configuration.ConfigurationSettings.GetConfig”.

f) You may also have to upgrade/modify your code to incorporate changes occurred in Standard External Libraries.

Ex-In our case we were using older version of “Microsoft Enterprise Library” dll which was later upgraded to 6.0 which made us to change our implementation/code as well.

g) Ensure to modify deprecated Adapters. For example SQL & SOAP adapters are deprecated in BizTalk 2013 R2, so they must be replaced by their corresponding WCF Adapters.

Note: – Deprecated doesn’t mean non-availability, it means that though you are able to use these adapters in current version of BizTalk but in future version they might get removed. Microsoft even may deny providing support. Ex- SQL adapter was deprecated with BizTalk Server 2009 was released and is now unsupported in BizTalk Server 2010 onwards.

Read here for more details.

h) Take necessary steps for upgrading any other dependencies if required.

Later these applications can be used for regular deployment in any BizTalk environments.

Note: – All Applications should be thoroughly tested in all Pre-PROD environments.

Ideally Migration Effort is minimal as most of the work is done by the wizard, but BizTalk being a middleware depends on many external systems like Databases, SAP, SFTP etc. and so the main cost/effort is of Thorough Testing.

Also, if you decide to perform Code Refactor/Enhancement efforts will be increased accordingly. For example- If you have any SOAP Web Service (.asmx) and you want to upgrade it to WCF (.svc) it will need extra efforts.

Hope it helps.

Contact Me:- 

@Gmail@Facebook , @Twitter, @LinkedIn @MSDNTechnet, @My Personal Blog 

The map name was not provided and is required.

Recently while working on my first ESB POC I faced one strange issue. I was using BRE Resolver to resolve the map name for transformation, however I was getting below error.

Error Message:- There was a failure executing the receive pipeline: “Microsoft.Practices.ESB.Itinerary.Pipelines.ItinerarySelectReceiveXml, Microsoft.Practices.ESB.Itinerary.Pipelines, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35” Source: “ESB Dispatcher” Receive Port: “GetEmpRequest_RP” URI: “E:\Prashant\POC\IN\*Copy*.xml” Reason: The map name was not provided and is required.

I tested my Rule using “Test Policy” functionality in “Business Rule Composer” which was giving the expected output. I even looked into the ESB Traces using Debug View but all in vain.

I was using “Microsoft.Practices.ESB.ResolverProviderMessage” as Document Type as suggested in many tutorials and videos.

After struggling for hours and I found the below blog-

https://maximelabelle.wordpress.com/2010/06/10/resolving-multiple-maps-in-sequence-with-the-busines-rules-engine-in-biztalk-esb-toolkit-2-0/

Which suggests that the type of the incoming message is mistakenly determined to be “Microsoft.Practices.ESB.ResolveProviderMessage” i.e. Resolve instead of Resolver.

debug_view_log

Later by making Document Type as above things started working.

But this proves how little time and money Microsoft has invested in development of ESB Toolkit.

Contact Me:- 

@Gmail@Facebook , @Twitter, @LinkedIn @MSDNTechnet, @My Personal Blog 

Step by Step Guide to Install & Setup ESB Toolkit and Management Portal (Standalone Machine)

Installing and Configuring ESB Portal is really hectic & cumbersome task. I myself faced many roadblocks so thought to document it here, hope it’s useful.

Pre-requisites:

1. Install Both VS 2012 and 2013 (BT 2013 Dev tools & ESB toolkit 2.3 requires some components from VS 2012 which I haven’t figure out yet if you can figure out please share it across).

2. Install SQL Server with Analysis Services and ensure it’s running under the LocalSystem account

3. Install BizTalk Server 2013 R2, LOB WCF Adapters and ESB Toolkit.

4. Configure BizTalk with full BAM configuration. Ensure to check “Enable Analysis Services for BAM aggregations”, give the Server Name and Apply Configurations.

5. Install Enterprise Library 5.0 from below link-

http://www.microsoft.com/en-us/download/details.aspx?id=15104

6. Install Microsoft Report Viewer Redistributable 2008 from below link-

http://www.microsoft.com/en-us/download/details.aspx?id=577

Add Roles & Features:-

Roles:-

a) IIS with IIS 6 compatibility Tools(complete)

b) Windows Authentication & Basic Authentication(Under : Web Server(IIS) -> Security)

Features:-

a) WCF & HTTP Activation for both .Net 3.5 and 4.0/4.5

Setting up the ESB Samples and BAM

1. Add the System Account to the BAM STAR Database

a. Open SQL Management studio and connect to server having BAM Star Schema database

b. Run the following command:

use BAMStarSchema

EXEC sp_addrolemember ‘db_owner’, ‘NT AUTHORITY\SYSTEM’

2. Create a folder named: C:\Projects\Microsoft.Practices.ESB

3. Unzip C:\Program Files (x86)\Microsoft BizTalk ESB Toolkit\ESBSource.zip file into this folder

4. Remove Read-only attribute from all files in C:\Projects\Microsoft.Practices.ESB folder.

5. Open an elevated Visual Studios command prompt

a. Setup BAM

i. Navigate to C:\Program Files (x86)\Microsoft BizTalk Server 2013 R2\Tracking

ii. Run Bm.exe deploy-all -DefinitionFile:”C:\Program Files (x86)\Microsoft BizTalk ESB Toolkit\Bam\Microsoft.BizTalk.ESB.BAM.Itinerary.xml”

    iii. Run Bm.exe deploy-all -DefinitionFile:”C:\Program Files (x86)\Microsoft BizTalk ESB Toolkit\Bam\Microsoft.BizTalk.ESB.BAM.Exceptions.xml”

b. Re-register the Itinerary Visual Studio templates

i. Run devenv.exe /setup

c. Create the strong name key for the Samples

i. Run sn -k c:\Microsoft.Practices.ESB.snk

ii. Copy this to C:\Projects\Microsoft.Practices.ESB\Keys

6. Fix the WCF Resubmit Port

a. Navigate to C:\Program Files (x86)\Microsoft BizTalk ESB Toolkit\Web\ESB.ItineraryServices.WCF

b. Open the web.config file

c. Locate: <soapHeaderMetadata enabled=”true”> and Update to be <soapHeaderMetadata enabled=”false”>

Configure ESB

1) Run ESB Configuration Tool as administrator

2) In the ESB Configuration Tool, from the left pane, select ESB Configuration. On the right pane, for Database Server, specify the database server name where the databases required for ​BizTalk ESB Toolkit will be created.

3) In the IIS Web Services box, specify the user credentials under which the applications required for BizTalk ESB Toolkit are created. Also specify the name of the website in IIS under which the applications are created.

Note: – At this stage, you can click Apply Configuration towards the top of the ESB Configuration Tool to configure the BizTalk ESB Toolkit with the default settings. However, if you want to do a custom configuration, you can perform the remaining steps as well. In such a case, the values you specify in the subsequent steps take precedence over the default values.

4) From the left pane, expand Exception Management, and do the following:

5) Click the Database node, and clear the Enable Exception Management Database check box if you do not want to configure an exception management database.

6) Similarly, on the Database node, select the Use Existing Database checkbox if you want to use an existing database instead of the configuration tool creating a new database. You can also specify the database server name and the database name.

7) On the Exception Web Services node, clear the Enable Exception Services check box if you do not want to configure these services. Alternatively, if you want to run these services under a different website, you can specify that here too.

8) From the left pane, expand ESB Core Components, and do the following:

9) Click the Itinerary Database node, and clear the Itinerary Database check box if you do not want to configure an itinerary database.

10) Similarly, on the Itinerary Database node, select the Use Existing Database checkbox if you want to use an existing database instead of the configuration tool creating a new database. You can also specify the database server name and the database name.

11) On the Core Web Services node, clear the Enable Core Services check box if you do not want to configure these services. Alternatively, if you want to run these services under a different website, you can specify that here too.

12) From the left pane, click Configuration to specify the SSO configuration. If you are installing and configuring the BizTalk ESB Toolkit in a single server environment, you should select File Configuration Source, which is the default selection. However, if you are setting up a multiple-machine deployment, you must select the SSO Configuration Source, and then provide the following values.

13) SSO Server: Name of the SSO server.

14) Configuration file: Click the ellipsis button (…), and then browse to the Esb.config file, which is included in the ​Microsoft BizTalk ESB Toolkit

15) Application Name: Type a name for the SSO application. For example, ESB Toolkit.

16) Contact Information: Type the appropriate contact information in the following format: someone@example.com.

17) Administrator Group Name: Click the ellipsis button (…), and then browse to the appropriate name

18) User Group Name: Click the ellipsis button (…), and then browse to the appropriate name

19) Click Apply Configuration. Open IIS and notice that the applications required for ​BizTalk ESB Toolkit are now created under the website you specified while configuring ​BizTalk ESB Toolkit.

20) In the ESB Configuration Tool, on the ESB BizTalk Applications node, from the right pane, do the following:

21) Select the Enable ESB Core Components in BizTalk Server check box to create the application in the BizTalk Server Administration console. Select the Use Default Binding to bind this application to the default host. Select the Do not use Default Binding if you do not want to bind the application to the default host. In such a case, you must explicitly bind the application to a host, once the application is created.

22) Select the Enable ESB JMS/WMQ Components in BizTalk Server check box to create the application in the BizTalk Server Administration console. Select the Use Default Binding to bind this application to the default host. Select the Do not use Default Binding if you do not want to bind the application to the default host. In such a case, you must explicitly bind the application to a host, once the application is created.

23) Click Apply Configuration to create the applications you selected. Verify that the applications are created in the BizTalk Server Administration console.

Note: – Status of the configuration will be visible on the bottom left of this tool. Wait till it displays message – EsbBizTalkApplication: Configured

Refer: – https://msdn.microsoft.com/en-us/library/jj684558.aspx for more details

Configuring ESB Management Portal

  1. In Visual Studio 2012, open ESB.Portal.sln found in C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\ESB.Portal
  2. Click OK to the dialog pop up about the solution being unsupported by Visual Studio 2012
  3. Close the Migration Report IE window that opens once migration is complete
  4. In the ESB.BAM.Service.Implementation C# project add a reference to Microsoft.Practices.ServiceLocation.dll. This can be found at C:\Program Files (x86)\Microsoft Enterprise Library 5.0\Bin
  5. Open web.config file found in ESB.Portal project. Comment out the following section if present(ideally shouldn’t be there in BizTalk 2013 R2):

<sectionGroup name=”scripting”>…

  1. Save all
  2. Select the ESB.Portal project, right-click rebuild this project (note the full Solution will not build)
  3. Close visual studios
  4. Open a 32-Bit PowerShell windows as Administrator
  5. Type: Set-ExecutionPolicy –ExecutionPolicy Unrestricted (confirm Y when promoted)
  6. Run Management_Install.cmd file found in C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts

If everything is good, there shouldn’t be anything in RED.

  1. Finally Open http://localhost/ESB.Portal

Ensure you click on the Gray Gear Icon and change the selection to All Applications

Note: – Add the User (Identity) for which ESB Application Pools is configured and “Network Service” to BizTalk Isolated Host Users group. This will allow these websites to post messages in BizTalkMessageBox.

BizTalk Tracing

ESB processing can get complicated, so I would always suggest you to enable tracing.

1. In Notepad open the machine.config file for both 32 & 64 bit, placed at below locations

C:\Windows\Microsoft.NET\Framework\v4.0.30319\CONFIG\machine.config for 32-bit
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\CONFIG\machine.config for 64-bit

2. Locate the line with the closing configSections tag.

3. Under that line paste the following section:

<system.diagnostics>

<switches>

<add name=”BizTalkESBToolkit” value=”4″/>

</switches>

</system.diagnostics>

4. Save and close machine.config.

5. Start the DebugView program (Download)

6. In DebugView, on the Capture menu, click Capture Global Win32 to make sure that it is checked.

7. In the BizTalk Server Administration console, restart the BizTalkServerApplication host instance.

Refer below link for more details: –

https://midheach.wordpress.com/2013/05/13/tracing-with-esb-toolkit-2-2/

Known Issues:-

1) ESB Toolkit BRE Itinerary Resolver Fails with Exception:-

The following exception is thrown with ESB 2.2 & 2.3 on Microsoft BizTalk Server 2013 when the ItinerarySelectReceiveXML receive pipeline is used to call the Business Rules Engine to dynamically select an itinerary (using the ESB BRI Resolver).

Error Message:-

Exception has been thrown by the target of an invocation.

Source: Microsoft.Practices.ESB.Resolver.ResolverMgr

Solution: – It requires some changes in ESB.Config file, please check the below link about this issue.

https://support.microsoft.com/en-us/kb/2887594?wa=wsignin1.0

Contact Me:- 

@Gmail@Facebook , @Twitter, @LinkedIn @MSDNTechnet, @My Personal Blog 

My BizTalk Experiences