title: Execute WPF from a Console App in C# solution
body: <p>I am trying to execute a WPF Class library from a console app built in the same solution in VS2012. I have established I need to do this after my first encounter with this error</p>
  
  <blockquote>
    <p>A project with an output type of class library cannot be started directly.
    In order to debug this project, add an executable project to this solution which references the library project. Set the executable project as the startup project. </p>
  </blockquote>
  
  <p>This is when I added another console project and added the initial WPF as a reference for the console app. 
  I have also right-click on the solution>Properties>Startup project> I have selected the Console project and checked "Single startup project"</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Interfaces.Connection;
  
  namespace API
  {
  class Program
  {
      static void Main(string[] args)
      {
          Console.WriteLine("Do I need to do something in here to call the initializecomponent() method from the WPF project???");
  
          Console.ReadLine();            
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>As the code writeLine says, do I need to do something in the Main() method in program.Cs of the console application to call initializecomponent or something within that WPF??
  Because when I run the solution when Main() is fully empty, it seems to just run the console app. It opens and closes the command line really quick. </p>
  
  <p>Here are all the code for the WPF project if you need. </p>
  
  <p>Client.cs</p>
  
  <pre><code>using System;
  using System.ServiceModel;
  using System.ServiceModel.Channels;
  using Interfaces.Connection.ApplicationService;
  
  namespace Interfaces.Connection
  {
  /// &lt;summary&gt;
  /// This class is used to operate on the  web service.
  /// &lt;/summary&gt;
  public class Client
  {
      public ApplicationServiceClient Client { get; private set; }
      public string Username { get; set; }
      public string Context { get; set; }
      public string Password { get; set; }
  
      /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="Client"/&gt; class.
      /// &lt;/summary&gt;
      /// &lt;param name="client"&gt;The client.&lt;/param&gt;
      public Client(ApplicationServiceClient client)
      {
          Client = client;
      }
  
      /// &lt;summary&gt;
      /// Pings the web service.
      /// &lt;/summary&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public bool Ping()
      {
          using (OperationContextScope scope = new OperationContextScope(Client.InnerChannel))
          {
              AddCredentials();
              PingResponse pingResponse = (PingResponse)Client.Run(new PingRequest());
              return pingResponse != null;
          }
      }
  
      /// &lt;summary&gt;
      /// Creates an instance of a given entity.
      /// &lt;/summary&gt;
      /// &lt;param name="entityName"&gt;Name of the entity.&lt;/param&gt;
      /// &lt;param name="pivotMember"&gt;The pivot member.&lt;/param&gt;
      /// &lt;param name="parent"&gt;The parent.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public Instance Create(string entityName, Guid? pivotMember, ParentReference parent)
      {
          using (OperationContextScope scope = new OperationContextScope(Client.InnerChannel))
          {
              AddCredentials();
              return Client.Create(entityName, pivotMember, parent);
          }
      }
  
      /// &lt;summary&gt;
      /// Saves the specified instance.
      /// &lt;/summary&gt;
      /// &lt;param name="instance"&gt;The instance.&lt;/param&gt;
      public void Save(Instance instance)
      {
          using (OperationContextScope scope = new OperationContextScope(Client.InnerChannel))
          {
              AddCredentials();
              Client.Save(instance);
          }
      }
  
      /// &lt;summary&gt;
      /// Deletes the instance with the specified primary key.
      /// &lt;/summary&gt;
      /// &lt;param name="entityName"&gt;Name of the entity.&lt;/param&gt;
      /// &lt;param name="id"&gt;The id.&lt;/param&gt;
      public void Delete(string entityName, Guid id)
      {
          using (OperationContextScope scope = new OperationContextScope(Client.InnerChannel))
          {
              AddCredentials();
              Client.Delete(entityName, id);
          }
      }
  
      /// &lt;summary&gt;
      /// Selects an instance by its primary key.
      /// &lt;/summary&gt;
      /// &lt;param name="entityName"&gt;Name of the entity.&lt;/param&gt;
      /// &lt;param name="id"&gt;The id.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public Instance SelectById(string entityName, Guid id)
      {
          using (OperationContextScope scope = new OperationContextScope(Client.InnerChannel))
          {
              AddCredentials();
              return Client.SelectById(entityName, id);
          }
      }
  
      /// &lt;summary&gt;
      /// Executes the given QueryBase and returns the result.
      /// &lt;/summary&gt;
      /// &lt;param name="query"&gt;The query.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public QueryResult SelectByQuery(QueryBase query)
      {
          using (OperationContextScope scope = new OperationContextScope(Client.InnerChannel))
          {
              AddCredentials();
              return Client.SelectByQuery(query);
          }
      }
  
      private void AddCredentials()
      {
          const string contextNamespace = "http://.com/2011/servicecontracts/headers";
          const string contextName = "ContextualPerspective";
          const string userNameHeaderName = "UserName";
          const string passwordHeaderName = "Password";
  
          MessageHeader activeMember = MessageHeader.CreateHeader(contextName, contextNamespace, Context);
          OperationContext.Current.OutgoingMessageHeaders.Add(activeMember);
  
          MessageHeader userNameHeader = MessageHeader.CreateHeader(userNameHeaderName, contextNamespace, Username);
          OperationContext.Current.OutgoingMessageHeaders.Add(userNameHeader);
  
          MessageHeader userPasswordHeader = MessageHeader.CreateHeader(passwordHeaderName, contextNamespace, Password);
          OperationContext.Current.OutgoingMessageHeaders.Add(userPasswordHeader);
      }
  }
  }
  </code></pre>
  
  <p>MainWindowXAML(this is the window)</p>
  
  <pre><code>&lt;Window x:Class="Interfaces.Connection.ConnectionDialog"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="API Connector" Height="Auto" Width="525"&gt;
  &lt;Grid Background="#F0F0F0"&gt;
      &lt;Grid.Resources&gt;
          &lt;Style TargetType="TextBlock"&gt;
              &lt;Setter Property="VerticalAlignment" Value="Center"/&gt;
              &lt;Setter Property="Margin" Value="10,10,5,5"/&gt;
          &lt;/Style&gt;
          &lt;Style TargetType="Button"&gt;
              &lt;Setter Property="VerticalAlignment" Value="Center"/&gt;
              &lt;Setter Property="Height" Value="26"/&gt;
              &lt;Setter Property="Width" Value="100"/&gt;
          &lt;/Style&gt;
      &lt;/Grid.Resources&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width="Auto"/&gt;
          &lt;ColumnDefinition/&gt;
      &lt;/Grid.ColumnDefinitions&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="Auto"/&gt;
          &lt;RowDefinition Height="Auto"/&gt;
          &lt;RowDefinition Height="Auto"/&gt;
          &lt;RowDefinition Height="Auto"/&gt;
          &lt;RowDefinition Height="Auto"/&gt;
          &lt;RowDefinition Height="*"/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;TextBlock Text="Varasset Server Url" /&gt;
      &lt;TextBox x:Name="txtVarassetServer" Grid.Column="1" Margin="5,10,10,5" /&gt;
  
      &lt;TextBlock Grid.Row="1" Text="Username" /&gt;
      &lt;TextBox x:Name="txtUsername" Grid.Row="1" Grid.Column="1" Margin="5,5,10,5" /&gt;
  
      &lt;TextBlock Grid.Row="2" Text="Member Code" /&gt;
      &lt;TextBox x:Name="txtContext" Grid.Row="2" Grid.Column="1" Margin="5,5,10,5" /&gt;
  
      &lt;TextBlock Grid.Row="3" Text="Password" /&gt;
      &lt;PasswordBox x:Name="txtPassword" Grid.Row="3" Grid.Column="1" Margin="5,5,10,5" /&gt;
  
      &lt;Border Grid.Row="4" Grid.ColumnSpan="2" Margin="10,10,10,0" Height="1" BorderBrush="Gray" BorderThickness="0,1,0,0"/&gt;
  
      &lt;StackPanel Grid.Row="5" Grid.Column="1" Orientation="Horizontal" Margin="10"
                  HorizontalAlignment="Right" VerticalAlignment="Bottom"&gt;
          &lt;Button x:Name="btnOk"  Click="btnOk_Click"&gt;OK&lt;/Button&gt;
          &lt;Button x:Name="btnCancel" Margin="10,0,0,0" Click="btnCancel_Click"&gt;Cancel&lt;/Button&gt;
      &lt;/StackPanel&gt;
  
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>MainWindowxaml.cs(its CS)</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  using Microsoft.SqlServer.Dts.Runtime.Design;
  using Microsoft.SqlServer.Dts.Runtime;
  
  namespace Interfaces.Connection
  {
  /// &lt;summary&gt;
  /// Interaction logic for MainWindow.xaml
  /// &lt;/summary&gt;
  public partial class ConnectionDialog : Window
  {
      /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="ConnectionDialog"/&gt; class.
      /// &lt;/summary&gt;
      /// &lt;param name="context"&gt;The context.&lt;/param&gt;
      public ConnectionDialog(ConnectionManagerUI context)
      {
          InitializeComponent();
          DataContext = context;
  
          txtPassword.Password = context.Password;
          txtUsername.Text = context.Username;
          txtContext.Text = context.Context;
          txtServer.Text = context.Address;
      }
  
      private void btnOk_Click(object sender, RoutedEventArgs e)
      {
          DialogResult = true;
          ConnectionManagerUI manager = ((ConnectionManagerUI)DataContext);
          manager.Password = txtPassword.Password;
          manager.Username = txtUsername.Text;
          manager.Context = txtContext.Text;
          manager.Address = txtServer.Text;
          Close();
      }
  
      private void btnCancel_Click(object sender, RoutedEventArgs e)
      {
          Close();
      }
  }
  }
  </code></pre>
  
  <p>There are couple more files, but I do not think they are relevant at this point. </p>
  
  <p>I have tried calling initializeComponent from main like this as well.</p>
  
  <pre><code>Interfaces.Connection.ConnectionDialog app = new Interfaces.Connection.ConnectionDialog();
  app.InitializeComponent();
  </code></pre>
  
  <p>But it then gives error saying </p>
  
  <blockquote>
    <p>Interfaces.Connection.ConnectionDialog' does not contain a constructor that takes 0 arguments.</p>
  </blockquote>
  
  <p>And I am not sure what arguments it expects. </p>
  
  <pre><code> using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Microsoft.SqlServer.Dts.Runtime.Design;
  using Microsoft.SqlServer.Dts.Runtime;
  
  namespace Interfaces.Connection
  {
  /// &lt;summary&gt;
  /// This class extends the class Microsoft.SqlServer.Dts.Runtime.Design.IDtsConnectionManagerUI.
  /// &lt;/summary&gt;
  public class ConnectionManagerUI : IDtsConnectionManagerUI
  {
      private ConnectionManager _connectionManager;
      private IServiceProvider _serviceProvider;
  
      /// &lt;summary&gt;
      /// Gets or sets the username.
      /// &lt;/summary&gt;
      /// &lt;value&gt;
      /// The username.
      /// &lt;/value&gt;
      public string Username
      {
          get
          {
              var property = _connectionManager.Properties["Username"].GetValue(_connectionManager);
              return property == null ? string.Empty : property.ToString();
          }
          set { _connectionManager.Properties["Username"].SetValue(_connectionManager, value); }
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the context (member code).
      /// &lt;/summary&gt;
      /// &lt;value&gt;
      /// The context.
      /// &lt;/value&gt;
      public string Context
      {
          get
          {
              var property = _connectionManager.Properties["Context"].GetValue(_connectionManager);
              return property == null ? string.Empty : property.ToString();
          }
          set { _connectionManager.Properties["Context"].SetValue(_connectionManager, value); }
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the password.
      /// &lt;/summary&gt;
      /// &lt;value&gt;
      /// The password.
      /// &lt;/value&gt;
      public string Password
      {
          get 
          {
              var property = _connectionManager.Properties["Password"].GetValue(_connectionManager);
              return property == null ? string.Empty : property.ToString();
          }
          set { _connectionManager.Properties["Password"].SetValue(_connectionManager, value); }
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the address.
      /// &lt;/summary&gt;
      /// &lt;value&gt;
      /// The address.
      /// &lt;/value&gt;
      public string Address
      {
          get
          {
              var property = _connectionManager.Properties["Address"].GetValue(_connectionManager);
              return property == null ? string.Empty : property.ToString();
          }
          set { _connectionManager.Properties["Address"].SetValue(_connectionManager, value); }
      }
  
      /// &lt;summary&gt;
      /// Method not implemented.
      /// &lt;/summary&gt;
      /// &lt;param name="parentWindow"&gt;The IWin32Window interface of the designer hosting the task.&lt;/param&gt;
      public void Delete(System.Windows.Forms.IWin32Window parentWindow)
      {
          throw new NotImplementedException("Delete");
      }
  
      /// &lt;summary&gt;
      /// Edits an existing connection manager.
      /// &lt;/summary&gt;
      /// &lt;param name="parentWindow"&gt;The IWin32Window interface of the designer hosting the task.&lt;/param&gt;
      /// &lt;param name="connections"&gt;The connections available for editing.&lt;/param&gt;
      /// &lt;param name="connectionUIArg"&gt;A class that implements the &lt;see cref="T:Microsoft.SqlServer.Dts.Runtime.Design.ConnectionManagerUIArgs"/&gt;, such as the &lt;see cref="T:Microsoft.SqlServer.Dts.Runtime.Design.FileConnectionManagerUIArgs"/&gt; or &lt;see cref="T:Microsoft.SqlServer.Dts.Runtime.Design.MultiFileConnectionManagerUIArgs"/&gt;.&lt;/param&gt;
      /// &lt;returns&gt;
      /// true if the connection manager was modified.
      /// &lt;/returns&gt;
      public bool Edit(System.Windows.Forms.IWin32Window parentWindow, Microsoft.SqlServer.Dts.Runtime.Connections connections, ConnectionManagerUIArgs connectionUIArg)
      {
          return new ConnectionDialog(this).ShowDialog().GetValueOrDefault();
      }
  
      /// &lt;summary&gt;
      /// Initializes the connection manager user interface. This method is called when you need to create connections of a specific type.
      /// &lt;/summary&gt;
      /// &lt;param name="connectionManager"&gt;The connection manager to initialize.&lt;/param&gt;
      /// &lt;param name="serviceProvider"&gt;The object used to get registered services. Defines a mechanism for retrieving services offered by the designer for such activities as monitoring changes to components.&lt;/param&gt;
      public void Initialize(Microsoft.SqlServer.Dts.Runtime.ConnectionManager connectionManager, IServiceProvider serviceProvider)
      {
          _connectionManager = connectionManager;
          _serviceProvider = serviceProvider;
      }
  
      /// &lt;summary&gt;
      /// Provides notification to tasks of newly created connection managers. This method is called after a new connection manager is added to the package.
      /// &lt;/summary&gt;
      /// &lt;param name="parentWindow"&gt;The IWin32Window interface of the designer hosting the task.&lt;/param&gt;
      /// &lt;param name="connections"&gt;The connections collection to add the new connection to, or the connections to show in a drop-down or other control in the user interface.&lt;/param&gt;
      /// &lt;param name="connectionUIArg"&gt;A class that implements the &lt;see cref="T:Microsoft.SqlServer.Dts.Runtime.Design.ConnectionManagerUIArgs"/&gt;, such as the &lt;see cref="T:Microsoft.SqlServer.Dts.Runtime.Design.FileConnectionManagerUIArgs"/&gt; or &lt;see cref="T:Microsoft.SqlServer.Dts.Runtime.Design.MultiFileConnectionManagerUIArgs"/&gt;.&lt;/param&gt;
      /// &lt;returns&gt;
      /// true if a new connection was created.
      /// &lt;/returns&gt;
      public bool New(System.Windows.Forms.IWin32Window parentWindow, Microsoft.SqlServer.Dts.Runtime.Connections connections, ConnectionManagerUIArgs connectionUIArg)
      {
          return new ConnectionDialog(this).ShowDialog().GetValueOrDefault();
      }
  }
  }
  </code></pre>
  
  <p>Thanks in advance!</p>
  
  <p><em>EDIT</em>
  I have changed program.cs Main() method to:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          ConnectionManagerUI connectionManagerUI = new ConnectionManagerUI;
          ConnectionDialog dialog = new ConnectionDialog(ConnectionManagerUI);          
      }
  }
  </code></pre>
  
  <p>It runs now, but it gives error </p>
  
  <blockquote>
    <p>An unhandled exception of type "System.InvalidOperationException' occurred in PresentationCore.dll
    Additional Information: The calling thread must be STA, because many UI components require this.</p>
  </blockquote>
  
  <p>If I need to create a new post or I can research first before posting. But Tim was able to solve this post's issue. </p>
  
  <p>END of <em>EDIT</em></p>