简体   繁体   English

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

[英]How do I (properly) inherit classes from EWS such as an Appointment?

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

I am trying to write a derived class from Microsoft.Exchange.WebServices.Data.Appointment. 我试图从Microsoft.Exchange.WebServices.Data.Appointment编写派生类。 However, the derived class cannot be saved without throwing what appears to be a serialization error. 但是,如果不抛出看似序列化错误的内容,则无法保存派生类。 This happens even if I make no modifications to the derived class. 即使我没有对派生类进行任何修改,也会发生这种情况。

This works: 这有效:

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

But this doesn't: 但这不是:

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

Which is very strange, as I'm not actually doing anything in the derived class. 这很奇怪,因为我实际上并没有在派生类中做任何事情。

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

    }}

It looks like this is being seralized to XML, and some value is null, but I cannot for the life of me figure out what it is. 它看起来像是被封装到XML,有些值是null,但我不能为我的生活弄清楚它是什么。 Here's the raw error 这是原始错误

Test Name: TestMeetingCreation Test FullName: Hc360LibTests.UnitTest1.TestMeetingCreation Test Source: c:\\Users\\okane\\Documents\\Visual Studio 2012\\Projects\\HealthCheck360OutlookCallScheduleHelper\\Hc360LibTests\\UnitTest1.cs : line 15 Test Outcome: Failed Test Duration: 0:00:00.5728502 测试名称:TestMeetingCreation测试FullName:Hc360LibTests.UnitTest1.TestMeetingCreation测试源:c:\\ Users \\ okane \\ Documents \\ Visual Studio 2012 \\ Projects \\ HealthCheck360OutlookCallScheduleHelper \\ Hc360LibTests \\ UnitTest1.cs:第15行测试结果:失败测试持续时间:0:00 :00.5728502

Result Message: Test method Hc360LibTests.UnitTest1.TestMeetingCreation threw exception: System.ArgumentException: The empty string '' is not a valid local name. 结果消息:测试方法Hc360LibTests.UnitTest1.TestMeetingCreation引发异常:System.ArgumentException:空字符串''不是有效的本地名称。 Result StackTrace: at System.Xml.XmlWellFormedWriter.WriteStartElement(String prefix, String localName, String ns) at Microsoft.Exchange.WebServices.Data.EwsServiceXmlWriter.WriteStartElement(XmlNamespace xmlNamespace, String localName) at Microsoft.Exchange.WebServices.Data.PropertyBag.WriteToXml(EwsServiceXmlWriter writer) at 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() at 结果StackTrace:位于Microsoft.Exchange.WebServices.Data.PropertyBag上的Microsoft.Exchange.WebServices.Data.EwsServiceXmlWriter.WriteStartElement(XmlNamespace xmlNamespace,String localName)中的System.Xml.XmlWellFormedWriter.WriteStartElement(String prefix,String localName,String ns)位于Microsoft.Exchange.WebServices.Data上的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()at 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
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 sendInvitationsMode, ServiceErrorHandling errorHandling)
at Microsoft.Exchange.WebServices.Data.Item.InternalCreate(FolderId parentFolderId, 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() in c:\\Users\\okane\\Documents\\Visual Studio 2012\\Projects\\HealthCheck360OutlookCallScheduleHelper\\HealthCheck360ExchangeLib\\CallSlotRepository.cs:line 70 at Hc360LibTests.UnitTest1.TestMeetingCreation() in c:\\Users\\okane\\Documents\\Visual Studio 2012\\Projects\\HealthCheck360OutlookCallScheduleHelper\\Hc360LibTests\\UnitTest1.cs:line 17
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) ()位于c:\\ Users \\ okane \\ Documents \\ Visual Studio 2012 \\ Projects \\ HealthCheck360OutlookCallScheduleHelper \\ HealthCheck360ExchangeLib \\ CallSlotRepository.cs:第70行,位于c:\\ Users \\ okane \\ Documents \\ Visual Studio 2012中的Hc360LibTests.UnitTest1.TestMeetingCreation() Projects \\ HealthCheck360OutlookCallScheduleHelper \\ Hc360LibTests \\ UnitTest1.cs:第17行

It seems that it is not possible to save an inherited item. 似乎无法保存继承的项目。

If you look deep enough in the code for the EWS Managed API you will find that the serialization determines the element names in the XML from either the ServiceObjectDefinitionAttribute or from the method ServiceObject.GetXmlElementNameOverride. 如果您在EWS托管API的代码中看得太深,您会发现序列化从ServiceObjectDefinitionAttribute或方法ServiceObject.GetXmlElementNameOverride确定XML中的元素名称。 The Appointment class inherits from Item which in turn inherits from ServiceObject. Appointment类继承自Item,而Item又继承自ServiceObject。 The Appointment class also has the ServiceObjectDefinitionAttribute and is serialized with the XML element name "CalendarItem". Appointment类还具有ServiceObjectDefinitionAttribute,并使用XML元素名称“CalendarItem”进行序列化。

So, to use a class inherited from Appointment you need to either use the ServiceObjectDefintionAttribute on your class or you have to override GetXmlElementNameOverride. 因此,要使用从Appointment继承的类,您需要在类上使用ServiceObjectDefintionAttribute,或者必须覆盖GetXmlElementNameOverride。 Unfortunately ServiceObjectDefintionAttribute and GetXmlElementNameOverride are internal so there is no way to do this. 不幸的是ServiceObjectDefintionAttribute和GetXmlElementNameOverride是内部的,所以没有办法做到这一点。

The question is why do you want to use an inherited class? 问题是为什么要使用继承的类? Did you expect Exchange to be able to save the properties on your class? 您是否希望Exchange能够保存您班级的属性? In that case you might want to take a look at ExtendedProperties . 在这种情况下,您可能需要查看ExtendedProperties

UPDATE: 更新:

If you want to expose the extended properties as real properties on your own class you can do it by delegating to an Appointment object. 如果要将扩展属性公开为您自己类的真实属性,可以通过委派给约会对象来完成。 It is not perfect but it is an option: 它并不完美,但它是一个选择:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在EWS中创建时区被忽略的约会 - How do i Create Appointment in EWS with Timezone ignored 如何为我继承的类声明构造函数? - How do I declare constructors for classes I inherit from? 如何组织相互继承但又具有相互继承的属性的C#类? - How do I organize C# classes that inherit from one another, but also have properties that inherit from one another? 如何从Outlook Exchange Server(C#)获取约会的约会正文? - How to get appointment body text for an appointment from outlook exchange server(EWS),c#? 如何为从Unity中的Monobehaviour继承的类编写单元测试? - How do I write Unit Tests for classes that inherit from Monobehaviour in Unity? 如何编写通用方法来复制均从同一抽象基类继承的类的实例? - How do I write a general method to copy instances of classes that all inherit from the same abstract base class? 如何从Microsoft Exchange导出所有约会数据? - How do I export all appointment data from Microsoft Exchange? Exchange EWS API-从约会获取CalendarEvent - Exchange EWS API - Get CalendarEvent from Appointment 如何通过EWS与指定组织者添加约会? - How to add appointment with specify organizer via EWS? 我如何从Dictionary继承? - How do I inherit from Dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM