title: Connecting to GP Web service throws exception
body: <p>I'm trying connect to GP webservice to create sales order from C# application. But When I try to connect it is throwing exception as below.</p>
  
  <pre><code>The application encountered an unhandled system exception.  Contact your system administrator for details.
  </code></pre>
  
  <p>Below is my app.config file </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
    &lt;system.serviceModel&gt;
      &lt;bindings&gt;
        &lt;wsHttpBinding&gt;
          &lt;binding name="GPWebService" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00"
          sendTimeout="00:01:00" bypassProxyOnLocal="false"
          transactionFlow="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
          messageEncoding="Text" textEncoding="utf-8"
          useDefaultWebProxy="true" allowCookies="false"&gt;
            &lt;readerQuotas maxDepth="32" maxStringContentLength="8192"
            maxArrayLength="16384" maxBytesPerRead="4096"
            maxNameTableCharCount="2147483647" /&gt;
            &lt;reliableSession ordered="true" inactivityTimeout="00:10:00"
            enabled="false" /&gt;
            &lt;security mode="Message"&gt;
              &lt;transport clientCredentialType="Windows"
              proxyCredentialType="None" realm="" /&gt;
              &lt;message clientCredentialType="Windows"
              negotiateServiceCredential="true" algorithmSuite="Default"
              establishSecurityContext="true" /&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
        &lt;/wsHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;client&gt;
        &lt;endpoint address="http://WSGP:48620/Dynamics/GPService/GPService"
        binding="wsHttpBinding" bindingConfiguration="GPWebService"
        contract="GPWebService.DynamicsGP" name="GPWebService"&gt;
          &lt;identity&gt;
            &lt;userPrincipalName value="WSGP\Administrator" /&gt;
          &lt;/identity&gt;
        &lt;/endpoint&gt;
      &lt;/client&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>C# Code consuming the service</p>
  
  <pre><code>CompanyKey companyKey;
              Context context;
              SalesOrder salesOrder;
              SalesDocumentTypeKey salesOrderType;
              CustomerKey customerKey;
              BatchKey batchKey;
              SalesOrderLine salesOrderLine;
              ItemKey orderedItem;
              Quantity orderedAmount;
              Policy salesOrderCreatePolicy;
              // Create an instance of the service
              DynamicsGPClient wsDynamicsGP = new DynamicsGPClient();
              // Create a context with which to call the service
              wsDynamicsGP.ClientCredentials.Windows.ClientCredential.UserName = "admin";
              wsDynamicsGP.ClientCredentials.Windows.ClientCredential.Password = "pass";
              wsDynamicsGP.ClientCredentials.Windows.ClientCredential.Domain = "WSGP";
              System.ServiceModel.WSHttpBinding binding;
              binding = new System.ServiceModel.WSHttpBinding(System.ServiceModel.SecurityMode.None);
              //wsDynamicsGP.ClientCredentials.Windows.ClientCredential.Domain = "wsgp";
              context = new Context();
              // Specify which company to use (sample company)
              companyKey = new CompanyKey();
              companyKey.Id = (-1);
              // Set up the context object
              context.OrganizationKey = (OrganizationKey)companyKey;
              // Create a sales order object
              salesOrder = new SalesOrder();
              // Create a sales document type key for the sales order
              salesOrderType = new SalesDocumentTypeKey();
              salesOrderType.Type = SalesDocumentType.Order;
              // Populate the document type key of the sales order object
              salesOrder.DocumentTypeKey = salesOrderType;
              // Create a customer key
              customerKey = new CustomerKey();
              customerKey.Id = "121001";
              // Set the customer key property of the sales order object
              salesOrder.CustomerKey = customerKey;
              // Create a batch key
              batchKey = new BatchKey();
              batchKey.Id = "RMS";
              // Set the batch key property of the sales order object
              salesOrder.BatchKey = batchKey;
              // Create a sales order line to specify the ordered item
              salesOrderLine = new SalesOrderLine();
              // Create an item key
              orderedItem = new ItemKey();
              orderedItem.Id = "8011172009618";
              // Set the item key property of the sales order line object
              salesOrderLine.ItemKey = orderedItem;
              // Create a sales order quantity object
              orderedAmount = new Quantity();
              orderedAmount.Value = 4;
              // Set the quantity of the sales order line object
              salesOrderLine.Quantity = orderedAmount;
              // Create an array of sales order lines
              // Initialize the array with sales order line object
              SalesOrderLine[] orders = { salesOrderLine };
              // Add the sales order line array to the sales order
              salesOrder.Lines = orders;
              // Get the create policy for the sales order object
              salesOrderCreatePolicy = wsDynamicsGP.GetPolicyByOperation("CreateSalesOrder", context);
              // Create the sales order
              wsDynamicsGP.CreateSalesOrder(salesOrder, context, salesOrderCreatePolicy);
              // Close the service
              if (wsDynamicsGP.State != CommunicationState.Faulted)
              {
                  wsDynamicsGP.Close();
              }
  </code></pre>
  
  <p>GP Config file is below</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
      &lt;section name="cachingConfiguration" type="Microsoft.Dynamics.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Dynamics.EnterpriseLibrary.Caching, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/&gt;
    &lt;/configSections&gt;
    &lt;cachingConfiguration configSource="WSCachingConfiguration.config"/&gt;
    &lt;appSettings file="WSServiceAppSettings.config"&gt;
      &lt;add key="SecurityRuntimeAuditQueuing" value="0"/&gt;
      &lt;add key="SecurityRuntimeAuditingIsActive" value="false"/&gt;
    &lt;/appSettings&gt;
    &lt;system.serviceModel&gt;
      &lt;bindings configSource="WSBindings.config"/&gt;
      &lt;services&gt;
        &lt;service name="Microsoft.Dynamics.GP.WebServices.Contract.DynamicsGP" behaviorConfiguration="GPWCFStaticMetadataBehavior"&gt;
          &lt;endpoint address="Mex" name="http" binding="customBinding" bindingConfiguration="CustomBinding" contract="IMetadataExchange"/&gt;
          &lt;endpoint address="GPService" name="GPWebService" binding="wsHttpBinding" bindingConfiguration="WSHttpBindingTarget" contract="Microsoft.Dynamics.GP.WebServices.Contract.IGPServices"/&gt;
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress="http://WSGP:48620/Dynamics/GPService"/&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="GPWCFStaticMetadataBehavior"&gt;
            &lt;serviceThrottling maxConcurrentSessions="1024"/&gt;
            &lt;serviceMetadata httpGetEnabled="true" externalMetadataLocation="http://WSGP:48620/Metadata/WCF/Full/schemas.microsoft.com.dynamics.gp.2010.01.wsdl"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
            &lt;dataContractSerializer maxItemsInObjectGraph="2147483647"/&gt;
            &lt;serviceCredentials&gt;
              &lt;userNameAuthentication cacheLogonTokens="true"/&gt;
            &lt;/serviceCredentials&gt;
          &lt;/behavior&gt;
          &lt;behavior name="GPDynamicMetadataBehavior"&gt;
            &lt;serviceThrottling maxConcurrentSessions="1024"/&gt;
            &lt;serviceMetadata httpGetEnabled="true"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
            &lt;dataContractSerializer maxItemsInObjectGraph="2147483647"/&gt;
            &lt;serviceCredentials&gt;
              &lt;userNameAuthentication cacheLogonTokens="true"/&gt;
            &lt;/serviceCredentials&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;client/&gt;
    &lt;/system.serviceModel&gt;
    &lt;system.diagnostics&gt;
      &lt;switches&gt;
        &lt;add name="ApplicationTraceSwitch" value="0"/&gt;
      &lt;/switches&gt;
      &lt;trace autoflush="true" indentsize="4"&gt;
        &lt;listeners&gt;
          &lt;add name="dynamicsListener"/&gt;
        &lt;/listeners&gt;
      &lt;/trace&gt;
      &lt;sources&gt;
        &lt;source name="System.ServiceModel" switchValue="" propagateActivity="true"&gt;
          &lt;!--Values can be:http://msdn.microsoft.com/en-us/library/ms733025.aspx --&gt;
          &lt;listeners&gt;
            &lt;add name="wcfListener"/&gt;
          &lt;/listeners&gt;
        &lt;/source&gt;
        &lt;source name="System.IO.Log"&gt;
          &lt;listeners&gt;
            &lt;add name="wcfListener"/&gt;
          &lt;/listeners&gt;
        &lt;/source&gt;
        &lt;source name="System.Runtime.Serialization"&gt;
          &lt;listeners&gt;
            &lt;add name="wcfListener"/&gt;
          &lt;/listeners&gt;
        &lt;/source&gt;
        &lt;source name="System.IdentityModel"&gt;
          &lt;listeners&gt;
            &lt;add name="wcfListener"/&gt;
          &lt;/listeners&gt;
        &lt;/source&gt;
      &lt;/sources&gt;
      &lt;sharedListeners&gt;
        &lt;add name="wcfListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\Program Files\Microsoft Dynamics\GPWebServices\Logs\Tracing\GPWebServices.svclog"/&gt;
        &lt;add name="dynamicsListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Program Files\Microsoft Dynamics\GPWebServices\Logs\Tracing\GPWebServices.log"/&gt;
      &lt;/sharedListeners&gt;
    &lt;/system.diagnostics&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>What I'm doing wrong here? Am I missing something in the configuration file ?</p>
  
  <p>I should say, I'm novice to Web services development.</p>