Web.Config

		<add key="RCT-Use-Proxy" value="true" />
		<add key="RCT-Proxy"     value="http://10.101.101.101:8080" />

C# Code:


            string strUseProxy = ConfigurationManager.AppSettings["RCT-Use-Proxy"];
            string strProxy = ConfigurationManager.AppSettings["RCT-Proxy"];
            bool useProxy = System.Convert.ToBoolean(strUseProxy);
            if (useProxy)
            {
                valservice.Proxy = new System.Net.WebProxy(strProxy, true);
            }

Believe it or not, WCF is not “turned on” by default in IIS 7.

Run ?servicemodelreg -i? from

c:WindowsMicrosoft.NETFrameworkv3.0Windows Communication Foundation

The following was confirmed to work with Visual Studio 2008 in Dec 2009.

1. In Visual Studio, right click on your existing solution and select “add” then “New Project”. Select “Other Project Types” ? “Setup & Deployment” ? “Web Setup Project”.

2. Right-click on your new project and select “Add” then “Project Output”. Select the desired WCF project from the select box at the top, and make sure to select BOTH “Primary Output” and “Content Files”.

3. Build your project

4. Under the debug directory, you will find the generated .msi file.

5. Run the MSI. You can install into a different directory on your local machine to see if it works. I suggest after the install, you open IIS, verify, and in IIS 7 you use the content tab, click right-click “Browse” on one of your .svc files, and make sure it opens correctly in the browser.

Issues:

1. The default IIS Virtual Directory name is the same as your Visual Studio Project name. I’m trying to figure out how to change that …

BONUS:

1. Add a dialog box with four buttons. I named the button “EnvironmentButton”.
Set values of each button to literals such as LOCAL, QA, STAGING, PROD.

2. I created a directory called CustomWebConfigs, with subdirectories called QA, STAGE, and PROD. Each one has a file called web.config. I used this disk structure so the file could always be called web.config and no need to rename it.

3. I did an “add file” for the CustomWebConfigs/Prod/Web.Config and set the “condition” property to: EnvironmentButton = “PROD”

NOTE2: Use only one equal sign, not two! Enclose the value in double quotes.

4. Set TargetName property to “Web.config”
5. Repeat steps 3-4 for each web.config file that you need.

Xref Database Routines

