我如何(正确)从EWS继承类,例如约会?

时间:2013-01-03 22:41:50

标签: c# xml-serialization exchange-server exchangewebservices

同样的问题: 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,有些值是null,但我不能为我的生活弄清楚它是什么。这是原始错误

  

测试名称:TestMeetingCreation测试   FullName:Hc360LibTests.UnitTest1.TestMeetingCreation Test   来源:c:\ Users \ okane \ Documents \ Visual Studio   2012 \项目\ HealthCheck360OutlookCallScheduleHelper \ Hc360LibTests \的UnitTest1.cs   :第15行测试结果:测试持续时间失败:0:00:00.5728502

     

结果消息:测试方法   Hc360LibTests.UnitTest1.TestMeetingCreation抛出异常:   System.ArgumentException:空字符串''不是有效的本地字符串   名称。结果StackTrace:at   System.Xml.XmlWellFormedWriter.WriteStartElement(String prefix,String   localName,String ns)at   Microsoft.Exchange.WebServices.Data.EwsServiceXmlWriter.WriteStartElement(XML命名空间   xmlNamespace,String localName)at   Microsoft.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 1.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`1   sendInvitationsMode)at   Microsoft.Exchange.WebServices.Data.Appointment.Save(FolderId   destinationFolderId,SendInvitationsMode sendInvitationsMode)at   HealthCheck360ExchangeLib.CallSlotRepository.CreateTestAppointment()   在c:\ Users \ okane \ Documents \ Visual Studio中   2012 \项目\ HealthCheck360OutlookCallScheduleHelper \ HealthCheck360ExchangeLib \ CallSlotRepository.cs:行   70在Hc360LibTests.UnitTest1.TestMeetingCreation()中   c:\ Users \ okane \ Documents \ Visual Studio   2012 \项目\ HealthCheck360OutlookCallScheduleHelper \ Hc360LibTests \的UnitTest1.cs:行   17

1 个答案:

答案 0 :(得分:4)

似乎无法保存继承的项目。

如果您在EWS托管API的代码中看得太深,您会发现序列化从ServiceObjectDefinitionAttribute或方法ServiceObject.GetXmlElementNameOverride确定XML中的元素名称。 Appointment class继承自Item,而Item又继承自ServiceObject。 Appointment类还具有ServiceObjectDefinitionAttribute,并使用XML元素名称“CalendarItem”进行序列化。

因此,要使用从Appointment继承的类,您需要在类上使用ServiceObjectDefintionAttribute,或者必须覆盖GetXmlElementNameOverride。不幸的是,ServiceObjectDefintionAttribute和GetXmlElementNameOverride是内部的,因此无法做到这一点。

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

<强>更新

如果要将扩展属性公开为您自己类的真实属性,可以通过委派给约会对象来完成。这不是完美的,但它是一个选择:

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