【问题标题】:Stream Attachments from Microsoft Graph API to SOAP WebService从 Microsoft Graph API 流式传输附件到 SOAP WebService
【发布时间】:2019-10-11 05:41:07
【问题描述】:

首先我想描述一下我们的架构,它包括:

  • Outlook Web 插件(托管在本地 IIS 上)
  • ASP.NET Web API(本地托管)
  • 云中的 Microsoft Graph REST API
  • 一个 SOAP WebService(不在我的控制之下,在本地)。

流程可以用以下步骤描述:

  1. 用户单击 Outlook Web 插件中的按钮
  2. 从 Web 插件向 Web API 发出 Ajax 请求,并将消息 ID 作为参数。
  3. Web API 通过GraphServiceClient 从 Graph 请求消息
  4. Web API 将消息中的附件打包到 SOAP 请求中,并将其发送到 SOAP 服务。

Microsoft Graph 中的附件以字节数组的形式返回并存储在内存中。所以我们可能会遇到内存和性能问题。

我的问题是,是否可以将附件从 Microsoft Graph 直接流式传输到 SOAP 服务?

从 MS Graph Rest API 获取附件

var mail = graphClient
    .Users["mailbox"]
    .Messages["id"]
    .Request()
    .Expand("attachments");

foreach (var attachment in message.Attachments)
{
    if (attachment.GetType() == typeof(FileAttachment))
    {
        var fileAttachment = (FileAttachment) attachment;

        // ==> fileAttachment.ContentBytes already contains the bytes of the attachment
    }
}

是否应该通过第二个请求来获取附件的字节? 怎么流下来?

soap服务wsdl中附件部分的定义

<xsd:complexType name="DocumentData">
    <xsd:sequence>
        <xsd:element name="name" type="xsd:string"/>
        <xsd:element name="extension" type="xsd:string"/>
        <xsd:element name="content" type="xsd:base64Binary"/>
    </xsd:sequence>
</xsd:complexType>

...在服务引用中生成为

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3062.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://is.uniqa.at/UniqaDocumentWF.WS:startDoWFProcess")]
public partial class DocumentData : object, System.ComponentModel.INotifyPropertyChanged
{

    private string nameField;
    private string extensionField;
    private byte[] contentField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
            this.RaisePropertyChanged("name");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 1)]
    public string extension
    {
        get
        {
            return this.extensionField;
        }
        set
        {
            this.extensionField = value;
            this.RaisePropertyChanged("extension");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, DataType = "base64Binary", Order = 2)]
    public byte[] content
    {
        get
        {
            return this.contentField;
        }
        set
        {
            this.contentField = value;
            this.RaisePropertyChanged("content");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null))
        {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

...并且可以这样创建:

DoWF.DocumentData data = new DocumentData();
data.name = name;
data.extension = extension;

// At the moment content is set to 
// "fileAttachment.ContentBytes" before 
// the request is send to the soap service
data.content = content; 

如何以及在何处将来自 Rest API 的附件字节通过“我的”Web API 传输到 SOAP 服务?

我也将感谢一种完全不同的方法来解决这个问题。

【问题讨论】:

    标签: microsoft-graph-api asp.net-core-webapi soap-client microsoft-graph-mail


    【解决方案1】:

    考虑到所涉及的层数以及您无法控制工作流任一端的 API,我不确定这种方法是否可行。更重要的是,我怀疑即使你可以让这个工作,你会发现它非常脆弱。

    我会根据文档中的这篇文章提出一些建议:Get attachments of an Outlook item from the server。此使用示例使用 Exchange Web 服务而不是 Microsoft Graph,但一般流程是相同的:

    1. 用户点击插件中的按钮
    2. 插件将userPrincipalName 和消息id 传递给Web API
    3. Web API 将附件下载到临时存储中。
    4. Web API 将附件从临时存储上传到 SOAP 服务。

    为了完成这项工作,您的 Web API 需要使用 Client Credentials 以使用 Mail.Read application scope 连接到 Microsoft Graph。这将允许 Web API 连接到任何邮箱并下载附件。

    【讨论】:

      猜你喜欢
      • 2020-04-11
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多