Need Samples for Mono Zeroconf - c#

I'm trying to make a simple concept demonstration app the code of which I may use in the future. Specifically I want to have an iPhone running an app that sends XML data to a Bonjour service. I already have an Objective-C app that runs on mac and does this, but I need to make one on Windows with a strong preference for Mono Zeroconf because the project may involve the use of the Mono framework later on.
To this end I need a working sample of using the Mono Zeroconf library. The examples I could find on the website are not sufficient. ALl that they allow me to do is to register a service, but nothing after that. Specifically I have this:
RegisterService service = new RegisterService();
service.Name = "AirControlServer";
service.RegType = "_http.tcp.";
service.ReplyDomain = "local.";
service.Port = 3689;
service.Register();
And now I have absolutely no clue what to do. If someone could link me to some sample code or something, that would be really nice.
Thank you.

The Zeroconf is only used for announcing or discovering. That is, if you have some kind of service (for example, http server), you can use Mono.Zeroconf to announce it. The provided service is beyond the scope of the library, you have to provide it as usual. In other words, you can use the library to either answer questions like "What is the IP address of my service provider?" or announce such services ("Tell them that X.X.X.X has a service on port YY).
For the announcing part, you've already found the example, you can also look at this question. For the discovering, look here (at the "Listen for and resolve services" part).

In the Mono.Zeroconf source code, there is a sub-directory MZClient which contains good example code ZeroconfClient.cs for both publishing and browsing.

Related

Running a .asmx Web service as a Windows Service

I'm a total novice on asking questions on this forum - I hope I ask this question correctly.
The problem I'm trying to tackle is this: I have a Visual Studio 2010 project which is for a Web Service. The class which does the work is defined in a ".asmx" file. The class inherits from the "System.Web.Services.WebService" class.
It runs happily on IIS on a server. Clients can make SOAP calls to the Web Service and it actions the requests and sends a response. That's all fine.
The problem is that I want it to run on a Windows 7 PC on which I can't install IIS. So I wish it to run as a Windows Service (if possible). The only "client" of the service will be the user on that PC.
In other words, I wish to run all the code that is in my .asmx as a Windows Service (if possible).
In an ideal world I'd be able to re-use my existing .asmx (if possible), and not have to copy and paste all my code across to a new Windows Service solution.
So my goal is to find the up-to-date way of running an .asmx Web Service as a Windows Service. I'm hoping someone could point me in the right direction on how I accomplish this.
I've done quite a bit of searching, and it looks as though this is very much possible to do. The problem is that a lot of solutions out there, appear to be slightly out of date. Or I've got my concepts rather mixed up.
Based on "How can I deploy my WCF Service without IIS?", I've had a look at a few things.
The closest I've come is this: "https://msdn.microsoft.com/en-us/library/ms751414.aspx". However, in this example, the actual class which does the work (the calculator) is actually contained in the service itself. I would have thought you could have separated them out. In other words, have the service create instances of a class that comes from my .asmx file.
The second closes I've come across is "https://msdn.microsoft.com/en-us/library/bb332338.aspx#msdnwcfhc_topic4", however this is .NET 3.0 and VS 2005. I'm also struggling to understand how to link my existing class into the service.
The other solutions I encounter suggest adding references to "Microsoft.Web.Services3.dll", "System.Web.dll", and "System.Web.Services.dll", although a few people seem to think this is called WSE which was updated to WCF in .net 4.0 and so I should not be using it.
Any pointers as to where I get started?
In an ideal world, you would switch to WCF, as it can easily self-host, host in a service, etc. And there are plenty of examples out there. It is easy enough to migrate the code from ASMX to WCF. But this is 3.5, as NetFX (WCF, WF and WPF were released under 3.5).
With ASMX, you need a server to host with. You can create a simple "server" with the HttpListener class. This may be heavy enough for your purpose.
https://msdn.microsoft.com/en-us/library/system.net.httplistener(v=vs.85).aspx
IIS now has a hostable core, which can be used with newer versions of .NET, but if you cannot install the newer versions and have to leave the client machines at 3.0, it is probably not your best option.
You may be able to dig up old source for Cassini, which was Microsoft's implementation that eventually became the hostable web core.
Back in 2008, someone snagged the server in VS, which might be an option. Note that the version on this page is for VS 2008, which means 4.0:
http://www.codeproject.com/Articles/23939/Light-IIS-Run-Asp-net-without-IIS
Ultidev has a server that is reported to self-host: http://ultidev.com/download/default.aspx
Hope one of these helps.
Just for completeness, here's how I solved this:
In the end I went with running IIS on client PCs and just continued using the asmx.
I found that converting the asmx to WCF to be quite tricky. To be precise, it was the thread handling within the asmx (thread handling coded by someone else), that I found to be quite hard to convert. To explain further the asmx which I was trying to convert, did all its work in a class which inherited from "System.Web.Services.WebService". It could therefore, create an "Application" instance, which it then used to do its thread management.
So rather than try and do the same thing, using "System.ServiceModel" (which doesn't allow me to create an "Application" instance), I thought it easier to just to use IIS on the client PCs. I figured this was a better use of time than recoding a fairly complex asmx. A bit defeatist I know.
Kind regards to all.

Has anyone had success implementing maxmind's free GeoLiteCity DB on a C# webform?

We have several international partners/websites that have complained about their traffic being "stolen" by our domestic site (located in US). I thought it best to implement a free geoip service to offer a redirect option to our international users to their appropriate site if they happen upon our domestic site.
After researching the free options, I found that MaxMind offers a complete package with an option for fairly accurate IPaddress data (ipV4 and ipv6 included). In fact, our management strongly suggests the use of MaxMind's implementation.
I recently downloaded MaxMind's free GeoLiteCity.dat and github's C# code for my domestic website. I found the documentation to be quite vague as it relates to implementing their C# code on anything but a console application.
Before I implemented it on my current website - in my development environment, I successfully tested the implementation of the database, and the API on a console application and then successfully on a aspx page for a mock-website. I am currently experiencing dreaded "Object reference not set to an instance of an object" errors in my development environment.
I've added the class library for MaxMind and also added reference to that class library.
Has anyone run into issues implementing MaxMind's data while using C# code? Has anyone successfully utilized MaxMind's data and C# code on a webform? I would appreciate any insight. If you can provide a walkthrough of the implementation process and perhaps some sample C# code that works (or worked) for you, I would truly appreciate it.
I had to specify a full file system path to get it to read the database!! I may have missed something but relative path didn't work no matter what!!
var reader = new DatabaseReader(#"C:\Users\[User Name]\documents\visual studio 2013\Projects\glocalmvc\glocalmvc\Controllers\GeoLite2_City.mmdb", MaxMind.Db.FileAccessMode.Memory);
var omni = reader.Omni("1.1.1.1"); // replace it with a real ip address
after it worked, it didn't return city name, only country!! what a waste of time! :(

WCF replacement for cross process/machine communication

I am working on a C# application that contains multiple windows services that will need to communicate with each other to pass data around. These services may be on the same machine but they could be remote. I looked into using WCF for this purpose but it seems like WCF is too heavy and has a lot of extra configuration that, to me, seems unnecessary (.NET 3.5 is a requirement here, I know that .NET 4 simplified this)
So my question is, what would be the best replacement to WCF, besides the deprecated .NET Remoting that provide this functionality?
I have been using PInvoke to access the Windows RPC runtime for nearly 8 years. It's wicked fast and very reliable as far as a transport goes. When combined with a fast serializer like protobuf-csharp-port the resulting communications are rock solid and very fast.
So to build this from the ground-up this requires three parts:
Google's Protocol Buffers (protobuf-csharp-port) for serialization.
My own CSharpTest.Net.RpcLibrary for the transport.
A bit of glue code to put them together from protobuf-csharp-rpc.
These are all available on NuGet in the following packages: Google.ProtocolBuffers, CSharpTest.Net.RpcLibrary, and Google.ProtocolBuffers.Rpc.
The following is a quick run-down on getting started:
define a set of messages and a service using the Google Protocol Buffer Language.
Once you have that defined you will run ProtoGen.exe to generate the service stubs and messages in C#. Be sure to add the "-service_generator_type=IRPCDISPATCH" to generate the correct service code.
Now that you have the generated source files add them to a project and reference the three assemblies from the packages listed above.
Lastly take a look at the sample client/server code on the protobuf-csharp-rpc project page. Replace the "SearchService" with your service name, and you should be ready to run.
Optionally change the configuration of the RPC client/server. The example shows the use of LRPC which is local-host only; however the DemoRpcLibrary.cs source file show TCP/IP and Named Pipes as well.
You can always email me (roger # my user name) for any further information or examples.
Update
I wrote a quick startup guide: WCF replacement for cross process/machine communication.
You may want to look into ZeroMQ, it's very lightweight and effective and comes with good C# bindings. (Typing this on my mobile so you'll have to google for it yourself for now, sorry).
Look at NFX Glue.
It is way faster than WCF for coupled systems.
Interprocess communication with Glue Blog
Benchmark
Code: https://github.com/aumcode/nfx

Calling C# .NET Service From C++ Application?

I have an old MFC app written in Visual Studio 6. This will at some point be rewritten in C# .NET. However, before then I have to write a couple of new Windows services for the existing application. The others were written in ATL. What I would prefer to do is write these new services in C# .NET so that when the rest of the application is rewritten, these don't need to be.
Is it going to be possible to call the interfaces on the libraries hosted in a .NET windows service from the old application? If so, could you please explain how.
Absolutely. You're looking for a feature of .NET called COM-Interop.
http://msdn.microsoft.com/en-us/library/kew41ycz%28v=vs.71%29.aspx
http://msdn.microsoft.com/en-us/magazine/cc163494.aspx
The second link has an ATL example.
EDIT:
Based on your feedback in the comments, let me expand on this...
Ah - you're right about the sample on that page.
The first link is really where you want to start for all the details. If you follow the links, you'll find this page:
"Exposing .NET Framework Components to COM"
http://msdn.microsoft.com/en-us/library/zsfww439%28v=vs.71%29.aspx
Essentially, it's just a matter of applying a series of attributes to your classes and properties, and then generating the appropriate registry entries on the client machine (which .NET has a tool to do - see: http://msdn.microsoft.com/en-us/library/bctyca52%28v=vs.71%29.aspx)
I've done this several times myself for .NET projects people needed to call from VC++ and/or VB6.
Some other links that might be of interest:
http://www.codeproject.com/KB/COM/nettocom.aspx <-- Perfect example of what you're trying to do.
http://www.codeproject.com/KB/COM/Universal_CCW.aspx
I've done this exact thing with an MFC-based C++ application in Visual Studio 2008 and a .NET-based C# Windows service.
First, if you have not created the C# Windows services yet, I've got a couple of tutorials for creating the basic framework. The first tutorial provides a step-by-step procedure for creating the service and writing events to an application-specific event log. The second tutorial shows how to modify the service to install and uninstall itself from the command line, which I find of great use.
Second, you need to decide how you are going to communicate between your MFC application and your Windows service. Any kind of inter-process communication (IPC) model will work - sockets, pipes, shared memory, WCF, etc. Since you are wanting to migrate to .NET anyway, I would recommend using Windows Communication Foundation (WCF), which is the way I've done it. Specifically, I chose the named pipe aspect of WCF for my communication method based on the chart shown here.
If you go down the WCF route, you'll benefit from the fact that the communication between application and service is .NET-based. Thus, when you move your application to .NET, the communication mechanism won't have to be rewritten. The trick in the meantime is getting your MFC application to use the WCF code. To do this, write the WCF client code in a .NET assembly using C#. Then, use a C++ dll to bridge the gap between your MFC code and the .NET assembly. I've got another tutorial with step-by-step instructions for how to do this.
Hope this helps.

Web UI for Custom C# App

I have a application written in C#, installed as a [WCF] windows service, which will be running on headless machines. I would like to provide a nice web UI for it, but I'm not sure how to host it. After looking at embedded web servers such as Cassini, none seem ready for this without a ton of web server code. I would like to find a package that would let me design in something like Silverlight. Also, I need to be able to provide a simple installer for customers that doesn't involve "go get IIS", etc.
I saw this post that seemed close, but didn't really answer any of my questions, except affirming that its possible. I'm really looking for specifics on how to make this happen.
Why go silver-light route unless you need very rich/flexible UI? I would suggest using WCF itself to serve HTML over HttpBinding. Have a look at few articles regarding the same:
http://blog.iserviceoriented.com/index.php/2010/01/23/building-a-basic-web-server-using-wcf/
How do I create a simple Web Server using WCF without the ServiceHost class?
REST support by System.ServiceModel.Web namespace should make the task even more simpler (than described in above articles).

Categories