Tridion 使用核心服务创建组件时出现错误状态错误

Tridion 使用核心服务创建组件时出现错误状态错误,tridion,Tridion,在SDL Tridion 2011 SP1中使用核心服务时,我遇到了一个“错误状态”错误。下面的问题是什么 namespace coreservice1 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { try { us

在SDL Tridion 2011 SP1中使用核心服务时,我遇到了一个“错误状态”错误。下面的问题是什么

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);
        }

    }
  }
}
名称空间coreservice1
{
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
尝试
{
使用(渠道工厂)=
新ChannelFactory(“wsHttp_2011”))
{
ISessionAwareCreservice客户端=工厂.CreateChannel();
字符串模式_URI=“tcm:7-426-8”;
var schemaFields=client.ReadSchemaFields(SCHEMA_URI,true,new ReadOptions());
foreach(schemaFields.Fields中的var字段)
{ 
Write(string.Format(“{0}”,field.Name));
} 
Write(schemaFields.NamespaceUri);
字符串新的\u组件\u文件夹\u URI=“tcm:8-15-2”;
Tridion.ContentManager.CoreService.Client.ComponentData组件=新的Tridion.ContentManager.CoreService.Client.ComponentData组件
{
Schema=newlinktoschemadata{IdRef=“tcm:8-426-8”},
Title=“Helloworldalll”,
Id=“tcm:0-0-0”,
LocationInfo=新的LocationInfo
{
组织学=
新建LinkToOrganizationalItemData{IdRef=new_COMPONENT_FOLDER_URI}
},
};
字符串namespaceUri=schemaFields.namespaceUri;
System.Text.StringBuilder内容=新建StringBuilder();
string First=“你好,世界,这是第一个字段”;
content.AppendFormat(“”,schemaFields.RootElementName,namespaceUri);
AppendFormat(“{2}”,“first”,namespaceUri,first);
content.AppendFormat(“,schemaFields.RootElementName”);
component.Content=Content.ToString();
ComponentData comp=(ComponentData)client.Create(component,new ReadOptions());
字符串newlyCreatedComponentID=comp.Id;
回复。写下(“你好,海”);
Response.Write(“新创建组件的Id:+newlyCreatedComponentID”);
}
}
捕获(例外情况除外)
{
响应。写入(例如StackTrace);
响应。写入(“异常为”+ex.Message);
}
}
}
}

