Send a class, not just a class interface, between servers? - c#

I am creating a grid application which requires me to send software packets that wrap a class out to participating nodes within my grid.
The first idea that I came up with was to have the master nodes contact each node (running a Windows service) and send an assembly containing a class which adheres to a common interface along with a .config file containing, well, configuration information.
Is there a better way to do this? Aside from the discussion on whether this should be a push or a pull, what's the best way to get what is effectively a software update out? It would be great if I could use something similar to a WCF service called by the client nodes, but that would, of course, leave the real processing on the master node, which would effectively uncluster my cluster.

You can send the assembly bytes and load them directly into the runtime. It work well for managed code. I'm not aware of a better way to do this. And yes, use a push model :)

This seems like a valid approach. This is essentially how most .NET plugin models work.
The only other approach that I can think of would be sent script files back and forth. You could make your own custom DSL, but I think that that would be overkill. Using something like Iron Ruby or Iron Python would be a lot simpler, as well as a lot more powerful. Another thought would be to send PowerShell scripts.
I have never tried this, but it's also possible to send C# or VB files back and forth and then compile them when they need to be run. However, I see little advantage of this over the previous two suggestions.

Related

C# intercept file reads

I have a large bespoke container file (~3TB) in size, and another application that needs to read from it.
However, the application doesn't understand the structure of the container, so I have to convert it first, which means creating another ~3TB file; I'm hoping to streamline this process.
What I'd like to do is to create a file/pipe/something on the file system, and when the other applications reads from it, my application simply returns the correct data from within the container.
I'm not sure if this can be done in C# and I don't really want to have to hook any OS components, so I was thinking that a named pipe might work, but I'm not sure, if anyone has any suggestions or ideas, I'd appreciate it.
If you don't control the consuming application and it expects to be reading from the file system, there may be a way of doing this but it's a fair bit of work.
Recent releases of Windows 10 have included the Windows Projected File System. Windows takes care of all of the file system interception and you just have to be able to answer questions like "what files are meant to be in this directory?" and the like. I believe it's now used for OneDrive and that's one of the intended uses - where the actual files may normally reside in cloud storage rather than locally.
You do have to make file content available as Windows demands it. The one thing to say though is that it's not an easy job direct from C#. If you're going to try binding to this API, it really helps if you understand a bit of C or C++ too.
Earlier this year I was looking to create a managed binding to this API to make consumption easier from .NET languages. It's not, however, currently in a releasable state. But the basics worked and proves that this is a viable approach.
Once .NET Core 3 is fully released I'll probably dust this off again and make it work well, but for now it's a WIP

I'm making a JavaScript extension for a site. I'd like to use Java or C# instead. Is it possible?

This is hard for me to explain and even harder for me to visualize how I'd do it, since I don't know the bounds of communication with websites with Java and C#, so if I banter a lot/make no sense in the process of describing this, I apologize.
Basically, I'm making a 'bot' for www.plug.dj. This bot is able to do things on command like kick users, ban users, send chat messages, delete chat messages, say random things, etc. As of right now, it's powered by a simple one-file JavaScript code with a ton of listeners and callbacks using the Plug.dj API to handle them. This is ALL engineered by JS -- on the back-end, I think Plug.dj is powered by Python, I could be wrong.
Anyway, what I would LIKE to do is create this bot on a language other than JS. It's really basic and not super powerful, and there are things like communicate with databases and such that I'd like to implement that aren't possible/convenient with JS. I just want to know if this is possible, and if so, where should I start looking?
I'd prefer a language like Java or C#. If there's any more info you need to know in order to answer this, let me know, please! I'd like to start working on this, I think it'd be fun to learn how to communicate with websites with Java/C#/whatever.
If the bot javascript runs on "their" server, then there is no simple solution. They are providing a mechanism for running "your" javascript on their server, but the chances are that they don't support other languages. (And the only way to find out would be to ask "them"). Assuming that the answer is "no", you would need to investigate whether you can implement your "bot"s functionality in client-side code; e.g. a custom client that you implemented from scratch in Java or C# or whatever. That's a big "if" ... because it will only work if they expose the server functionality you need in their external APIs.
OTOH, if the bot javascript runs on "your" server, then you should be able to change it to support other languages. (It wouldn't necessarily be easy though ...)
My advice would be to take a deep breath ... and stick with Javascript. We all have to use languages that we don't think are "fun".
I honestly would just leave it in javascript if it is something you need to have run in the client.
If you need to make database calls, you can introduce a web services layer in between against which you can make AJAX calls which interact with databases.
I think your perception of javascript as basic and not very powerful is not a very good one. They are very complex apps build today in just javascript and HTML5. You just might need to start looking at things like backbone.js, underscore.js, and similar libraries that can help provide more advanced code organization functionality available to you.
If however you are looking at building something that individual clients are not going to have installed in their browsers, but rather would just interact with the website in an automated admin, then certainly you can establish you own web service in whatever language you like to interact with their API's and perform admin tasks.
If they provide a JavaScript library that runs client side, it seems likely that it will be communicating with the server over HTTP. Therefore, it should be possible for you to analyse the library and the calls it makes to reverse engineer the server API (which would be the HTTP calls) and re-implement it in the language of your choice.
Looking at the code of bot.js:
https://github.com/backus/Plug.DJ-Javascript-Chatbot/blob/master/bin/bot.js
it seems everything comes down to calls against their API object, eg API.getDJs(), API.getWaitList() etc. If you can determine how this API object works, then you might be able to reverse engineer and re-implement.

Implementing an SNMP Reporter (agent) in C# (must run on Mono)?

I am attempting to implement some additional statistics gathering in a C# server application - I have about 20 or so variables I'd like to be able to report to network monitoring tools; so I am assuming (hopefully correctly) that SNMP is the correct way to go.
There are however two problems:
The application is an open source server that cant include proprietary components, and
It needs to run on Mono under *nix environs as well as Windows via .NET.
The "#SNMP" library at http://sharpsnmplib.codeplex.com/ appears to be a promising solution - but there are no samples I can find; and my knowledge of SNMP is lacking.
Does anyone here have any (quick) examples of reporting data via SNMP? (ideally using a library under the LGPL, X/MIT, BSD or Zlib licenses)
Help much appreciated
Edit:
The problem appears to be a lack of libraries capable of acting as SNMP servers, if anyone is aware of any - this would probably solve the problem nicely.
Edit #2:
Basically I'm looking for a SNMP server library or sample reference implementation.
As the author :) I suggest you check out TestAgent sample. It can send out TRAP or INFORM messages to the IP address you specify.
Note that this is a tiny sample, which does not reveal all powers of SNMP.
#SNMP source comes with some samples (they're actually tests, but one can have an idea about how it works).
Another thing you could try is send your snmp queries calling directly the snmpget executable with the Process class. It's not the best way, but it could work (I've used this kind of approach with another language).

