Pablo Retyk's blog

"An error occurred while trying to display the error message."

Sponsors

News

 Subscribe in a reader

Blogroll

Adding a Reference Path to all your .NET projects

This is usually useful when you have several projects that have references to other's projects or 3rd parties assemblies, or for your compile server, instead of adding the reference paths to the project properties for each project you can just add the following key to the registry:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v2.0.50727\AssemblyFoldersEx\MyAssemblies]
@="p:\\somefolder\\somebuild\\bin"
EDIT: for 64 bits machines use this:
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v2.0.50727\AssemblyFoldersEx\MyAssemblies]
@="p:\\somefolder\\somebuild\\bin"
Override WCF client settings for custom config file using DuplexChannel

WCF config files are a great thing, once you have everything configured you just need 2 or 3 lines of code and everything get parsed and you are able to consume the service. By default WCF configuration is stored in the app.config or web.config of the main application, but sometimes (as was my case) the client is in-process of another unknown application, if so the WCF config file will have to be named SomeApp.exe.config and would need to be placed in the same folder as the application. Dealing with this can make things difficult, this is when you have to create your own ChannelFactory, this way you can provide your own config file. The solution I found after doing some goggling was to inherit from ChannelFactory and the pass the config file to the constructor

public CustomChannelFactory(string configurationPath)
     : base(typeof(T))
{
     this.configurationPath = configurationPath;
}

 

and the override CreateDescription method in which we set our config file. This works great, but not for my case since I was using a DuplexChannel, so when we inherith from DuplexChannelFactory we must call the base constructor with the CallBackInstance parameter, so the CreateDescription will be called before we can initialize the configurationPath member. The only solution I had in mind was make ConfigurationPath to be static member and then we initialize this before we create the instance of our CustomDuplexChannel, this solution seemed "ugly" for me at first but then I realized that the configuration can be stored in a static member since we are planning to use one configuration file for consuming all our services.

