Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Monday, December 16, 2013

Jquery: Displaying Object Property Names and Values

Jquery Displaying Object Property Names and Values

Here is one way to display a JSON object’s property name and value.  I used this to display the results of a test service call to a paging service.

C# class:
 /// 
    /// Container class to hold results of paging service calls
    /// 
    public class PagerComm
    {

        public string Address { get; set; }
        public string Domain { get; set; }
        public string ElaspedTime { get; set; }
        public string Env { get; set; }
        public string Error { get; set; }
        
        
        public PagerComm()
        {

        }

        public PagerComm(string address, string domain, string elaspedTime, string env,string error)
        {
            this.Address = address;
            this.Domain = domain;
            this.ElaspedTime = elaspedTime;
            this.Env = env;
            this.Error = error;

        }





    }


Asmx method returning the class:

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public PagingTools.PagerComm SendTestPage(string tstTyp, string pgrId, string pgrTyp)
    {
        PagingTools.PagerComm ret = new PagingTools.PagerComm();
 
        try
        {
 
            PagingTools.ETestPageService svc = new PagingTools.ETestPageService("local:dxf", "test");
 
            ret = svc.TestEpicPaging(tstTyp, pgrId, pgrTyp);
            
            
            return ret;
        }
 
 
        catch (Exception e)
        {
            //TODO:  log e.message etc.
 
            return ret;
        }
 
 
    }
    




Service call:

function SendTestPage() {
    var serviceUrl = '..//Tools.asmx/SendTestPage';
    var tstTyp = $("#tstTypDdl option:selected").val();
    var pgrId = $("#pgrIdTxt").val();
    var pgrTyp = $("#pgrTypTxt").val();
 
    $.ajax({
        type: "POST",
        data: "{'tstTyp':'" + tstTyp + "', 'pgrId':'" + pgrId + "', 'pgrTyp':'" + pgrTyp + "'}",
        contentType: "application/json; charset=utf-8",
        url: serviceUrl,
        dataType: "json",
        success: function (response) {
            ParsePageComm(response.d);
        },
        error: function (response) {
            alert("failed service call" + serviceUrl);
        }
 
 
    });
 
}


Call back function:

function ParsePageComm(result) {
    if (result) {             
        $("#resultsDiv").html('');
        $.each(result, function (prpNm, prpVl) {
            if (prpNm.indexOf("_") < 0)
                $("#resultsDiv").html($("#resultsDiv").html() + prpNm + ": " + prpVl + "
");
        });
 
    }
    else {
        alert("Error with Service ");
    }
 
 
}




Example UI:

Address:
Domain:
ElaspedTime: 1004.805
Env:
Error: Failed to execute E3C command.


Wednesday, December 11, 2013

Adding a simple custom section in App.config c#

In App.config: you need to set up a configSections / sectionGroup sections, then you can add your custom sections below appSettings:

xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="EpicSettings">
      <section name="EpicVersions" type="System.Configuration.DictionarySectionHandler"/>
    </sectionGroup>
  </configSections>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <appSettings>
    <add key="rootLevel" value="C:\Program Files (x86)\Epic"/>
    
    <add key="drFltr" value="Interconnect-*"/>
    <add key="DevVersion" value="Interconnect-DEV"/>

    <add key="CmdBldr" value="CommandBuilder.exe"/>
    <add key="CnfgEdt" value="ConfigEditor.exe"/>
    <add key="TrcVw" value="TraceViewer.exe"/>

    <add key="GnrlCnfg" value="\Web\App_Data\Config\General.config"/>

    <add key="WbSvPxyBldrSubDir" value="\Tools\"/>
    <add key="WbSvPxyBldr" value="WebServiceProxyBuilder.exe"/>

    <add key="PhsGenSvcSubDir" value="\Web\bin\"/>

  </appSettings>
  
  <EpicSettings>
    <EpicVersions>
      <add key="v7.9" value="Epic 2012"/>
      <add key="v8.1" value="Epic 2014"/>
    </EpicVersions>
  </EpicSettings>

</configuration>



In code:  Here is one way to access your custom section in the code:  Here we’re trying to determine the Epic Version based on if the directory path contains a string matching one of the config keys.

private string getEpicVersion(string vrsnpth)
        {
            string ret = string.Empty;
 
            var epicVersions = (ConfigurationManager.GetSection("EpicSettings/EpicVersions") as System.Collections.Hashtable)
                 .Cast()
                 .ToDictionary(n => n.Key.ToString(), n => n.Value.ToString());
 
            foreach (System.Collections.Generic.KeyValuePair<string, string> vrsn in epicVersions)
            {
                if (vrsnpth.ToLower().Contains(vrsn.Key.ToLower()))
                {
                    ret = vrsn.Value;
                    break;
                }
 
            }
 
            return ret;
        }





Tuesday, November 26, 2013

some code refactor

Before:

            PAS.UserAuths ret = new PAS.UserAuths();
            PAS.PASservicesSoapClient svc = new PAS.PASservicesSoapClient();
            var securityHeader = new PHSCredentialsHeader()
            {
                Username = _Username,
                Password = _Password
            };

            using (System.ServiceModel.OperationContextScope contextScope = new System.ServiceModel.OperationContextScope(svc.InnerChannel))
            {
                System.ServiceModel.OperationContext.Current.OutgoingMessageHeaders.Add(securityHeader);

                                     ...

After using dll:

            PAS.UserAuths ret = new PAS.UserAuths();
            PAS.PASservicesSoapClient svc = new PAS.PASservicesSoapClient();
 
            PHSServiceAI.PHSServiceWrapper sw = new PHSServiceAI.PHSServiceWrapper(_Username, _Password, svc.InnerChannel);           
 
            using (sw.cScope)
            {
                                ...




DLL Code:

namespace PHSServiceAI
{
    /// 
    /// Security Header Wrapper for PHS AI Servcies
    /// 
    public class PHSServiceWrapper
    {
        public string UserId { get; set; }
        public string Password { get; set; }
        public System.ServiceModel.OperationContextScope cScope { get; set; }
 
        public PHSServiceWrapper()
        {
 
        }
 
        public PHSServiceWrapper(string uName, string pw, System.ServiceModel.IContextChannel ic)
        {
 
            try
            {
 
                this.UserId = uName;
                this.Password = pw;
                PHSCredentialsHeader crdHdr;
 
                crdHdr = new PHSCredentialsHeader()
                {
                    Username = this.UserId,
                    Password = this.Password
                };
 
                cScope = new System.ServiceModel.OperationContextScope(ic);
                System.ServiceModel.OperationContext.Current.OutgoingMessageHeaders.Add(crdHdr);
 
 
            }
            catch (Exception e)
            {
                //TODO log e.Message etc.
            }
 
 
        }
    }
}