﻿# Interfaces as messages

<!-- Version variant: core_9; default: [/nservicebus/messaging/messages-as-interfaces.md](/nservicebus/messaging/messages-as-interfaces.md) -->


Events can be created on the fly from interfaces without first defining an explicit class implementing the interfaces. This technique can be used to support multiple inheritance for [polymorphic routing scenarios](dynamic-dispatch-and-routing.md). In general, it is recommended to use [dedicated, simple types](/nservicebus/messaging/messages-events-commands.md) as messages instead.

## Sending interface messages

Interface messages can be sent using the following syntax:

<!-- snippet: InterfaceSend -->

```cs
await endpoint.Send<IMyMessage>(message =>
{
    message.SomeProperty = "Hello world";
});
```

<!-- endsnippet -->

Replies are supported via:

<!-- snippet: InterfaceReply -->

```cs
return context.Reply<IMyReply>(message =>
{
    message.SomeProperty = "Hello world";
});
```

<!-- endsnippet -->

## Publishing interface messages

Interface messages can be published using the following syntax:

<!-- snippet: InterfacePublish -->

```cs
return context.Publish<IMyEvent>(message =>
{
    message.SomeProperty = "Hello world";
});
```

<!-- endsnippet -->

## Creating interface messages with IMessageCreator

If an interface message is needed before calling `Send` or `Publish`, use `IMessageCreator` directly to create the message instance:

<!-- snippet: IMessageCreatorUsage -->

```cs
//IMessageCreator is available via dependency injection
async Task PublishEvent(IMessageCreator messageCreator)
{
    var eventMessage = messageCreator.CreateInstance<IMyEvent>(message =>
    {
        message.SomeProperty = "Hello world";
    });

    await messageSession.Publish(eventMessage);

    //or if on a message handler

    await context.Publish(eventMessage);
}
```

<!-- endsnippet -->