BizTalk has the ability to store cross reference (xref) values in the BizTalkMgmtDB. Usually, mapping functoids handle the conversion, but the following routines allow the same lookups from anywhere you can call C#.


        /// <summary>
        /// This does the SQL lookup/conversion to the BizTalk XREF tables.
        /// </summary>
        /// <param name="appType"></param>
        /// <param name="valueXRefName"></param>
        /// <param name="strLookupValue"></param>
        /// <returns></returns>
        static string GetLookupCommonValue(string appType, string valueXRefName, string strLookupValue)
        {

            SqlConnection sqlConn = new SqlConnection(GetBizTalkDBConnectionString());

            sqlConn.Open();

            string query =
            "select commonValue from xref_ValueXRefData " +
            "   inner join xref_ValueXref on xref_ValueXref.valueXrefId = xref_ValueXRefData.valueXRefID " +
            "   inner join xref_AppType on xref_AppType.appTypeId = xref_ValueXRefData.appTypeID " +
            "   where valueXRefName = '" + valueXRefName + "'" +
            "   and appType = '" + appType + "'" +
            "   and appValue = '" + strLookupValue + "'";
            //Console.WriteLine("query=" + query);

            SqlCommand cmd = new SqlCommand(query);
            cmd.Connection = sqlConn;
            SqlDataReader rdr = cmd.ExecuteReader();


            bool gotRow = rdr.Read(); // only expecting one row - no reason to loop here
            if (!gotRow)
            {
                sqlConn.Close();
                throw new Exception("strLookupValue=" + strLookupValue + " not found.  (BizTalkMgmtDb) No value found for query=" + query);
            }
            string commonValue = (string)rdr["commonValue"];
            //Console.WriteLine("CommonValue=" + commonValue);
            sqlConn.Close();

            return commonValue;
        }


        static string GetLookupCommonValueByKeys(int valueXRefId, int appTypeId, string strLookupValue)
        {

            SqlConnection sqlConn = new SqlConnection(GetBizTalkDBConnectionString());

            sqlConn.Open();

            string query = "select commonValue from xref_ValueXRefData where valueXRefID = " + valueXRefId +
                " and appTypeID = " + appTypeId +
                " and appValue = '" + strLookupValue + "'";

            //Console.WriteLine("query=" + query);

            SqlCommand cmd = new SqlCommand(query);
            cmd.Connection = sqlConn;
            SqlDataReader rdr = cmd.ExecuteReader();


            bool gotRow = rdr.Read(); // only expecting one row - no reason to loop here
            if (!gotRow)
            {
                sqlConn.Close();
                throw new Exception("strLookupValue=" + strLookupValue + " not found.  (BizTalkMgmtDb) No value found for query=" + query);
            }
            string commonValue = (string)rdr["commonValue"];
            //Console.WriteLine("CommonValue=" + commonValue);
            sqlConn.Close();

            return commonValue;
        }

        /// <summary>
        /// xref tables are part of BizTalkMgmtDb.  Any system running BizTalk with have the connection string
        /// in the registry.  The following  common routine  finds that conn-string in the registry. 
        /// </summary>
        /// <returns></returns>
        private static string GetBizTalkDBConnectionString()
        {
            string connString =
                TFBIC.Common.BizTalk.Components.MapHelper.DynamicSQLConnectionString("BizTalkMgmtDb");
            //Console.WriteLine("ConnString=" + connString);

            // Above routine was changed so as not to include the OLEDB provider.
            //connString = connString.Replace("Provider=SQLOLEDB.1;", "");  // remove old OLEDB provider
            //Console.WriteLine("ConnString=" + connString);
            return connString;

        }
        // This was before we had two methods to get connection strings
        private static string GetBizTalkDBConnectionString2()
        {
            string connString =
                TFBIC.Common.BizTalk.Components.MapHelper.DynamicSQLConnectionStringOLEDB("BizTalkMgmtDb");
            //Console.WriteLine("ConnString=" + connString);

            connString = connString.Replace("Provider=SQLOLEDB.1;", "");  // remove old OLEDB provider
            //Console.WriteLine("ConnString=" + connString);
            return connString;

        }



Converts several date/time formats to standard XML Date/ Time


        /// <summary>
        /// This takes a vendor's non-standard dates, such as 5/1/2008 and formats as XML Date.
        /// (i.e. the month may one or two digits, the day may be one or two digits).
        /// Neal Walters - changed on 09/03/2008 to  throw exception. 
        /// also tested to make sure it handles all the following dates (and it does)
        ///    string testDate1 = "03/09/2008";
        ///    string testDate2 = "1999-05-31";
        ///    string testDate3 = "03/09/2008 10:50:16.157";
        ///    string testDate4 = "1999-05-31T13:20:00.000-05:00";
        ///    string testDate5 = "2008-09-03 10:50:16.157";
        ///    string testDate6 = "2008-09-32";
        /// </summary>
        /// <param name="inDate"></param>
        /// <returns></returns>
        public static string ConvertDateTimetoXmlDateTime(string inDate)
        {
            try
            {
                DateTime dtIn = DateTime.Parse(inDate);
                return System.Xml.XmlConvert.ToString(dtIn, System.Xml.XmlDateTimeSerializationMode.Local);

            }
            catch
            {
                string errorMessage = "invalid-Date/Time-conversion from value='" + inDate + "'";
                throw new Exception(errorMessage);
                //return errorMessage;
            }
        }

We had a case where many maps need to convert YYYYMMDD date format to a standard XML date.


        /// <summary>
        /// Convert date in format YYYYMMDD to the standard XML Date format
        /// </summary>
        /// <param name="inDate"></param>
        /// <returns></returns>
        public static string ConvertYYYYMMDDtoXmlDate(string inDate)
        {
            try
            {
                return inDate.Substring(0, 4) + "-" +
                        inDate.Substring(4, 2) + "-" +
                        inDate.Substring(6, 2);
            }
            catch
            {
                return "invalid-YYYYMMDD-conversion from value=" + inDate + "'";
            }
        }