public class CustomDuplexChannelFactory<TChannel> : DuplexChannelFactory<TChannel>
    {
        /// <summary>
        /// Gets or sets the configuration path.
        /// </summary>
        /// <value>The configuration path.</value>
        public static string ConfigurationPath { get; set; }
 
        /// <summary>
        /// Custom client channel constructor.
        /// </summary>
        public CustomDuplexChannelFactory(InstanceContext callbackInstance)
            : base(callbackInstance)
        {
        }
 
        /// <summary>
        /// Overrides the CreateDescription() method of the channel factory
        /// to apply a new configuration file.
        /// </summary>
        /// <returns></returns>
        protected override ServiceEndpoint CreateDescription()
        {
            ServiceEndpoint serviceEndpoint = base.CreateDescription();
            if(ConfigurationPath == null || !File.Exists(ConfigurationPath))
                return base.CreateDescription();
 
            ExeConfigurationFileMap executionFileMap = new ExeConfigurationFileMap();
            executionFileMap.ExeConfigFilename = ConfigurationPath;
 
            System.Configuration.Configuration config = 
                ConfigurationManager.OpenMappedExeConfiguration(
                executionFileMap, ConfigurationUserLevel.None);
            ServiceModelSectionGroup serviceModeGroup = 
                ServiceModelSectionGroup.GetSectionGroup(config);
 
            ChannelEndpointElement selectedEndpoint = null;
 
            foreach(ChannelEndpointElement endpoint in serviceModeGroup.Client.Endpoints)
            {
                if(endpoint.Contract == serviceEndpoint.Contract.ConfigurationName)
                {
                    selectedEndpoint = endpoint;
                    break;
                }
            }
 
            if(selectedEndpoint != null)
            {
                if(serviceEndpoint.Binding == null)
                {
                    serviceEndpoint.Binding = CreateBinding(selectedEndpoint.Binding, serviceModeGroup);
                }
 
                if(serviceEndpoint.Address == null)
                {
                    serviceEndpoint.Address = new EndpointAddress(selectedEndpoint.Address,
                        GetIdentity(selectedEndpoint.Identity), selectedEndpoint.Headers.Headers);
                }
 
                if(serviceEndpoint.Behaviors.Count == 0 && 
                    !String.IsNullOrEmpty(selectedEndpoint.BehaviorConfiguration))
                {
                    AddBehaviors(selectedEndpoint.BehaviorConfiguration, 
                        serviceEndpoint, serviceModeGroup);
                }
 
                serviceEndpoint.Name = selectedEndpoint.Contract;
            }
 
            return serviceEndpoint;
        }
 
        #region private
 
        /// <summary>
        /// Configures the binding for the selected endpoint.
        /// </summary>
        /// <param name="bindingName"></param>
        /// <param name="group"></param>
        /// <returns></returns>
        private Binding CreateBinding(string bindingName, ServiceModelSectionGroup group)
        {
            BindingCollectionElement bindingElementCollection = group.Bindings[bindingName];
            if(bindingElementCollection.ConfiguredBindings.Count > 0)
            {
                IBindingConfigurationElement be = bindingElementCollection.ConfiguredBindings[0];
 
                Binding binding = GetBinding(be);
                if(be != null)
                {
                    be.ApplyConfiguration(binding);
                }
 
                return binding;
            }
 
            return null;
        }
 
        /// <summary>
        /// Adds the configured behavior to the selected endpoint
        /// </summary>
        /// <param name="behaviorConfiguration"></param>
        /// <param name="serviceEndpoint"></param>
        /// <param name="group"></param>
        private void AddBehaviors(string behaviorConfiguration, ServiceEndpoint serviceEndpoint,
            ServiceModelSectionGroup group)
        {
            EndpointBehaviorElement behaviorElement = 
                group.Behaviors.EndpointBehaviors[behaviorConfiguration];
            for(int i = 0; i < behaviorElement.Count; i++)
            {
                BehaviorExtensionElement behaviorExtension = behaviorElement[i];
                object extension = behaviorExtension.GetType().InvokeMember("CreateBehavior",
                BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
                null, behaviorExtension, null);
                if(extension != null)
                {
                    serviceEndpoint.Behaviors.Add((IEndpointBehavior)extension);
                }
            }
        }
 
        /// <summary>
        /// Gets the endpoint identity from the configuration file
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        private EndpointIdentity GetIdentity(IdentityElement element)
        {
            EndpointIdentity identity = null;
            PropertyInformationCollection properties = element.ElementInformation.Properties;
            if(properties["userPrincipalName"].ValueOrigin != PropertyValueOrigin.Default)
            {
                return EndpointIdentity.CreateUpnIdentity(element.UserPrincipalName.Value);
            }
            if(properties["servicePrincipalName"].ValueOrigin != PropertyValueOrigin.Default)
            {
                return EndpointIdentity.CreateSpnIdentity(element.ServicePrincipalName.Value);
            }
            if(properties["dns"].ValueOrigin != PropertyValueOrigin.Default)
            {
                return EndpointIdentity.CreateDnsIdentity(element.Dns.Value);
            }
            if(properties["rsa"].ValueOrigin != PropertyValueOrigin.Default)
            {
                return EndpointIdentity.CreateRsaIdentity(element.Rsa.Value);
            }
            if(properties["certificate"].ValueOrigin != PropertyValueOrigin.Default)
            {
                X509Certificate2Collection supportingCertificates = new X509Certificate2Collection();
                supportingCertificates.Import(Convert.FromBase64String(element.Certificate.EncodedValue));
                if(supportingCertificates.Count == 0)
                {
                    throw new InvalidOperationException("UnableToLoadCertificateIdentity");
                }
                X509Certificate2 primaryCertificate = supportingCertificates[0];
                supportingCertificates.RemoveAt(0);
                return EndpointIdentity.CreateX509CertificateIdentity(primaryCertificate, 
                    supportingCertificates);
            }
 
            return identity;
        }
 
        /// <summary>
        /// Helper method to create the right binding depending on the configuration element
        /// </summary>
        /// <param name="configurationElement"></param>
        /// <returns></returns>
        private Binding GetBinding(IBindingConfigurationElement configurationElement)
        {
            if(configurationElement is CustomBindingElement)
                return new CustomBinding();
            else if(configurationElement is BasicHttpBindingElement)
                return new BasicHttpBinding();
            else if(configurationElement is NetMsmqBindingElement)
                return new NetMsmqBinding();
            else if(configurationElement is NetNamedPipeBindingElement)
                return new NetNamedPipeBinding();
            else if(configurationElement is NetPeerTcpBindingElement)
                return new NetPeerTcpBinding();
            else if(configurationElement is NetTcpBindingElement)
                return new NetTcpBinding();
            else if(configurationElement is WSDualHttpBindingElement)
                return new WSDualHttpBinding();
            else if(configurationElement is WSHttpBindingElement)
                return new WSHttpBinding();
            else if(configurationElement is WSFederationHttpBindingElement)
                return new WSFederationHttpBinding();
 
            return null;
        }
    }
