@@ -15,43 +15,21 @@ public static class MongoDBBuilderExtensions
15
15
{
16
16
private const int DefaultContainerPort = 27017 ;
17
17
18
- /// <summary>
19
- /// Adds a MongoDB container to the application model. The default image is "mongo" and the tag is "latest".
20
- /// </summary>
21
- /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param>
22
- /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>
23
- /// <param name="port">The host port for MongoDB.</param>
24
- /// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
25
- public static IResourceBuilder < MongoDBContainerResource > AddMongoDBContainer (
26
- this IDistributedApplicationBuilder builder ,
27
- string name ,
28
- int ? port = null )
29
- {
30
- var mongoDBContainer = new MongoDBContainerResource ( name ) ;
31
-
32
- return builder
33
- . AddResource ( mongoDBContainer )
34
- . WithManifestPublishingCallback ( context => WriteMongoDBContainerToManifest ( context , mongoDBContainer ) )
35
- . WithAnnotation ( new EndpointAnnotation ( ProtocolType . Tcp , port : port , containerPort : DefaultContainerPort ) ) // Internal port is always 27017.
36
- . WithAnnotation ( new ContainerImageAnnotation { Image = "mongo" , Tag = "latest" } ) ;
37
- }
38
-
39
18
/// <summary>
40
19
/// Adds a MongoDB resource to the application model. A container is used for local development.
41
20
/// </summary>
42
21
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param>
43
22
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>
23
+ /// <param name="port">The host port for MongoDB.</param>
44
24
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
45
- public static IResourceBuilder < MongoDBServerResource > AddMongoDB (
46
- this IDistributedApplicationBuilder builder ,
47
- string name )
25
+ public static IResourceBuilder < MongoDBServerResource > AddMongoDB ( this IDistributedApplicationBuilder builder , string name , int ? port = null )
48
26
{
49
27
var mongoDBContainer = new MongoDBServerResource ( name ) ;
50
28
51
29
return builder
52
30
. AddResource ( mongoDBContainer )
53
31
. WithManifestPublishingCallback ( WriteMongoDBServerToManifest )
54
- . WithAnnotation ( new EndpointAnnotation ( ProtocolType . Tcp , containerPort : DefaultContainerPort ) ) // Internal port is always 27017.
32
+ . WithAnnotation ( new EndpointAnnotation ( ProtocolType . Tcp , port : port , containerPort : DefaultContainerPort ) ) // Internal port is always 27017.
55
33
. WithAnnotation ( new ContainerImageAnnotation { Image = "mongo" , Tag = "latest" } ) ;
56
34
}
57
35
@@ -61,7 +39,7 @@ public static IResourceBuilder<MongoDBServerResource> AddMongoDB(
61
39
/// <param name="builder">The MongoDB server resource builder.</param>
62
40
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>
63
41
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
64
- public static IResourceBuilder < MongoDBDatabaseResource > AddDatabase ( this IResourceBuilder < IMongoDBParentResource > builder , string name )
42
+ public static IResourceBuilder < MongoDBDatabaseResource > AddDatabase ( this IResourceBuilder < MongoDBServerResource > builder , string name )
65
43
{
66
44
var mongoDBDatabase = new MongoDBDatabaseResource ( name , builder . Resource ) ;
67
45
@@ -77,7 +55,7 @@ public static IResourceBuilder<MongoDBDatabaseResource> AddDatabase(this IResour
77
55
/// <param name="hostPort">The host port for the application ui.</param>
78
56
/// <param name="containerName">The name of the container (Optional).</param>
79
57
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
80
- public static IResourceBuilder < T > WithMongoExpress < T > ( this IResourceBuilder < T > builder , int ? hostPort = null , string ? containerName = null ) where T : IMongoDBParentResource
58
+ public static IResourceBuilder < T > WithMongoExpress < T > ( this IResourceBuilder < T > builder , int ? hostPort = null , string ? containerName = null ) where T : MongoDBServerResource
81
59
{
82
60
containerName ??= $ "{ builder . Resource . Name } -mongoexpress";
83
61
@@ -96,6 +74,7 @@ private static void ConfigureMongoExpressContainer(EnvironmentCallbackContext co
96
74
var hostPort = GetResourcePort ( resource ) ;
97
75
98
76
context . EnvironmentVariables . Add ( "ME_CONFIG_MONGODB_URL" , $ "mongodb://host.docker.internal:{ hostPort } /?directConnection=true") ;
77
+ context . EnvironmentVariables . Add ( "ME_CONFIG_BASICAUTH" , "false" ) ;
99
78
100
79
static int GetResourcePort ( IResource resource )
101
80
{
@@ -109,7 +88,12 @@ static int GetResourcePort(IResource resource)
109
88
}
110
89
}
111
90
112
- private static void WriteMongoDBContainerToManifest ( this ManifestPublishingContext context , MongoDBContainerResource resource )
91
+ public static IResourceBuilder < MongoDBServerResource > PublishAsContainer ( this IResourceBuilder < MongoDBServerResource > builder )
92
+ {
93
+ return builder . WithManifestPublishingCallback ( context => WriteMongoDBContainerToManifest ( context , builder . Resource ) ) ;
94
+ }
95
+
96
+ private static void WriteMongoDBContainerToManifest ( this ManifestPublishingContext context , MongoDBServerResource resource )
113
97
{
114
98
context . WriteContainer ( resource ) ;
115
99
context . Writer . WriteString ( // "connectionString": "...",
0 commit comments