How can I reduce maintenance when my JavaScript and C# codebases overlap?

I have a library with some objects that I use both from C# and JavaScript. There are several objects, let's call one of them Foo, with the same basic implementation in C# and JavaScript. When I want to transmit an object of this type from the server to the browser, I simply call Foo.ToJson() to serialize this object to JSON and then revive the object on the browser side with a safe eval() operation.
This is all well and good, but the library is becoming complex and the need to keep the JavaScript and C# code bases synchronized is increasingly difficult and error-prone. I'm interested in ideas for simplifying this architecture or making it easy to maintain. Thoughts?
You could look at using script#: http://projects.nikhilk.net/ScriptSharp
Since your classes are all coded in C#, you could reuse the same .cs file for both the server and client projects. that way, when you make changes, it's automatically compiled into the javascript :-)
have you considered making your "shared code" into a web service, this way any of your applications can access it, and you can make everything distributed to help with performance.
Just one solution.
http://en.wikipedia.org/wiki/Web_services
Make the parts of your app that overlap as data-driven as possible. At a certain size, it's easier to have a state-machine interpreter running on both sides that can parse the same data.
Write the code once. There are a couple C# to JavaScript compilers you can look into. Script# and jsc.
Use DTOs. Data Transfer Objects.
This way if your JScript or C# objects change, your DTOs don't necessarily have to change unless that property is immediately required at the client. And even then, you have a clear separation of what is intended to be Serialized and what is not.
Dave Ward at Encosia speaks a lot about this.

Light weight HTTP Server library in .NET

I'm looking for a small and fast library implementing an HTTP server in .NET
My general requirements are:
Supports multiple simultaneous connections
Only needs to support static content (no server side processing)
HTTP only, HTTPS not needed
Preferably be able to serve a page from an in memory source. I want to integrate it into another app to be able to make changing data available via a browser, but I don't want to have to write it to a file on disk first. For example, just pass it a C# string to use as the current page content.
Preferably open source so I can modify it if needed
Definitely needs to be free... it's for a personal project with no budget other than my own time. I also want to be able to release the final product that would use this library freely (even if that means complying to the particular OSS license of that library.
Edit: To clarify some more, what I need can be REALLY simple. I need to be able to serve essentially 2 documents, which I would like to be served directly from memory. And that's it. Yes, I could write my own, but I wanted to make sure I wasn't doing something that was already available.
Use Cassini.
Free, Open Source.
It would take trivial hacking to serve from memory.
Well, how complicated of a HTTP server do you need? .NET 2.0 has the HttpListener Class which you can use to roll your own basic library. Since this is for a personal project and you are willing to invest the time, it would also make for a good learning experience as you you would get to learn how to work with the class. Additionally, according to the MSDN documentation, it has an asynchronous mode that gives each request its own thread.
Getting a basic HTTP server with the class up and running isn't too difficult either, you should be able to get it done in only a couple hundred lines of code.
Check out Kayak.
Note: kayak doesn't seem to be maintained anymore - though it deserves to be so
LightHTTP is an open-source library I've created that does exactly what you need.
It can be used in testing and mocking, or other scenarios where a lightweight HTTP server is preferred.
It works asynchronously.
Supports simultaneous connections.
It can serve anyway you'd need, since it's based on HttpListener.

Categories