"No files were found to look in"

Today I was really frustrated trying to use the search on Visual Studio, I was searching for something I was sure it was somewhere in the code, but Visual Studio didn't find anything. I searched again and again thinking that maybe I checked some 'don't show any results" checkbox, I even started to believe that that piece of code I was looking for didn't exist, even though I saw it a thousand times, fortunately I decided to read what was written in the search results window:

 

Then I consulted Dr Google who lead me to many forums and blogs and found that the solution to this is to press Ctrl+C and Ctrl+Pause, and when I tried this it worked like charmed.

Still I'm curious to know:

  • What the hell does Ctrl+C and Ctrl+Pause do?
  • Since there is no official information about this well-known old bug, how did the first person discover such a combination?
  • If some developer at microsoft created an event for this combination to solve the problem, why didn't he do what solves the problem right away when the problem arrives?
Posted: Aug 26 2009, 06:54 PM by pablito900 | with 4 comment(s) |
Filed under: ,
Attach to process for lazies

Attaching to a process is something that developers use very often to debug, but what happens when the process (A) you want to debug is launched from another process (B) and you want to debug is the initialization of process A,  Supposing that process A can only run within the context of process B in that case the only way to start process A is to do so from process so classic F5 won't help us to debug the initialization of A.

I had this scenario at work so at first I "trained" myself to do a very fast Alt+Ctrl+P select the process and press Attach button, of course this training didn't help much, after googling a little I found this macro

Sub AttachProcess()
        Dim process As EnvDTE.Process
 
        If Not (DTE.Debugger.DebuggedProcesses Is Nothing) Then
            For Each process In DTE.Debugger.DebuggedProcesses
                If (process.Name.IndexOf("aspnet_wp.exe") <> -1) Then
                    Exit Sub
                End If
            Next
        End If
 
        For Each process In DTE.Debugger.LocalProcesses
            If (process.Name.IndexOf("myprocess.exe") <> -1) Then
                process.Attach()
                Exit Sub
            End If
        Next
    End Sub 


but still it would have to wait for the process to start and then attach to it, so finally I created this Visual Studio add-in, which have this option.
 
The main idea is to wait for the process to start and then attach to it:
 
 
   1:private AttachResult PessimisticAttach(AttachType attachType)
   2:{
   3:     AttachResult res = Attach(attachType);
   4:     DateTime timeout = DateTime.Now.AddSeconds(WaitTimeout);
   5:   
   6:     while(res == AttachResult.NotRunning && timeout > DateTime.Now)
   7:     {
   8:           res = Attach(attachType);
   9:           System.Threading.Thread.Sleep(100);
  10:     }
  11:     return res;
  12:}
  13:   
  14:private AttachResult Attach(AttachType attachType)
  15:{
  16:     string engine =attachTypesMap[attachType];
  17:     if(IsBeingDebugged())
  18:     {
  19:          return AttachResult.BeingDebugged;
  20:     }
  21:   
  22:     Debugger2 dbg = dte.Debugger as Debugger2;
  23:     Transport trans = dbg.Transports.Item("Default");
  24:     Engine eng;
  25:   
  26:     eng = trans.Engines.Item(engine);
  27:   
  28:     EnvDTE80.Process2 proc = null;
  29:   
  30:     try
  31:     {
  32:          proc = dbg.GetProcesses(trans, "").Item(processName) 
  33:              as EnvDTE80.Process2;
  34:     }
  35:     catch(Exception ex)
  36:     {
  37:          if(ex.Message.Contains("Invalid index."))
  38:          {
  39:              return AttachResult.NotRunning;
  40:          }
  41:     }
  42:   
  43:     proc.Attach2(eng);
  44:     return AttachResult.Attached;
  45:}
 
I wrote the add-in using Declarative Visual Studio addin buttons with icons which saved me a lot of time and pain.
 

You can download add in source here

More Posts