Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

tridion - Faulted State error while creating component with Core Service

I get a "faulted state" error when working with the Core Service in SDL Tridion 2011 SP1. What's wrong with the following?

namespace coreservice1
{
      public partial class _Default : System.Web.UI.Page
      {
       protected void Page_Load(object sender, EventArgs e)
       {
        try
        {

            using (ChannelFactory<ISessionAwareCoreService> factory =
      new ChannelFactory<ISessionAwareCoreService>("wsHttp_2011"))
            {
                ISessionAwareCoreService client = factory.CreateChannel();
                string SCHEMA_URI = "tcm:7-426-8";

                var schemaFields = client.ReadSchemaFields(SCHEMA_URI, true, new ReadOptions());
                foreach (var field in schemaFields.Fields) 
                { 
                    Response.Write(string.Format("{0}", field.Name));                         
                } 
                Response.Write(schemaFields.NamespaceUri);
                string NEW_COMPONENT_FOLDER_URI = "tcm:8-15-2";

               Tridion.ContentManager.CoreService.Client.ComponentData component = new Tridion.ContentManager.CoreService.Client.ComponentData
               {
                   Schema = new LinkToSchemaData { IdRef = "tcm:8-426-8"},
                   Title = "Helloworldalll",
                   Id = "tcm:0-0-0",
                   LocationInfo = new LocationInfo
                   {
                       OrganizationalItem =
                           new LinkToOrganizationalItemData { IdRef = NEW_COMPONENT_FOLDER_URI}
                   },
               };

               string namespaceUri = schemaFields.NamespaceUri;
               System.Text.StringBuilder content = new StringBuilder();
               string First = "Hello World.This is Fisrt field";
               content.AppendFormat("<{0} xmlns="{1}">", schemaFields.RootElementName, namespaceUri);
               content.AppendFormat("<{0} xmlns="{1}">{2}</{0}>", "first", namespaceUri, First);
               content.AppendFormat("</{0}>", schemaFields.RootElementName);
               component.Content = content.ToString();
               ComponentData comp = (ComponentData)client.Create(component, new ReadOptions());
               string newlyCreatedComponentID = comp.Id;
               Response.Write("Hello hai");
               Response.Write("Id of newly created component: " + newlyCreatedComponentID);
            }

        }

        catch (Exception ex)
        {
            Response.Write(ex.StackTrace);
            Response.Write("exception is " + ex.Message);
        }

    }
  }
}

“at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelFactory.OnClose(TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelFactory.TypedServiceChannelFactory`1.OnClose(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout) at System.ServiceModel.ChannelFactory.OnClose(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout) at System.ServiceModel.ChannelFactory.System.IDisposable.Dispose() at coreservice1._Default.Page_Load(Object sender, EventArgs e) in D:SampleProjects_Tridionestcoreservice1coreservice1coreservice.aspx.cs:line 73exception is The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.“

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this might be to do with the way you instantiate your client object.

You can add this as Service Reference in Visual Studio:

e.g. Add Service reference to http://{your tridion url}/webservices/CoreService.svc and give it a namespace of TridionCoreService, then you can use it like this:

TridionCoreService.CoreService2010Client client = new TridionCoreService.CoreService2010Client();   

Alternatively you can use the method here which allows you to create a Core Service reference without needing a config file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...