URL mapping with C# HttpListener - c#

In the code below I am waiting for any call to the 8080 port.
public static void Main()
{
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://*:8080/");
listener.Start();
while(isRunning)
{
HttpListenerContext ctx = listener.GetContext();
new Thread(new Worker(ctx).ProcessRequest).Start();
}
}
Is it possible to map specific URL patterns to different behavior? I want achieve a REST-style server i.e. a call to localhost:8080/person/1 will launch getPersonHandler(int)
[Mapping("*:8080/person/$id")]
public void getPersonHandler(int id)
{
// ...
}
The Mapping syntax is just my wishful analogy to JAX-RS libraries that I know. I would like to do the same in C# (desktop C#, not asp).

You can get a similar effect without attributes
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://*:8080/");
listener.Start();
while (true)
{
HttpListenerContext ctx = listener.GetContext();
ThreadPool.QueueUserWorkItem((_) =>
{
string methodName = ctx.Request.Url.Segments[1].Replace("/", "");
string[] strParams = ctx.Request.Url
.Segments
.Skip(2)
.Select(s=>s.Replace("/",""))
.ToArray();
var method = this.GetType().GetMethod(methodName);
object[] #params = method.GetParameters()
.Select((p, i) => Convert.ChangeType(strParams[i], p.ParameterType))
.ToArray();
object ret = method.Invoke(this, #params);
string retstr = JsonConvert.SerializeObject(ret);
});
Usage would be:
http://localhost:8080/getPersonHandler/333
if you really want to use Attributes then
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://*:8080/");
listener.Start();
while (true)
{
HttpListenerContext ctx = listener.GetContext();
ThreadPool.QueueUserWorkItem((_) =>
{
string methodName = ctx.Request.Url.Segments[1].Replace("/", "");
string[] strParams = ctx.Request.Url
.Segments
.Skip(2)
.Select(s=>s.Replace("/",""))
.ToArray();
var method = this.GetType()
.GetMethods()
.Where(mi => mi.GetCustomAttributes(true).Any(attr => attr is Mapping && ((Mapping)attr).Map == methodName))
.First();
object[] #params = method.GetParameters()
.Select((p, i) => Convert.ChangeType(strParams[i], p.ParameterType))
.ToArray();
object ret = method.Invoke(this, #params);
string retstr = JsonConvert.SerializeObject(ret);
});
}
Then you can use as http://localhost:8080/Person/333 and your definitions would be
class Mapping : Attribute
{
public string Map;
public Mapping(string s)
{
Map = s;
}
}
[Mapping("Person")]
public void getPersonHandler(int id)
{
Console.WriteLine("<<<<" + id);
}

If you are working in .NET 4.0 or higher and looking for a pre-existing REST server solution that you can plug into (which it sounds like you are), you might want to check out Grapevine. You can get it using NuGet, and the project wiki has lots of sample code. Plus, it is open source, so if you just wanted to see how it can be accomplished, you can see all the source code there.
You can filter requests by path info (using regular expressions) and request methods (GET, POST, etc.).
I am the project author, and I had a similar need as the one you described. Using resources I found here and elsewhere, I built Grapevine so that I would have a solution in my back pocket whenever I needed it again (DRY).

Related

Akka.Net in-memory persistence calls `Recover` after app restart

I'am trying to test persistence actor, but behavior is wierd.
My tested actor:
public class PredictionManager : ReceivePersistentActor
{
public override string PersistenceId => _persistanceId;
public PredictionManager(string persistenceId)
{
_persistanceId = persistenceId;
Command<AddPredictionRequest>(OnPrediction);
Recover<SnapshotOffer>(x => OnRecover((PredictionManagerState)x.Snapshot), x => x.Snapshot is PredictionManagerState);
}
private void OnPrediction(AddPredictionRequest request)
{
/* some code */
_state.Add(request);
SaveSnapshot(_state);
}
private void OnRecover(PredictionManagerState state)
{
foreach(var request in state.RequestMap)
{
OnPrediction(request.Value);
}
}
}
My state save all messages and deletes them after manager actor recieve some message. When I try to debug my test, Recover function called first and after this called OnPrediction. My question is - how it's possible? If data stores in momory, why it have SnapshotOffer? Also I have tried to generate new percistenceId from Guid.NewGuid() but it doesn't work.
public void AddPrediction_PassToChild_CreateNewManager_PassToChild()
{
var sender = CreateTestProbe(Sys);
var persistanceId = "AddPrediction_PassToChild_CreateNewManager_PassToChild";
var props = Props.Create(() => new PredictionManager(Mock.Of<IEventBus>(), persistanceId));
var predictionManager = ActorOf(props);
var message = new PredictionManager.AddPredictionRequest(Props.Create(() => new ChildTestActor(sender.Ref)),
new StartPrediction<IPredictionParameter>("a", 1, "a", new Param() ));
//Act
predictionManager.Tell(message, sender);
sender.ExpectMsg<string>(x => x == "ok", TimeSpan.FromSeconds(15));
Sys.Stop(predictionManager);
predictionManager = Sys.ActorOf(props);
sender.ExpectMsg<string>(x => x == "ok", TimeSpan.FromSeconds(15));
Sys.Stop(predictionManager);
}
I found out that default storage for snapshots is LocalStorage not MemoryStorage. So it stores snapshots in files, and this is why it has SnapshotOffer after app restart. But I still can't get why Guid.NewGuid() as persistanceId is not working.

Any examples of getnextpage usage in the twilio api for c#?

The old code I've inherited for Twilio retrieves messages using the absolute PageNumber property of the MessageListRequest but according to the documentation this is obsolete and I should be using GetNextPage and GetPrevPage.
The API metadata shows this as obsolete with the message "Use GetNextPage and GetPreviousPage for paging. Page parameter is scheduled for end of life https://www.twilio.com/engineering/2015/04/16/replacing-absolute-paging-with-relative-paging".
Are there any examples of this usage? I couldn't find any in the documentation except in one of the API test methods and I'm not sure how well I can get to processing multiple pages with this example as a guide.
public class Foo : TwilioBase
{
public string Bar { get; set; }
}
public class FooResult : TwilioListBase
{
public List<Foo> Foos { get; set; }
}
[Test]
public void ShouldGetNextPage()
{
IRestRequest savedRequest = null;
FooResult firstPage = new FooResult();
firstPage.NextPageUri = new Uri("/Foos?PageToken=abc123", UriKind.Relative);
mockClient.Setup(trc => trc.Execute<FooResult>(It.IsAny<IRestRequest>()))
.Callback<IRestRequest>((request) => savedRequest = request)
.Returns(new FooResult());
var client = mockClient.Object;
var response = client.GetNextPage<FooResult>(firstPage);
mockClient.Verify(trc => trc.Execute<FooResult>(It.IsAny<IRestRequest>()), Times.Once);
Assert.IsNotNull(savedRequest);
Assert.AreEqual("/Foos?PageToken=abc123", savedRequest.Resource);
Assert.AreEqual(Method.GET, savedRequest.Method);
Assert.IsNotNull(response);
}
The old usage might look something like so:
var twilio = new TwilioRestClient(config.AccountSid, config.AuthToken);
var result = new List<Message>();
MessageResult tempResult;
int page = 0;
do
{
var request = new MessageListRequest();
request = new MessageListRequest { Count = 1000, DateSent = newestDate, DateSentComparison = ComparisonType.GreaterThanOrEqualTo, PageNumber = page++, To = config.FromNumber };
tempResult = twilio.ListMessages(request);
result.AddRange(tempResult.Messages);
} while (tempResult.NextPageUri != null);
Finally, I built the Twilio API 3.4.1.0 from the twilio-csharp GitHub project instead of NuGet since I need to update it to use the MessagingServiceSid which isn't included in the API yet.
Thanks for any pointers. I'll post a solution if I can figure it out on my own.
Actually, I got it to work now!
MessageResult messages = twilio.ListMessages(request);
do
{
if (messages.Messages != null)
{
foreach (var message in messages.Messages)
{
... process results
}
if (messages.NextPageUri != null)
{
messages = twilio.GetNextPage<MessageResult>(messages);
}
}
} while (messages.NextPageUri != null);
Did you try the example from the API Explorer?
https://www.twilio.com/console/dev-tools/api-explorer/sms/sms-mms-list
var twilio = new TwilioRestClient(AccountSid, AuthToken);
// Build the parameters
var options = new MessageListRequest();
var messages = twilio.ListMessages(options);
foreach (var message in messages.Messages)
{
Console.WriteLine(message.Body);
}
The helper library will automatically fetch from the API as you loop over the list until all records matching your criteria are processed.
You can limit the results with MessageListRequest.
Please give that a try and let me know how it goes.

Looking for a C# URL router, but not for HTTP

I am looking for a C# URL router component. Something very classic, taking inputs string such as /resources/:id/stuff and calling the appropriate methods. Something similar to the default ASP.net routing or RestfullRouting.
However, I am not using HTTP and I don't want a full HTTP stack/framework. I am looking for something light to route my MQTT messages.
Do you know if such a component exist?
The following non-optimized, not really defensively coded code parses URIs against routes:
public class UriRouteParser
{
private readonly string[] _routes;
public UriRouteParser(IEnumerable<string> routes)
{
_routes = routes.ToArray();
}
public Dictionary<string, string> GetRouteValues(string uri)
{
foreach (var route in _routes)
{
// Build RegEx from route (:foo to named group (?<foo>[a-z0-9]+)).
var routeFormat = new Regex("(:([a-z]+))\\b").Replace(route, "(?<$2>[a-z0-9]+)");
// Match uri parameter to that regex.
var routeRegEx = new Regex(routeFormat);
var match = routeRegEx.Match(uri);
if (!match.Success)
{
continue;
}
// Obtain named groups.
var result = routeRegEx.GetGroupNames().Skip(1) // Skip the "0" group
.Where(g => match.Groups[g].Success && match.Groups[g].Captures.Count > 0)
.ToDictionary(groupName => groupName, groupName => match.Groups[groupName].Value);
return result;
}
// No match found
return null;
}
}
It makes a few assumptions about the input (both routes and URIs), but basically it picks the :foo parameter names from the routes and makes named capture groups from that, which are matched against the input URI.
To be used like this:
var parser = new UriRouteParser(new []{ "/resources/:id/stuff" });
var routeValues = parser.GetRouteValues("/resources/42/stuff");
This will yield a dictionary of { "id" = "42" }, which you can then use as you like.
I quickly changed #CodeCaster's solution to attach and invoke delegates.
public class UriRouter
{
// Delegate with a context object and the route parameters as parameters
public delegate void MethodDelegate(object context, Dictionary<string, string> parameters);
// Internal class storage for route definitions
protected class RouteDefinition
{
public MethodDelegate Method;
public string RoutePath;
public Regex RouteRegEx;
public RouteDefinition(string route, MethodDelegate method)
{
RoutePath = route;
Method = method;
// Build RegEx from route (:foo to named group (?<foo>[a-z0-9]+)).
var routeFormat = new Regex("(:([a-z]+))\\b").Replace(route, "(?<$2>[a-z0-9]+)");
// Build the match uri parameter to that regex.
RouteRegEx = new Regex(routeFormat);
}
}
private readonly List<RouteDefinition> _routes;
public UriRouter()
{
_routes = new List<RouteDefinition>();
}
public void DefineRoute(string route, MethodDelegate method)
{
_routes.Add(new RouteDefinition(route, method));
}
public void Route(string uri, object context)
{
foreach (var route in _routes)
{
// Execute the regex to check whether the uri correspond to the route
var match = route.RouteRegEx.Match(uri);
if (!match.Success)
{
continue;
}
// Obtain named groups.
var result = route.RouteRegEx.GetGroupNames().Skip(1) // Skip the "0" group
.Where(g => match.Groups[g].Success && match.Groups[g].Captures.Count > 0)
.ToDictionary(groupName => groupName, groupName => match.Groups[groupName].Value);
// Invoke the method
route.Method.Invoke(context, result);
// Only the first match is executed
return;
}
// No match found
throw new Exception("No match found");
}
}
That can be used like this:
var router = new UriRouter();
router.DefineRoute("/resources/:id/stuff",
(context, parameters) => Console.WriteLine(parameters["id"] + " - " + context));
router.Route("/resources/42/stuff", "abcd");
That will print 42 - abcd on the standard output.

C# - Castle WCF Facility - How to properly setup client side OperationBehavior + WcfEndpoint.FromEndpoint usage

I was playing with wcf facility and trying to setup DataContractResolver but I couldn't find any example...
In the end I make it work .. not sure this is ok though ..
Questions
(code below) Is this the right way to accomplish behavior configuration or I am misunderstanding something?
also .. I had to disable async to make it work.. is it a library bug/issue?
Others nice to have before research ..
I was really thinking about performance impact of wcf facility interceptions.. vs benefits of use it. any thoughts?
why is this library not updated any more, (http://docs.castleproject.org/Windsor.AllPages.aspx?Cat=Windsor.WCF-Facility) my concern is regarded using a library that is not longer under maintenance?
var contractdesc = ContractDescription.GetContract(typeof(T));
if (dcr != null)
{
foreach (var operation in contractdesc.Operations)
{
operation.Behaviors.Find<DataContractSerializerOperationBehavior>().DataContractResolver = dcr;
}
}
var myEndpoint = WcfEndpoint.FromEndpoint(
new System.ServiceModel.Description.ServiceEndpoint(
contractdesc
, binding, new EndpointAddress(Url)));
var clientModel = new DefaultClientModel { Endpoint = myEndpoint };
clientModel.WithoutAsyncCapability();
container
.Register(
Types
.From(typeof(T))
.InSameNamespaceAs<T>()
.If(m => m.IsInterface)
.Configure(
c => c.Named(c.Implementation.Name)
.AsWcfClient(
clientModel
).Interceptors(typeof(CastleServiceInterceptor))
),
Component.For<CastleServiceInterceptor>()
);
So i realized (debugging wcf facility) that if WantsAsyncCapability= true the DefaultClientModel is not copying all ServiceEndpoint behaviors, just endpoint behaviors (true is de fault configuration) so here ..
public override ChannelFactory CreateChannelFactory(Type channelFactoryType, M clientModel,
params object[] constructorArgs)
{
if (!clientModel.WantsAsyncCapability)
{
return base.CreateChannelFactory(channelFactoryType, clientModel, constructorArgs);
}
EnsureValidChannelFactoryType(channelFactoryType);
ReplaceServiceEndpointAsyncContracts(constructorArgs);
var interceptor = new CreateDescriptionInterceptor();
var proxyOptions = new ProxyGenerationOptions(asyncChannelFactoryProxyHook);
return (ChannelFactory)generator.CreateClassProxy(
channelFactoryType, Type.EmptyTypes, proxyOptions, constructorArgs, interceptor);
}
Then in ReplaceServiceEndpointAsyncContracts is recreating ServiceEndpoint
private static void ReplaceServiceEndpointAsyncContracts(object[] constructorArgs)
{
for (int i = 0; i < constructorArgs.Length; ++i)
{
var endpoint = constructorArgs[i] as ServiceEndpoint;
if (endpoint != null)
{
var asyncEndpoint = new ServiceEndpoint(ContractDescription.GetContract(
AsyncType.GetAsyncType(endpoint.Contract.ContractType)))
{
Name = endpoint.Name,
Address = endpoint.Address,
Binding = endpoint.Binding,
ListenUri = endpoint.ListenUri,
ListenUriMode = endpoint.ListenUriMode
};
asyncEndpoint.Behaviors.Clear();
foreach (var behavior in endpoint.Behaviors)
{
asyncEndpoint.Behaviors.Add(behavior);
}
constructorArgs[i] = asyncEndpoint;
}
}
}
Above can be seen that not contracto or operation behavior is copied.
So thats it, thanks.

Combining latest from an observable of observables

Suppose I have a set of URIs that I am monitoring for availability. Each URI is either "up" or "down", and new URIs to monitor may be added to the system at any time:
public enum ConnectionStatus
{
Up,
Down
}
public class WebsiteStatus
{
public string Uri
{
get;
set;
}
public ConnectionStatus Status
{
get;
set;
}
}
public class Program
{
static void Main(string[] args)
{
var statusStream = new Subject<WebsiteStatus>();
Test(statusStream);
Console.WriteLine("Done");
Console.ReadKey();
}
private static void Test(IObservable<WebsiteStatus> statusStream)
{
}
}
Now suppose in Test() I want to reactively ascertain:
whether all URIs are down (as a bool)
which URIs are down (as IEnumerable<string>)
So Test would end up creating an observable like IObservable<Tuple<bool, IEnumerable<string>>> where the bool indicates whether all URIs are down and the IEnumerable<string> contains those URIs that are.
How do I go about this? My initial thinking is that I would need to group by the URI, then combine the latest from each group into a list that I could then perform a Select against. However, this did not work out due to the way CombineLatest works.
EDIT: Thanks to Matthew's answer I looked into rxx and found that it implemented a CombineLatest overload in exactly the fashion I would have expected in rx out of the box, except that I needed to change it so that it publishes even when there is only a single source stream being combined (by default it was waiting for a minimum of two source streams). Also, I can't justify pulling in an extra 2MB of binaries for the sake of one method, so I have copy/pasted it into my project. Doing so, I was able to solve as follows:
private static void Test(IObservable<WebsiteStatus> statusStream)
{
statusStream
.GroupBy(x => x.Uri)
.CombineLatest()
.Select(
x =>
{
var down = x.Where(y => y.Status == ConnectionStatus.Down);
var downCount = down.Count();
var downUris = down.Select(y => y.Uri).ToList();
return new
{
AllDown = x.Count == downCount,
DownUris = downUris
};
})
.Subscribe(x =>
{
Console.WriteLine(" Sources down ({0}): {1}", x.AllDown ? "that's all of them" : "some are still up", x.DownUris.Aggregate("", (y, z) => y += (z + " | ")));
});
}
The neatest way is to use the Rxx extension in this answer. An alternative is below, it just keeps a list of sites that are down/up.
var downStream = statusStream
.Aggregate<WebsiteStatus, IEnumerable<string>>(new string[0], (down, newStatus) =>
{
if (newStatus.IsUp)
return down.Where(uri => uri != newStatus.Uri);
else if (!down.Contains(newStatus.Uri))
return down.Concat(new string[] { newStatus.Uri });
else
return down;
});
var upStream = statusStream
.Aggregate<WebsiteStatus, IEnumerable<string>>(new string[0], (up, newStatus) =>
{
if (!newStatus.IsUp)
return up.Where(uri => uri != newStatus.Uri);
else if (!up.Contains(newStatus.Uri))
return down.Concat(new string[] { newStatus.Uri });
else
return up;
});
var allDown = upStream.Select(up => !up.Any());

Categories