# IResourceWithCustomWithReference<TSelf> Methods

- Package: [Aspire.Hosting](/reference/api/csharp/aspire.hosting.md)
- Type: [IResourceWithCustomWithReference<TSelf>](/reference/api/csharp/aspire.hosting/iresourcewithcustomwithreference-1.md)
- Kind: `Methods`
- Members: `1`

Defines custom `WithReference` dispatch behavior for a resource type.

## TryWithReference(IResourceBuilder<TDestination>, IResourceBuilder<IResource>, string?, bool, string?)

- Name: `TryWithReference(IResourceBuilder<TDestination>, IResourceBuilder<IResource>, string?, bool, string?)`
- Modifiers: `static` `abstract` `nullable`
- Returns: `IResourceBuilder<TDestination>`

Applies a reference from `source` to `builder` using resource-specific behavior.

```csharp
public interface IResourceWithCustomWithReference<TSelf>
{
    public static abstract IResourceBuilder<TDestination>? TryWithReference<TDestination>(
        IResourceBuilder<TDestination> builder,
        IResourceBuilder<IResource> source,
        string? connectionName = null,
        bool optional = false,
        string? name = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<TDestination>`)
  The destination resource builder.
- `source` (`IResourceBuilder<IResource>`)
  The source resource builder.
- `connectionName` (`string?`) `optional`
  An optional connection string override used by connection-string-based references.
- `optional` (`bool`) `optional`
  `true` to allow a missing connection string; otherwise, `false`.
- `name` (`string?`) `optional`
  An optional service discovery name override used by service-based references.

## Returns

`IResourceBuilder<TDestination>` -- The destination `ApplicationModel.IResourceBuilder`1` when handled; otherwise, `null`.
