title: How to dynamically call a net.tcp Web Service
body: <p>I am trying to call a WCF from my C# desktop app but i get an error. This is my code:</p>
  
  <pre><code>//Client Code
  System.ServiceModel.EndpointAddress addressSync = new System.ServiceModel.EndpointAddress("net.tcp://an ip address/Sync2.svc");
  Shared.FactorySync = new System.ServiceModel.ChannelFactory&lt;LiteEdition.wsSyncFastest.ISync2Channel&gt;("NetTcpBinding_ISync2", addressSync);
  Shared.UpLoadSync = Shared.FactorySync.CreateChannel();
  </code></pre>
  
  <p><strong>[QUESTION. If I dynamically create a client call to a WCF can I assume i do NOT need to include anything in th app.config file about the bindings?]</strong></p>
  
  <pre><code>//error message on this line:
  Shared.UpLoadSync.UploadMotionDynamic2(new byte[1]{0}, 0, 0);
  
  //IS:
  The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:00:59.9349963'
  </code></pre>
  
  <p>.</p>
  
  <pre><code>//My Service:
  public void UploadMotionDynamic2(byte[] jpegData, int status, int framePart)
  {
      DAL dal = new DAL();
      try
      {
          if (jpegData != null)
      {
              LiveView2.SetNewFrame(status, framePart, jpegData);            
      }
      }
      catch (Exception ex)
      {
          email.SendError("Sync.UploadMotionDynamic:" + ex.ToString(), "");
      }
  }
  
  //In my Web.Config
   &lt;system.serviceModel&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="NetTCPBehaviour"&gt;
            &lt;serviceTimeouts transactionTimeout="0.00:00:30" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
            &lt;dataContractSerializer maxItemsInObjectGraph="65536" /&gt;
            &lt;serviceMetadata httpGetEnabled="true" /&gt;
          &lt;/behavior&gt;
          &lt;behavior name=""&gt;
            &lt;serviceMetadata httpGetEnabled="true" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;services&gt;
        &lt;service name="testme" behaviorConfiguration="NetTCPBehaviour"&gt;
          &lt;endpoint  address="Sync2.svc" binding="netTcpBinding" contract="ISync" name="wsMotionUploader" bindingConfiguration="NetTCPBindingEndPoint"&gt;
            &lt;!--&lt;security mode="None"&gt;&lt;/security&gt;--&gt;
          &lt;/endpoint&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;bindings&gt;
        &lt;netTcpBinding&gt;
          &lt;binding  name="NetTCPBindingEndPoint" receiveTimeout="00:15:00" sendTimeout="00:15:00" transferMode="Streamed" closeTimeout="00:02:00" openTimeout="00:02:00" 
              maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"&gt;
            &lt;readerQuotas maxArrayLength="32768" /&gt;
            &lt;security mode="None"&gt;
              &lt;transport clientCredentialType="None" protectionLevel="None" /&gt;
              &lt;message clientCredentialType="None" /&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
        &lt;/netTcpBinding&gt;
      &lt;/bindings&gt;
      &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt;
    &lt;/system.serviceModel&gt; 
  </code></pre>
  
  <p>Amended Code:</p>
  
  <pre><code>            System.ServiceModel.NetTcpBinding binding = new System.ServiceModel.NetTcpBinding();
              System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress("http:ip/Sync2");
  
              System.ServiceModel.ChannelFactory&lt;System.ServiceModel.Channels.IRequestChannel&gt; factory =
                  new System.ServiceModel.ChannelFactory&lt;System.ServiceModel.Channels.IRequestChannel&gt;(binding, address);
  
              var channel = factory.CreateChannel();
              channel.Open();
  </code></pre>
  
  <p>UPDATED TO THIS:</p>
  
  <pre><code>            System.ServiceModel.NetTcpBinding binding = new System.ServiceModel.NetTcpBinding();                
              System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress("net.tcp://ip/Sync2");
              System.ServiceModel.ChannelFactory&lt;WindowsFormsApplication2.ws.ISync2Channel&gt; factory =
                  new System.ServiceModel.ChannelFactory&lt;WindowsFormsApplication2.ws.ISync2Channel&gt;(binding, address);
              factory.Endpoint.Contract.SessionMode = System.ServiceModel.SessionMode.Allowed;
              WindowsFormsApplication2.ws.ISync2Channel channel = factory.CreateChannel();
              channel.Open();
  </code></pre>
  
  <p>But get this error 'Object reference not set to an instance of an object.'</p>
  
  <p>on the .Open() method.</p>
  
  <p>Also, the 'WindowsFormsApplication2.ws.ISync2Channel' is defined because I had already added a reference to the Service.  If I had not what would I have
  used instead?</p>
  
  <p>///******************new settings:</p>
  
  <pre><code>Client code:
  
  ws.Sync2Client wcf = new ws.Sync2Client("NetTcpBinding_ISync2");
  wcf.UploadMotionDynamic2(new byte[1]{1},0,0);
  
  Client App.Config:
      &lt;system.serviceModel&gt;
          &lt;bindings&gt;
              &lt;basicHttpBinding&gt;
                  &lt;binding name="BasicHttpBinding_ISync2" /&gt;
              &lt;/basicHttpBinding&gt;
              &lt;netTcpBinding&gt;
                  &lt;binding name="NetTcpBinding_ISync2" /&gt;
              &lt;/netTcpBinding&gt;
          &lt;/bindings&gt;
          &lt;client&gt;
              &lt;endpoint address="http://ip/Sync2.svc"
                  binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISync2"
                  contract="ws.ISync2" name="BasicHttpBinding_ISync2" /&gt;
              &lt;endpoint address="net.tcp://ip/Sync2.svc" binding="netTcpBinding"
                  bindingConfiguration="NetTcpBinding_ISync2" contract="ws.ISync2"
                  name="NetTcpBinding_ISync2"&gt;
                  &lt;identity&gt;
                      &lt;servicePrincipalName value="host/ip" /&gt;
                  &lt;/identity&gt;
              &lt;/endpoint&gt;
          &lt;/client&gt;
      &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>Server Web.Config</p>
  
  <p>
      
        
          
            
            
          
          <br>
            <br>
            
            
            -->
          
          
          
            
            
          
        
      
      
        
          
        
        
          
        
      
      
        
          
            
          
        
        
          
            
            
              
              
            
          
        
      
      
    </p>
  
  <p>The error I get is on this line:</p>
  
  <pre><code>wcf.UploadMotionDynamic2(new byte[1]{1},0,0);
  </code></pre>
  
  <p>The error is:
  Object reference not set to an instance of an object.
  the stack is:</p>
  
  <p>Server stack trace: 
     at System.ServiceModel.Channels.SocketConnectionInitiator.Connect(Uri uri, TimeSpan timeout)
     at System.ServiceModel.Channels.BufferedConnectionInitiator.Connect(Uri uri, TimeSpan timeout)
     at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout)
     at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout)
     at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
     at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
     at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
     at System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel channel, TimeSpan timeout)
     at System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade)
     at System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan timeout)
     at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
     at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
     at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
     at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)</p>
  
  <p>Exception rethrown at [0]: 
     at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
     at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData, Int32 type)
     at WindowsFormsApplication2.ws.ISync2.UploadMotionDynamic2(Byte[] jpegData, Int32 status, Int32 framePart)
     at WindowsFormsApplication2.ws.Sync2Client.UploadMotionDynamic2(Byte[] jpegData, Int32 status, Int32 framePart) in m:\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Service References\ws\Reference.cs:line 81
     at WindowsFormsApplication2.Form1.button1_Click(Object sender, EventArgs e) in m:\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs:line 581</p>
  
  <p>This all works when I use http binding though...</p>
  
  <p>The port 780 is open on my server.
  the Net.Tcp.Listener Adaptor is running
  Enabled Protocols in IIS has net.tcp</p>
  
  <p>Still no joy</p>