A way to get the BizTalk SQL Server name from the register, and use it to build a connection string:

        /// <summary>
        ///  Code sample of how to get connection string from the  Registry. 
        /// </summary>
        /// <param name="DBName"></param>
        /// <returns></returns>
        public static string DynamicSQLConnectionStringOLEDB(string DBName)
        {
            try
            {

                Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWAREMicrosoftBizTalk Server3.0Administration", false);

                string ConnString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=" + DBName + ";Data Source=" + rk.GetValue("MgmtDBServer").ToString();

                return ConnString;

            }
            catch
            {
                return "";
            }
        }

This code builds an xml document in memory, then returns it as an System.Xml.XmlDocument. It also show the proper way to build an XML date.

        /// <summary>
        /// This routine is needed when an in-bound endpoint has a map failure when translating
        /// inbound schema to  TMXML. 
        /// </summary>
        /// <returns></returns>
        public static XmlDocument ReturnNewTMXML()
        {
            DateTime currentDateTime = DateTime.Now;
            string strDateTime = currentDateTime.ToLongDateString();

            string strMyDoc =
            "<n2:MyRootEl xsi:schemaLocation='http://MyNamespace.MySchema.xsd' +
'xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" +
                "<source>" +
                  "<objectId>NULL</objectId>" +
                  "<objectType>NULL</objectType>" +
                  "<requestor>NULL</requestor>" +
                  "<transactionGuid>NULL</transactionGuid>" +
                  "<requestType>NULL</requestType>" +
                  "<dateTimeReceived>" + TFBIC.Common.BizTalk.Components.MapHelper.ConvertDateTimetoXmlDateTime(strDateTime) + "</dateTimeReceived>" +
                  "<isResponseExpected>false</isResponseExpected>" +
                "</source>" +
            "</n2:MyRootEl>";

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.PreserveWhitespace = true; //making sure no whitespaces get added
            xmlDoc.LoadXml(strTMXML);
            return xmlDoc;

        }

This sample shows how to build an XML node and add it as a new child in an existing

        /// <summary>
        /// This routine is needed when an in-bound endpoint Sends TMXML with no Itinerary Node in the Header
        /// The Receiver must make sure that the Itinerary node is present in the Header.
        /// </summary>
        /// <returns></returns>
        public static XmlDocument AddItineraryNode(XmlDocument xmlDoc)
        {
            xmlDoc.PreserveWhitespace = true; //making sure no whitespaces get added
            string strXPath = "//*[local-name()='ItineraryStep']";
            string strItinerary =
                    "<policyName>NULL</policyName>" +
                     "<status>NULL</status>" +
                     "<currentStepName>NULL</currentStepName>" +
                     "<currentStepStatus>NULL</currentStepStatus>" +
                     "<currentStepIndex>0</currentStepIndex>" +
                     "<currentStepErrorType>NULL</currentStepErrorType>" +
                     "<currentProcessCode>NULL</currentProcessCode>" +
                     "<currentProcessMethod>NULL</currentProcessMethod>" +
                     "<itinerarySteps></itinerarySteps>";
            XmlNamespaceManager iMgr = new XmlNamespaceManager(xmlDoc.NameTable);
            XmlNode nodeItin = xmlDoc.SelectSingleNode(strXPath, iMgr);

            if (nodeItin == null)
            {
                XmlElement newItinerary = xmlDoc.CreateElement("Itinerary");
                newItinerary.InnerXml = strItinerary;
                xmlDoc.DocumentElement.FirstChild.AppendChild(newItinerary);
            }

            return xmlDoc;

        }


This can be useful for generating test data.
Or perhaps for generating a random delay in seconds.

        public static double GetRandomNumber(double minimum, double maximum)
        {
            Random random = new Random();
            return random.NextDouble() * (maximum - minimum) + minimum;
        }