Thursday, October 25, 2012

Connecting to Tridion Core Service

Today marks the first day of me learning about the Tridion Core Service! First let's step through how to connect (remotely from the server as a client)

  • Open Visual Studio and create a new Windows Console project
  • Add Tridion.ContentManager.CoreService.Client.dll as a reference in the solution
  • Add Systems.ServiceModel and System.Runtime.Serialization as a reference in the solution
  • Make sure the project is running .NET Framework 4.0
  • Create an app.config file with the content from Tridion.ContentManager.CoreService.Client.config and update the netTcp_2011 address with the appropriate server IP
<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <!-- Default/example WCF settings for Core Service. These settings should be copied into the host application's configuration file. -->
    <bindings>
      <!-- Default Core Service binding settings are provided here. These can be used as a starting point for further customizations. -->
      <basicHttpBinding>
        <binding name="basicHttp" maxReceivedMessageSize="10485760">
          <readerQuotas maxStringContentLength="10485760" maxArrayLength="10485760"/>
          <security mode="TransportCredentialOnly">
            <!-- For LDAP or SSO authentication of transport credentials, use clientCredentialType="Basic" -->
            <transport clientCredentialType="Windows"/>
          </security>
        </binding>
        <binding name="streamDownload_basicHttp" maxReceivedMessageSize="209715200" transferMode="StreamedResponse" messageEncoding="Mtom" sendTimeout="00:10:00">
          <security mode="TransportCredentialOnly">
            <!-- For LDAP or SSO authentication of transport credentials, use clientCredentialType="Basic" -->
            <transport clientCredentialType="Windows"/>
          </security>
        </binding>
        <binding name="streamUpload_basicHttp" maxReceivedMessageSize="209715200" transferMode="StreamedRequest" messageEncoding="Mtom" receiveTimeout="00:10:00">
          <security mode="None"/>
        </binding>
      </basicHttpBinding>
      <wsHttpBinding>
        <binding name="wsHttp" transactionFlow="true" maxReceivedMessageSize="10485760">
          <readerQuotas maxStringContentLength="10485760" maxArrayLength="10485760"/>
          <security mode="Message">
            <message clientCredentialType="Windows"/>
          </security>
        </binding>
      </wsHttpBinding>
      <netTcpBinding>
        <binding name="netTcp" transactionFlow="true" transactionProtocol="WSAtomicTransaction11" maxReceivedMessageSize="10485760">
          <readerQuotas maxStringContentLength="10485760" maxArrayLength="10485760"/>
        </binding>
        <binding name="streamDownload_netTcp" maxReceivedMessageSize="2147483647" transferMode="StreamedResponse" sendTimeout="00:10:00"/>
        <binding name="streamUpload_netTcp" maxReceivedMessageSize="2147483647" transferMode="StreamedRequest" receiveTimeout="00:10:00"/>
      </netTcpBinding>
    </bindings>
    <client>
      <!-- Default Core Service endpoint settings are provided here. The endpoint name should be specified when constructing a proxy service instance.
      The mapping between proxy service types and applicable endpoint names is as follows (see also the contracts specified on each endpoint):
      CoreServiceClient: basicHttp
      SessionAwareCoreServiceClient: wsHttp, netTcp
      StreamDownloadClient: streamDownload_basicHttp, streamDownload_netTcp
      StreamUploadClient: streamUpload_basicHttp, streamUpload_netTcp
      -->
      <endpoint name="basicHttp_2011" address="http://10.9.5.48/webservices/CoreService2011.svc/basicHttp" binding="basicHttpBinding" bindingConfiguration="basicHttp" contract="Tridion.ContentManager.CoreService.Client.ICoreService"/>
      <endpoint name="streamDownload_basicHttp_2011" address="http://10.9.5.48/webservices/CoreService2011.svc/streamDownload_basicHttp" binding="basicHttpBinding" bindingConfiguration="streamDownload_basicHttp" contract="Tridion.ContentManager.CoreService.Client.IStreamDownload"/>
      <endpoint name="streamUpload_basicHttp_2011" address="http://10.9.5.48/webservices/CoreService2011.svc/streamUpload_basicHttp" binding="basicHttpBinding" bindingConfiguration="streamUpload_basicHttp" contract="Tridion.ContentManager.CoreService.Client.IStreamUpload"/>
      <endpoint name="wsHttp_2011" address="http://10.9.5.48/webservices/CoreService2011.svc/wsHttp" binding="wsHttpBinding" bindingConfiguration="wsHttp" contract="Tridion.ContentManager.CoreService.Client.ISessionAwareCoreService">
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
      <endpoint name="netTcp_2011" address="net.tcp://10.9.5.48:2660/CoreService/2011/netTcp" binding="netTcpBinding" bindingConfiguration="netTcp" contract="Tridion.ContentManager.CoreService.Client.ISessionAwareCoreService"/>
      <endpoint name="streamDownload_netTcp_2011" address="net.tcp://localhost:2660/CoreService/2011/streamDownload_netTcp" binding="netTcpBinding" bindingConfiguration="streamDownload_netTcp" contract="Tridion.ContentManager.CoreService.Client.IStreamDownload"/>
      <endpoint name="streamUpload_netTcp_2011" address="net.tcp://localhost:2660/CoreService/2011/streamUpload_netTcp" binding="netTcpBinding" bindingConfiguration="streamUpload_netTcp" contract="Tridion.ContentManager.CoreService.Client.IStreamUpload"/>
    </client>
  </system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
  • Write the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tridion.ContentManager.CoreService.Client;

namespace CoreService
{
    class Program
    {
        static void Main(string[] args)
        {
            // Connecting to Core Service
            var client = new SessionAwareCoreServiceClient("netTcp_2011");
            client.ChannelFactory.Credentials.Windows.ClientCredential = new System.Net.NetworkCredential("administrator", "tridion");
            Console.WriteLine("Connected to CoreService with user " + client.GetCurrentUser().Title);
            Console.Read();
        }
    }
}



4 comments:

  1. Nice, Robert! Welcome to Tridion blogging. I could have used a guide just recently, thanks for sharing.

    ReplyDelete
    Replies
    1. Thanks! I'm still a complete newbie. Glad to join the community =)

      Delete
    2. Being a newbie is sometimes an advantage. When you're learning something, you are in touch with all the issues that other learners will encounter. Once you're through the learning process, and have "absorbed the abstraction" it's too late. Blogging about your 'newbie' experiences is a great way to help others, and also a great way to help you remember the moment.

      Delete
  2. Nice Article for the beginners… Thanks Robert for Sharing

    ReplyDelete