“在System.ServiceModel.Channels.CommunicationObject.Close(时间跨度超时)在System.ServiceModel.Channels.ServiceChannelFactory.OnClose(时间跨度超时)在System.ServiceModel.Channels.ServiceChannelFactory.TypedServiceChannelFactory`1.OnClose(时间跨度超时)在System.ServiceModel.Channels.CommunicationObject.Close(时间跨度超时)在System.ServiceModel.ChannelFactory.OnClose(TimeSpan timeout)在System.ServiceModel.ChannelFactory.System.IDisposable.Dispose()在System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout)在System.ServiceModel.ChannelFactory.System.IDisposable.Dispose()在coreservice1.\默认值.页面\加载(对象发送方,事件参数e)在D:\SampleProjects\u Tridion\test\coreservice1\coreservice1\coreservice.aspx.cs中:第73行异常是通信对象System.ServiceModel.Channels.ServiceChannel无法用于通信,因为它处于故障状态。”


我认为这可能与实例化客户机对象的方式有关

您可以在Visual Studio中将其添加为服务引用:

e、 g.将服务引用添加到http://{your tridion url}/webservices/CoreService.svc,并给它一个TridionCoreService名称空间,然后您可以像这样使用它:

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

或者,您也可以使用这种方法来创建核心服务引用,而不需要配置文件。

它确实创建了组件吗?您做到了吗

我通常为CoreService编写一个包装类,实现
IDisposable
,并在其中使用以下方法:

private void InitializeClient()
{
NetTcpBinding=new NetTcpBinding{MaxReceivedMessageSize=2147483647};
XmlDictionaryReaderQuotas=新的XmlDictionaryReaderQuotas
{
MaxStringContentLength=2147483647,
MaxArrayLength=2147483647
};
binding.ReaderQuotas=配额;
_client=new SessionAwareCoreServiceClient(绑定,_endpointAddress);
如果(_client!=null)_coreServiceVersion=_client.GetApiVersion();
}

public void Dispose()
{
if(_client.State==CommunicationState.Faulted)
{
_client.Abort();
}
其他的
{
_client.Close();
}
}

发送一个我前段时间创建的示例,以防

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreWebService.ServiceReference1;

namespace CoreWebService
{
    class CoreWebServiceSamples
    {

        public static void  createComponent()
        {
            string schemaWebDavUrl = "/webdav/020%20Content/Building%20Blocks/Content/wstest/wstest.xsd";
            string folderWebDavUrl = "/webdav/020%20Content/Building%20Blocks/Content/wstest";

            CoreServicesUtil coreServicesUtil = new CoreServicesUtil();

            FolderData folderData = coreServicesUtil.getFolderData(folderWebDavUrl);
            ComponentData componentData = folderData.AddComponentData();
            componentData.Title = "This is a Test ..... ";
            componentData.Schema = coreServicesUtil.getLinkToSchemaData(schemaWebDavUrl);

            SchemaData schemaData = coreServicesUtil.getSchemaData(schemaWebDavUrl);

            componentData.Content = xmlUtil.GetNewXmlNode("Content", schemaData.NamespaceUri);
            componentData.Metadata = xmlUtil.GetNewXmlNode("Metadata", schemaData.NamespaceUri);

            componentData.AddSingleField("singlefield", "singlefield sample", schemaData.NamespaceUri);
            componentData = (ComponentData)coreServicesUtil.coreServiceClient.Save(componentData, coreServicesUtil.readOptions);
            coreServicesUtil.coreServiceClient.CheckIn(componentData.Id, coreServicesUtil.readOptions);
            coreServicesUtil.coreServiceClient.Close();
        }
    }
}
CoreServicesUtil

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreWebService.ServiceReference1;
using CoreWebService.Properties;
using System.Xml;
using System.Xml.Serialization;



namespace CoreWebService
{

    public class CoreServicesUtil
    { 
        public CoreService2010Client coreServiceClient;
        public ReadOptions readOptions;
        /// <summary>
        /// 
        /// </summary>
        public CoreServicesUtil()
        {
            this.coreServiceClient = new CoreService2010Client("basicHttp_2010");
            this.readOptions = new ReadOptions();
        }

        public  FolderData getFolderData(string tcmuri)
        {
            FolderData folderData = (FolderData)coreServiceClient.Read(tcmuri,ReadOptions);
            return folderData;
        }
        public LinkToSchemaData getLinkToSchemaData(string tcmuri)
        {
            LinkToSchemaData linkToSchemaData = new ServiceReference1.LinkToSchemaData();
            linkToSchemaData.IdRef = getSchemaData(tcmuri).Id;
            return linkToSchemaData;
        }
        public  SchemaData getSchemaData(string tcmuri)
        {
            SchemaData schemaData = (SchemaData)coreServiceClient.Read(tcmuri, readOptions);
            return schemaData;
        }

   }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用CoreWebService.ServiceReference1;
使用CoreWebService.Properties;
使用System.Xml;
使用System.Xml.Serialization;
命名空间CoreWebService
{
公共类CoreServicesUtil
{ 
public CoreService2010Client coreServiceClient;
公共阅读选项;
/// 
/// 
/// 
公共CoreServicesUtil()
{
this.coreServiceClient=新的CoreService2010Client(“basicHttp_2010”);
this.readOptions=新的readOptions();
}
public FolderData getFolderData(字符串tcmuri)
{
FolderData FolderData=(FolderData)coreServiceClient.Read(tcmuri,ReadOptions);
返回folderData;
}
public LinkToSchemaData getLinkToSchemaData(字符串tcmuri)
{
LinkToSchemaData LinkToSchemaData=新服务引用1.LinkToSchemaData();
linkToSchemaData.IdRef=getSchemaData(tcmuri).Id;
返回linkToSchemaData;
}
公共SchemaData getSchemaData(字符串tcmuri)
{
图式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace CoreWebService
{
    public class xmlUtil
    {

        /**
* <summary>
* Name: enumeration
* </summary>
**/
        public  enum Scope
        {
            Content,
            Metadata
        }


        /**
 * <summary>
 * Name: AddItemLinkFieldGeneric
 * Description: basic method for add component links, multimedia links, keyword field to an XmlElement
 * </summary>
 **/

        /**
        * <summary>
        * Name: getXMLElementData
        * Description: adds a single field to an XmlElement
        * </summary>
        **/
        public static XmlElement getXMLElementData(string dataNode)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(dataNode);
            return doc.DocumentElement;
        }



        /**
        * <summary>
        * Name: GetNewXmlNode
        * Description: returns an xml element based on the name and schema
        * </summary>
        **/
        public static string GetNewXmlNode(string Name, string Namespace)
        {
            XmlDocument doc = new XmlDocument();
            XmlElement xmlElem = doc.CreateElement(Name, Namespace);
            doc.AppendChild(xmlElem);
            return doc.FirstChild.OuterXml;
        }

    }
}