【问题标题】:Unable to cast object of type List[Microsoft.Graph.ChatMessageHostedContent] to type 'Microsoft.Graph.IChatMessageHostedContentsCollectionPage'无法将 List[Microsoft.Graph.ChatMessageHostedContent] 类型的对象转换为类型“Microsoft.Graph.IChatMessageHostedContentsCollectionPage”
【发布时间】:2020-09-17 14:07:25
【问题描述】:

我一直面临以下问题:

无法转换类型的对象 'System.Collections.Generic.List`1[Microsoft.Graph.ChatMessageHostedContent]' 键入“Microsoft.Graph.IChatMessageHostedContentsCollectionPage”

当我尝试使用 Microsoft Graph SDK 在 Microsoft Teams 中发布图像以及聊天消息时。我使用了最新的 Microsoft.Graph.Beta(0.25.0-preview) 和 Microsoft.Graph.Core(1.21.0) Nuget 包,并使用了以下链接中的整个代码:

https://docs.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-beta&tabs=csharp#request

代码:

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var chatMessage = new ChatMessage
{
    Subject = null,
    Body = new ItemBody
    {
        ContentType = BodyType.Html,
        Content = "<attachment id=\"74d20c7f34aa4a7fb74e2b30004247c5\"></attachment>"
    },  
    HostedContents = (IChatMessageHostedContentsCollectionPage)new List<ChatMessageHostedContent>()
    {
        new ChatMessageHostedContent
        {
            ContentBytes = Encoding.ASCII.GetBytes("iVBORw0KGgoAAAANSUhEUgAAA..."),
            ContentType = "image/png",
            AdditionalData = new Dictionary<string, object>()
            {
                {"@microsoft.graph.temporaryId", "1"}
            }
        },
        new ChatMessageHostedContent
        {
            ContentBytes = Encoding.ASCII.GetBytes("iVBORw0KGgoAAAANSUhEUgAAA..."),
            ContentType = "image/png",
            AdditionalData = new Dictionary<string, object>()
            {
                {"@microsoft.graph.temporaryId", "2"}
            }
        }
    }
};

await graphClient.Teams["{id}"].Channels["{id}"].Messages
    .Request()
    .AddAsync(chatMessage);

谁能帮我解决这个问题?

【问题讨论】:

  • 您正在尝试将对象列表转换为单个对象。您需要获取实现该接口的类 (IChatMessageHostedContentsCollectionPage),创建它的新实例,并将列表添加到页面上包含项目的任何属性(通常在图形 SDK 上称为 CurrentPage
  • @GuruStron,我已经更新了上面的代码,请帮忙。
  • @Pravin ,我们无法重现该问题,代码工作正常。能否请您简单介绍一下这个问题。
  • VaraPrasad-MSFT,感谢您的回复。其实这个问题已经解决了。但实际问题在下面发布。请帮忙。 stackoverflow.com/questions/64028614/…

标签: c# microsoft-graph-api microsoft-teams microsoft-graph-sdks microsoft-graph-teams


【解决方案1】:

终于,问题已经解决了。

工作代码

IChatMessageHostedContentsCollectionPage chatMessageHostedContentsCollectionPage = new ChatMessageHostedContentsCollectionPage();

string imageBase64Text = Convert.ToBase64String(System.IO.File.ReadAllBytes(currentImageFullPath));

ChatMessageHostedContent chatMessageHostedContent = new ChatMessageHostedContent
{
    ContentBytes = Encoding.ASCII.GetBytes(imageBase64Text),
    ContentType = "image/jpeg",
    AdditionalData = new Dictionary<string, object>()
    {
        {"@microsoft.graph.temporaryId", "1"}
    },
};

chatMessageHostedContentsCollectionPage.Add(chatMessageHostedContent);

//Its working fine with the below code.
chatMessage.HostedContents = chatMessageHostedContentsCollectionPage;

 ChatMessage sentMessage = await graphClient.Teams["{id}"].Channels["{id}"].Messages
    .Request()
    .AddAsync(chatMessage);

请注意,以下代码集仍然存在相同的问题:

List<ChatMessageHostedContent> chatMessageHostedContents = new List<ChatMessageHostedContent>();

chatMessageHostedContents.Add(chatMessageHostedContent);//chatMessageHostedContent is used from above **working code**.

//Below statement actually gives the exception.
chatMessage.HostedContents = (IChatMessageHostedContentsCollectionPage) chatMessageHostedContents;

虽然工作代码已解决此异常,但在 Microsoft Teams 中发布图像和文本时,Microsoft Graph Beta API 下 ChatMessage 的 HostedContents 属性仍然存在一些问题。它为我们提供了上述工作代码的以下异常。

代码:BadRequest 消息:发送的请求正文无效。内部错误: AdditionalData: 日期: 2020-09-24T19:18:55 request-id: 298a3ea5-827f-4375-a0d5-ee619dd834a6 客户端请求 ID: 298a3ea5-827f-4375-a0d5-ee619dd834a6 ClientRequestId: 298a3ea5-827f-4375-a0d5-ee619dd834a6

我什至尝试过使用下面的 sn-p,但它给出了“未知错误”异常,通常在 Microsoft Graph API 不可用时发生。

await graphClient.Teams["{id}"].Channels["{id}"].Messages[sentMessage.Id].HostedContents
        .Request()
        .AddAsync(chatMessageHostedContent);

下面的问题指的是同一个问题。

Unable to post inline images along with the message in Microsoft Teams via Microsoft Graph API using C#

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 2021-12-06
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    相关资源
    最近更新 更多