Thursday, June 7, 2012

Logging idea

good idea to log time to see how long a process is taking


//logging proc
public static void Write(string message, DateTime beginTimeStamp)
        {
            double elapsedMS = DateTime.Now.Subtract(beginTimeStamp).TotalMilliseconds;
            _logger. Write (message + " (" + elapsedMS + ")");
        }

//client
public string GetBlah()
         {
             Logger.Debug("GetBlah Begin");
             DateTime beginTimeStamp = DateTime.Now;
             Logger.Debug("GetBlah End", beginTimeStamp);
             return “”;
         }

Adding your own security policy to WCF


<behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" />
          <serviceDebug includeExceptionDetailInFaults="True" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <serviceAuthorization principalPermissionMode="Custom">
            <authorizationPolicies>
              <add policyType="blah" />

Blah will have to implement  IAuthorizationPolicy
Issuer can be disregarded thus:
public System.IdentityModel.Claims.ClaimSet Issuer
        {
            get { throw new NotImplementedException(); }
        }
But at least evaluate should be implemented
public bool Evaluate(EvaluationContext evaluationContext, ref object state)
        {

At this point you can add information to the authorization policy thus
evaluationContext.Properties["Principal"] = new ReportingPrincipal(identity);

later this information can be derived thus
var principal = ServiceSecurityContext.Current.AuthorizationContext.Properties["Principal"] as ReportingPrincipal;

calendar for asp.net

had so many problems with calendar extender finally used this

Tuesday, June 5, 2012

WCF converting exceptions to faults


To return all errors (dev) without any configurations:
<serviceBehaviors>
  <behavior name="Debug">
    <serviceDebug includeExceptionDetailInFaults="true" />
  </behavior>
</serviceBehaviors>

WCF   converting exceptions to faults
1)Create basic class
[DataContract]
    public class BlahServiceFault
    {
        [DataMember]
        public string Message { get; set; }

        [DataMember]
        public Guid Id { get; set; }
    }

2)Add reference to Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling

3)Add to interface
[OperationContract]
        [FaultContract(typeof(BlahServiceFault))]
        IEnumerable<blases> GetUserBlases();

4)add to service config file
<exceptionHandling>
    <exceptionPolicies>
      <add name="WCF Exception Shielding">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" exceptionMessage="An error occured while processing the request." faultContractType=" BlahServiceFault" name="Fault Contract Exception Handler">
                <mappings>
                  <add source="{Message}" name="Message" />
                  <add source="{Guid}" name="Id" />
                </mappings>
              </add>
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>


5)Usage in client
try
           {
               url = client.GetUserBlases(Params);
           }
           catch (FaultException<BlahServiceFault> ex)
           {
               lblError.Text = ex.Detail.Message;
           }

Diagnostics in WCF

for diagnostics - add the following to config

<system.diagnostics>
    <sources>
      <source propagateActivity="true" name="System.ServiceModel" switchValue="Error,ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default"></add>
          <add initializeData="c:\logs\blah.svclog" type="System.Diagnostics.XmlWriterTraceListener" name="traceListener"></add>
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

interestingly, if you add this twice the config still works.
 for bugs that stop the request from ever reaching the service, check the event log

Monday, June 4, 2012

ajax toolkit

I was creating calendar controls on the fly and this was producing bizarre javascript errors. The answer -
I was using plain scriptmanager instead of
 <asp:ToolkitScriptManager ID="Scriptmanager1" runat="server"></asp:ToolkitScriptManager>


The HTTP service located at blah is too busy

Could just mean the application pool is down