我有一个公共交通发布者和一个消费者(均在下方)。
发送的消息是
public class SendEmailContract
{
public List<Attachment> Attachments { get; internal set; }
public string AddressFrom { get; internal set; }
public string AddressTo { get; internal set; }
public string AddressCC { get; internal set; }
public string AddressBCC { get; internal set; }
public string Subject { get; internal set; }
public string Data { get; internal set; }
public string TemplateCode { get; internal set; }
public string EmailHtml { get; internal set; }
public bool WithoutTemplate { get; internal set; }
}
public class Attachment
{
public string Name { get; internal set; }
public string ContentType { get; internal set; }
public byte[] Content { get; internal set; }
}
电子邮件合同有一个附件列表。
当我运行示例发布者时,消费者会收到电子邮件合同及其附件。
当电子邮件合同位于 nuget 包中(即在单独的 DLL 中)时,会收到电子邮件合同,但附件为空。
问题 当合约位于单独的 DLL 中时,为什么附件字段为空? (问题可能很模糊,但我不知道如何进一步减少这个问题,任何帮助和想法将不胜感激)
下方发布者
using MassTransit;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace ConsolePublisher
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
cfg.Host("localhost", "/", h =>
{
h.Username("guest");
h.Password("guest");
});
});
var source = new CancellationTokenSource(TimeSpan.FromSeconds(10));
await busControl.StartAsync(source.Token);
var message = new SendEmailContract
{
AddressFrom = "[email protected]",
AddressTo = "[email protected]",
AddressCC = "[email protected],[email protected]",
AddressBCC = "[email protected],[email protected],[email protected]",
Subject = $"Test subject email {DateTime.Now}",
Data = @"<Data><CustomerName>NewReg NewReg</CustomerName><Barcode>bc12345678us</Barcode><URL>https://example.com</URL><ServiceTitle>Priority</ServiceTitle></Data>",
TemplateCode = "emailtemplate123",
EmailHtml = "emailhtml",
WithoutTemplate = false,
Attachments = new List<Attachment>
{
new Attachment
{
Name = "Attachment1.txt",
ContentType = "txt",
Content = new byte[] { 1,2,3,4,5,6},
},
new Attachment
{
Name = "Attachment2.txt",
ContentType = "txt",
Content = new byte[] { 2,2,3,4,5,7},
}
},
};
await busControl.Publish<SendEmailContract>(message);
await busControl.StopAsync();
}
}
public class SendEmailContract
{
public string AddressFrom { get; internal set; }
public string AddressTo { get; internal set; }
public string AddressCC { get; internal set; }
public string AddressBCC { get; internal set; }
public string Subject { get; internal set; }
public string Data { get; internal set; }
public string TemplateCode { get; internal set; }
public string EmailHtml { get; internal set; }
public bool WithoutTemplate { get; internal set; }
public List<Attachment> Attachments { get; internal set; }
}
public class Attachment
{
public string Name { get; internal set; }
public string ContentType { get; internal set; }
public byte[] Content { get; internal set; }
}
}
下方消费者
using GreenPipes;
using MassTransit;
using MassTransit.ConsumeConfigurators;
using MassTransit.Definition;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace ConsuleConsumer
{
public class Program
{
public static async Task Main(string[] args)
{
var services = new ServiceCollection();
services.AddMassTransit(x =>
{
x.AddConsumer<EmailConsumer>(typeof(EmailConsumerDefinition));
x.SetKebabCaseEndpointNameFormatter();
x.UsingRabbitMq((context, cfg) =>
{
cfg.Host("rabbitmq://localhost");
cfg.ConfigureEndpoints(context);
});
});
var serviceProvider = services.BuildServiceProvider();
var bus = serviceProvider.GetRequiredService<IBusControl>();
await bus.StartAsync();
Console.WriteLine("Press any key to exit");
await Task.Run(() => Console.ReadKey());
Console.WriteLine("Now stopping bus...");
await bus.StopAsync();
Console.WriteLine("Bus stopped");
Console.ReadKey();
}
}
public class EmailConsumer :
IConsumer<ConsolePublisher.SendEmailContract>
{
readonly ILogger<EmailConsumer> _logger;
public EmailConsumer ()
{
Console.WriteLine("starting email consumer");
}
public EmailConsumer(ILogger<EmailConsumer> logger)
{
_logger = logger;
}
public async Task Consume(ConsumeContext<ConsolePublisher.SendEmailContract> context)
{
_logger.LogInformation("Email:", context.Message.Data);
}
}
public class EmailConsumerDefinition :
ConsumerDefinition<EmailConsumer>
{
public EmailConsumerDefinition()
{
// override the default endpoint name
EndpointName = "email-service";
}
protected override void ConfigureConsumer(IReceiveEndpointConfigurator endpointConfigurator,
IConsumerConfigurator<EmailConsumer> consumerConfigurator)
{
endpointConfigurator.UseScheduledRedelivery(r => r.Intervals(TimeSpan.FromSeconds(10)));
endpointConfigurator.UseInMemoryOutbox();
}
}
}
namespace ConsolePublisher
{
public class SendEmailContract
{
public string AddressFrom { get; internal set; }
public string AddressTo { get; internal set; }
public string AddressCC { get; internal set; }
public string AddressBCC { get; internal set; }
public string Subject { get; internal set; }
public string Data { get; internal set; }
public string TemplateCode { get; internal set; }
public string EmailHtml { get; internal set; }
public bool WithoutTemplate { get; internal set; }
public List<Attachment> Attachments { get; internal set; }
}
public class Attachment
{
public string Name { get; internal set; }
public string ContentType { get; internal set; }
public byte[] Content { get; internal set; }
}
}
}
发布者使用的版本
消费者使用的版本