How do I use projections in Embedded mode

I’m running embedded mode for integration testing. I can append events and subscribe to individual streams. However, I can’t subscribe to a category stream. I get an AccessDeniedException: Read access denied for stream ‘$ce-test’.

I have enabled projections:

EmbeddedVNodeBuilder.AsSingleNode().RunProjections(ProjectionsMode.All).StartStandardProjections()

.OnDefaultEndpoints().RunInMemory().Build()

``

I’ve also tried explicitly passing user credentials or as default on the connection setting, but then I get an Authentication Error. What am I doing wrong?

Hi Jonathan,
I have actually been playing with that very same scenario and just so happen to have a bit of code to share with you.

Let me know if this works for you?

public class Program

{

public static void Main()

{

var eventStore = StartEmbeddedEventStore();

var defaultCredentials = new UserCredentials(“admin”, “changeit”);

var connectionSettings = ConnectionSettings.Create()

.SetDefaultUserCredentials(null) //replace with

//.SetDefaultUserCredentials(defaultCredentials)

.KeepReconnecting();

using (var connection = EmbeddedEventStoreConnection.Create(eventStore, connectionSettings))

{

connection.ConnectAsync().Wait();

var sub = connection.SubscribeToStreamFrom("$ce-test", null, true,

(_, evt) => Console.WriteLine(“Event Appeared: {0}”, evt.Event.EventType),

subscriptionDropped: (_, reason, e) => Console.WriteLine(“Subscription Dropped, Reason : {0}/{1}”, reason, e.InnerException.Message),

userCredentials: null);

//userCredentials: defaultCredentials);

connection.AppendToStreamAsync(“test-account”,

ExpectedVersion.Any,

GetEventDataFor(100)).Wait();

Console.WriteLine("-------------------------------");

Console.WriteLine(“Embedded Event Store is now running with Projections Enabled.”);

Console.WriteLine(“You are able to issue a curl request to http://localhost:2113/stats as well as http://localhost:2113/projections/any”);

Console.WriteLine();

Console.WriteLine(“Press any key to kill the node.”);

Console.WriteLine("-------------------------------");

Console.ReadLine();

sub.Stop();

connection.Close();

}

eventStore.Stop();

}

static ClusterVNode StartEmbeddedEventStore()

{

var embeddedEventStore = EmbeddedVNodeBuilder.AsSingleNode()

.RunInMemory()

.RunProjections(ProjectionsMode.All)

.OnDefaultEndpoints()

.StartStandardProjections()

.AddExternalHttpPrefix(“http://*:2113/”)

.Build();

var startedEvent = new ManualResetEventSlim(false);

embeddedEventStore.MainBus.Subscribe(

new AdHocHandler<UserManagementMessage.UserManagementServiceInitialized>(m => startedEvent.Set()));

embeddedEventStore.Start();

if (!startedEvent.Wait(60000))

throw new TimeoutException(“Embedded Event Store has not started in 60 seconds.”);

return embeddedEventStore;

}

private static EventData GetEventDataFor(int i)

{

return new EventData(

Guid.NewGuid(),

“DepositMade”,

true,

Encoding.ASCII.GetBytes("{“somedata” : " + i + “}”),

Encoding.ASCII.GetBytes("{“metadata” : " + i + “}”)

);

}

}

``

Thanks for the code, but I get a similar error when running this:

Subscription Dropped, Reason : CatchUpError/Read access denied for stream ‘$ce-test’

``

or did you replace userCredentials: null with userCredentials: defaultCredentials in the subscription call?

For the default user credentials to work in the connection settings, you have to be using the latest Client which is 3.3.1

Ah, ok. Yes now it works. Thanks. I can’t immediately see what you are doing differently. I will investigated.

OK, when I add this part, it works:

var startedEvent = new ManualResetEventSlim(false);

embeddedEventStore.MainBus.Subscribe(new AdHocHandler<UserManagementMessage.UserManagementServiceInitialized>(m => startedEvent.Set()));
embeddedEventStore.Start();

if (!startedEvent.Wait(60000))

throw new TimeoutException("Embedded Event Store has not started in 60 seconds.")

``

I was using:

let nodeTcs = TaskCompletionSource()

clusterVNode.NodeStatusChanged.Add(fun x -> if x.NewVNodeState.Equals(VNodeState.Master) then nodeTcs.SetResult(true))

clusterVNode.Start()

``

It’s probably not quite ready at that point. Thanks a lot!