Creating projection with .net

Hi,

Anybody tried creating a projection with .net?

var client = new RestClient(“http://127.0.0.1:2113/projections/transient”);

client.Authenticator = new HttpBasicAuthenticator(“admin”,“changeit”);

var request = new RestRequest(Method.POST);

request.AlwaysMultipartFormData = true;

string projStr = “fromCategory(‘12-testentity’).foreachStream().whenAny(function(s, e) { linkTo(‘test-12’, e) })”;

request.AddParameter(“enabled”, “yes”, ParameterType.QueryString);

var filePara = FileParameter.Create(string.Empty, Encoding.UTF8.GetBytes(projStr), string.Empty, “application/json”);

request.Files.Add(filePara);

var response = client.Execute(request);

when({$any : function(s,e) {}})

Hi,

Make sure that the first event this query receives is syntactically correct JSON.

-yuriy

I got exactly the same error with “when({$any : function(s,e) {}})”

http://127.0.0.1:2113/projection/aafbcc54-5c93-48a2-b6f7-e3f4df701cf4

{

  "version": -1,
  "epoch": -1,
  "effectiveName": "aafbcc54-5c93-48a2-b6f7-e3f4df701cf4",
  "writesInProgress": 0,
  "readsInProgress": 0,
  "partitionsCached": 0,
  "status": "Faulted",
  "stateReason": "SyntaxError: Unexpected token :",
  "name": "aafbcc54-5c93-48a2-b6f7-e3f4df701cf4",
  "mode": "Transient",
  "position": "",
  "progress": 0.0,
  "eventsProcessedAfterRestart": 0,
  "statusUrl": "[http://127.0.0.1:2113/projection/aafbcc54-5c93-48a2-b6f7-e3f4df701cf4](http://127.0.0.1:2113/projection/aafbcc54-5c93-48a2-b6f7-e3f4df701cf4)",
  "stateUrl": "[http://127.0.0.1:2113/projection/aafbcc54-5c93-48a2-b6f7-e3f4df701cf4/state](http://127.0.0.1:2113/projection/aafbcc54-5c93-48a2-b6f7-e3f4df701cf4/state)",
  "resultUrl": "[http://127.0.0.1:2113/projection/aafbcc54-5c93-48a2-b6f7-e3f4df701cf4/result](http://127.0.0.1:2113/projection/aafbcc54-5c93-48a2-b6f7-e3f4df701cf4/result)",
  "bufferedEvents": 0,
  "writePendingEventsBeforeCheckpoint": 0,
  "writePendingEventsAfterCheckpoint": 0
}

Hi Yuriy,

My json is correct, I can use it to create new projection without the api.

-Eranda

When you add a filepRemeter with this testclient what does the http request look like?

To confirm the projection contents should be in the body of the request not as a multi-part type thing

“To add a file for upload, use AddFile() (request will be sent as multipart encoded form). To include a request body (like XML or JSON), use AddBody();”

Had a few minutes during lunch so here you go:

class Program

{

static void Main(string[] args)

{

var client = new RestClient(“http://127.0.0.1:2113/projections/transient”);

client.Authenticator = new HttpBasicAuthenticator(“admin”, “changeit”);

var request = new RestRequest(Method.POST);

request.AlwaysMultipartFormData = false;

string projStr = “fromCategory(‘12-testentity’).foreachStream().whenAny(function(s, e) { linkTo(‘test-12’, e) })”;

request.AddParameter(“enabled”, “yes”, ParameterType.QueryString);

request.AddParameter(“text/json”, projStr, ParameterType.RequestBody);

request.AddHeader(“ContentType”, “application/json”);

var response = client.Execute(request);

}

}

Then check content-location for the status uri (you can get to everything else from there). For more information see esquery on github https://github.com/EventStore/EventStore/blob/dev/src/EventStore/esquery/CommandProcessor.cs or padmin.

Also many of these methods are exposed in the C# client API (padmin uses them). We have been considering making typed objects on top if this is something that would interest you.

Cheers,

Greg

Thanks Greg and thanks James.This solved my issue.
Typed objects on top with C# client API will be cool indeed! appreciate it.
-Eranda

Hi Greg,

I was playing the things around with C# client API, So this is what I got

private static void Main(string[] args)

{

var projectionsManager = new ProjectionsManager(new ConsoleLogger(), new IPEndPoint(IPAddress.Loopback, 1113));

const string projStr =

“fromCategory(‘12-testentity’).foreachStream().whenAny(function(s, e) { linkTo(‘test-12’, e) })”;

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

projectionsManager.CreateContinuous("$test-12", projStr, credentials);

projectionsManager.Enable("$test-12", credentials);

Console.ReadLine();

}

Which ended me with the following exception,

“The server committed a protocol violation. Section=ResponseStatusLine”

Am I missing something?

Not necessarily, which client and server versions are you using?

Hi James,

My server was ver 2.0.1

and my client ver 2.0.2, Same error with client ver 2.0.1 too

I think those Apis have been fixed over time! There’ll be a binary release today or tomorrow where they definitely work together

Ok James, thanks for your response, I will try later today (or may be tomorrow) with new release and keep you updated.
-Eranda

Try port 2113 1113 is the tcp interface :slight_smile:

Doh, missed that one :slight_smile:

Oh… yes, thanks for pointing :slight_smile: