如何(正确地)从EWS继承类,比如Appointment

本文关键字:比如 Appointment 继承 EWS 正确地 如何 | 更新日期: 2023-09-27 17:50:41

同样的问题:https://stackoverflow.com/questions/11294207/exchange-web-services-argumentexception-using-my-own-contact-class

我正试图从Microsoft.Exchange.WebServices.Data.Appointment编写一个派生类。但是,如果不抛出似乎是序列化错误的东西,则无法保存派生类。即使我没有对派生类做任何修改,也会发生这种情况。

如此:

public void CreateTestAppointment() {
        Appointment appointment = new Appointment(Exchange) {
            ItemClass = "IPM.Appointment", //Exchange Specific. Dont touch unless you     know what this does. I sure as hell don't.
            Subject = string.Format("{0} Test", "Oliver")
        };
        appointment.RequiredAttendees.Add("okane@cottinghambutler.com");
        appointment.Body = new MessageBody {
            Text = "Meeting invite body text placeholder"
        };
        //Add item to the appropriate categories
        appointment.Categories.Add(string.Format("[C]{0}", "Arbitrary Client Group"));
        // Add calendar properties to the appointment.
        appointment.Start = DateTime.Now.AddMinutes(10);
        appointment.End = DateTime.Now.AddMinutes(30);
        appointment.Save(Hc360CallCalendarFolderId, SendInvitationsMode.SendOnlyToAll);
    }

但这不是:

 public void CreateTestAppointment() {
        InheritedAppointment appointment = new InheritedAppointment(Exchange) {
            ItemClass = "IPM.Appointment", //Exchange Specific. Dont touch unless you know what this does. I sure as hell don't.
            Subject = string.Format("{0} Test", "Oliver")
        };
        appointment.RequiredAttendees.Add("okane@cottinghambutler.com");
        appointment.Body = new MessageBody {
            Text = "Meeting invite body text placeholder"
        };
        //Add item to the appropriate categories
        appointment.Categories.Add(string.Format("[C]{0}", "Arbitrary Client Group"));
        // Add calendar properties to the appointment.
        appointment.Start = DateTime.Now.AddMinutes(10);
        appointment.End = DateTime.Now.AddMinutes(30);
        appointment.Save(Hc360CallCalendarFolderId, SendInvitationsMode.SendOnlyToAll);
    }

这很奇怪,因为我实际上并没有在派生类中做任何事情。

    public class InheritedAppointment : Microsoft.Exchange.WebServices.Data.Appointment {
    public InheritedAppointment(ExchangeService serivce)
        : base(serivce) {
    }}

看起来像是被序列化为XML,有些值是空的,但我无论如何也弄不清楚它是什么。下面是原始错误

测试名称:TestMeetingCreation TestFullName: Hc360LibTests.UnitTest1。TestMeetingCreation测试来源:c:'Users'okane'Documents'Visual Studio2012 ' ' HealthCheck360OutlookCallScheduleHelper ' Hc360LibTests ' UnitTest1.cs项目: line 15 Test result: Failed Test Duration: 0:00:00.5728502

Result Message:测试方法Hc360LibTests.UnitTest1。TestMeetingCreation抛出异常:系统。空字符串"不是一个有效的本地字符串名字结果StackTrace: atSystem.Xml.XmlWellFormedWriter。WriteStartElement(字符串前缀,字符串localName, String ns) atMicrosoft.Exchange.WebServices.Data.EwsServiceXmlWriter.WriteStartElement (XmlNamespacexmlNamespace, String localName) atMicrosoft.Exchange.WebServices.Data.PropertyBag.WriteToXml (EwsServiceXmlWriter作家)Microsoft.Exchange.WebServices.Data.CreateRequest 2.WriteElementsToXml(EwsServiceXmlWriter writer) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.WriteBodyToXml(EwsServiceXmlWriter writer) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.WriteToXml(EwsServiceXmlWriter writer) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.EmitRequest(IEwsHttpWebRequest request) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.BuildEwsHttpWebRequest() at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request) at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest . execute ()在Microsoft.Exchange.WebServices.Data.ExchangeService。InternalCreateItems(IEnumerable 1 items, FolderId parentFolderId, Nullable 1 messageDisposition)Nullable 1 sendInvitationsMode, ServiceErrorHandling errorHandling)
at Microsoft.Exchange.WebServices.Data.Item.InternalCreate(FolderId parentFolderId, Nullable
1 messageDisposition, Nullable ' 1sendInvitationsMode)Microsoft.Exchange.WebServices.Data.Appointment.Save (FolderIdSendInvitationsMode SendInvitationsMode) atHealthCheck360ExchangeLib.CallSlotRepository.CreateTestAppointment ()c:'Users'okane'Documents'Visual Studio2012 ' ' HealthCheck360OutlookCallScheduleHelper ' HealthCheck360ExchangeLib项目' CallSlotRepository.cs:行70在Hc360LibTests.UnitTest1.TestMeetingCreation(c:'Users'okane'Documents'Visual工作室2012 ' ' HealthCheck360OutlookCallScheduleHelper ' Hc360LibTests项目' UnitTest1.cs:行17日

如何(正确地)从EWS继承类,比如Appointment

似乎无法保存继承的项。

如果您深入查看EWS Managed API的代码,您将发现序列化从ServiceObjectDefinitionAttribute或ServiceObject.GetXmlElementNameOverride方法确定XML中的元素名称。Appointment类继承自Item, Item又继承自ServiceObject。Appointment类也有ServiceObjectDefinitionAttribute,并使用XML元素名称"CalendarItem"序列化。

所以,要使用从Appointment继承的类,你需要在你的类上使用ServiceObjectDefintionAttribute,或者你必须重写GetXmlElementNameOverride。不幸的是,ServiceObjectDefintionAttribute和GetXmlElementNameOverride是内部的,所以没有办法做到这一点。

问题是为什么要使用继承类?您是否期望Exchange能够保存类上的属性?在这种情况下,您可能需要查看ExtendedProperties。

更新:

如果你想在你自己的类上公开扩展属性作为真实属性,你可以通过委托给一个Appointment对象来实现。这不是完美的,但它是一个选项:

public class ExtendedAppointment
{
    Appointment _appointment;
    public ExtendedAppointment(Appointment appointment)
    {
        _appointment = appointment;
    }
    public Appointment Appointment { get { return _appointment; } }
    public object SomeExtendedProperty
    {
        get 
        { 
            return _appointment.ExtendedProperties[0].Value;
        }
        set
        {
            _appointment.ExtendedProperties[0].Value = value;
        }
    }
}