Wednesday, February 28, 2018

C#, EPiServer - Find First Parent Page Implementing a Interface



   

 public static T FindFirstParentImplementingInterface<T>(this PageData rootPage, bool includeRootPage = true)
      {
          var page = rootPage;
          if (!includeRootPage)
          {
              page = page.GetParentPage();
          }
          while (page != null)
          {
              if (page.ImplementsInterface(typeof(T)))
              {
                  return (T)(object)page;
              }
              page = page.GetParentPage();
          }

          return default(T);
      }

public static PageData FindFirstParentImplementingInterface(this PageData rootPage, Type interfaceType, bool includeRootPage = true)
      {
          var page = rootPage;
          if (!includeRootPage)
          {
              page = page.GetParentPage();
          }
          while (page != null)
          {
              if (page.ImplementsInterface(interfaceType))
              {
                  return page;
              }
              page = page.GetParentPage();
          }

          return null;
      }

//Support Method
public static bool ImplementsInterface(this PageData pageData, Type interfaceType)
      {
          var type = pageData.GetOriginalType();
          return type != null && interfaceType.IsAssignableFrom(type);
      }

Friday, February 16, 2018

SOAP - HTTP with C# Basic Realm

private RrewardServiceClient GetLocalM2MSoapClient()
    {

        string endpointFromConfig = ConfigurationManager.AppSettings["RewardM2MEndpointAdress"];
        string username = ConfigurationManager.AppSettings["RewardServiceClientCredentialsUser"];
        string password = ConfigurationManager.AppSettings["RewardServiceClientCredentialsPassword"];

        BasicHttpBinding basicAuthBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);

        basicAuthBinding.MaxReceivedMessageSize = Convert.ToInt32(200000000);
        basicAuthBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
        basicAuthBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
        basicAuthBinding.Security.Transport.Realm = "AXIS";
        basicAuthBinding.Security.Transport.ExtendedProtectionPolicy = new ExtendedProtectionPolicy(PolicyEnforcement.Never);


        EndpointAddress basicAuthEndpoint = new EndpointAddress(endpointFromConfig);

        RrewardServiceClient sapClient = new RrewardServiceClient(basicAuthBinding, basicAuthEndpoint);

        try
        {

            ClientCredentials clientCredentials = new ClientCredentials();
            clientCredentials.UserName.UserName = username;
            clientCredentials.UserName.Password = password;

            sapClient.ChannelFactory.Endpoint.Behaviors.RemoveAt(1);
            sapClient.ChannelFactory.Endpoint.Behaviors.Add(clientCredentials);


            if (sapClient.State == CommunicationState.Faulted)
            {
                sapClient.Abort();
                return null;
            }
            return sapClient;
        }
        catch (Exception ex)
        {
            _log.Error("SOA Client error:" + ex);
            sapClient.Abort();
            return null;
        }

    }

Tuesday, February 6, 2018

Thursday, February 1, 2018

Visual Studio hangs - Touch Screen bug Cause and fix

This is a known issue that will be addressed in an upcoming Windows Update.
Until the fix is available, to work around this issue, turn off all pen and touch support for Visual Studio. To do this,
  1. Find the devenv.exe.config file. It is located in the folder <VS Install Location>\Common7\IDE.
  2. Copy devenv.exe.config to a non-protected folder, for example your Documents folder, and then create a backup copy of devenv.exe.config.
  3. Open devenv.exe.config, and modify the <AppContextSwitchOverrides> element to add “Switch.System.Windows.Input.Stylus.DisableStylusAndTouchSupport=true”
    • For example
      • <AppContextSwitchOverrides value="Switch.System.Threading.ThrowExceptionIfDisposedCancellationTokenSource=false;Switch.System.Windows.Forms.DoNotSupportSelectAllShortcutInMultilineTextBox=false;Switch.System.Windows.Input.Stylus.DisableStylusAndTouchSupport=true" />
  4. Copy the edited devenv.exe.config into <VS Install Location>\Common7\IDE.
Once the update to .NET Framework 4.7 is available, remove this property from the “value” string to restore pen and touch input to Visual Studio.
NOTE: because this file is replaced with each update to Visual Studio, you must re-apply this workaround after updating Visual Studio until the update to .NET Framework 4.7 is available.