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

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.

Related

Implementing a Web Service Server in Windows CE 2013

I have a smart device (Windows CE 2013) that just got the requirement to be controlled from a number of different devices, to do this, we decided to serve a web page from the device that allows it to be controlled.
I've also read that compact framework doesn't support ASP.NET so I'm intending to serve a static webpage that contains a javascript application that calls web services hosted on the device.
I also found this link explaining how to serve a SOAP Service in C++, but it seems old, and I'd prefer to do this in C# as it's my preffered language and I already have some code on it
I'm not quite happy with this solution, am I missing something? Can't I really do this in C#?
You could try this library for .NET https://github.com/ServiceStack/ServiceStack .
It is a free in case if you are using branch v3 form a github.
There's nothing built-in in the CF that provides web server capability. Windows CE has always shipped with an HTTPD server, but, honestly, it sucks and can't be integrated with managed code anyway.
We solved this problem long ago by creating our own IIS-like web server implementation (available commercially). You could do something similar as well - it's just a lot of Socket work. That trivializes the complexity of concurrent request handling and providing an IIS-like object model, but you get the idea. Basically you have to write the whole thing.
The main answer to this is to use Mongoose (or the MIT-licenced equivalent Civetweb). They are C webservers designed for just your task - an embedded web server.
They are really easy to implement, 1 C source file added to your project, give it an array of options, and you're done. See the examples. It has plenty of features and is fast, and small (40kb compiled!). They say it takes 5 minutes to get going with it, and they're right - I tend to use it to add webserver functionality for normal applications now.

Call Java function using .Net(C#)

is it possible to invoke function which is written in Java using WCF or any class application written in C# .net
Can it be possible by using webOrb..
i can't find enough information about Java to .Net remoting..
If you want to communicate between C# and Java you have a couple of options.
The cleanest: Build a service.
This assumes you have access to the source code of both your C# component and your Java component. In the case that you want to call a method within Java, you can build a service that allows a connection from your C# client, to your Java service, and the service then executes the desired functionality, and returns a value back to the C# client. Some easy ways to do this is by building a RESTful service or using Thrift. I recommend you choose a solution similar to this one.
The most complex: Corba
Corba is a standard defined to communicate amongst different computer languages. Most mature languages have support for it, but it is a bit unusual, and the use of it has declined in favor of building service. This also assumes access to both source codes.
You'd have to independently look for the information regarding how to use Corba on both Java and C#. I would really advice against this.
The dirtiest but quickest: Execute as process and parse output
I really do NOT recommend you to do it this way unless you really have no choice. This would entail executing a Java program from within C#. This is only a good choice when you have no other option, because all you have is an executable. If that were the case, you can use the Process class to execute the external program, sending it parameters, and then reading the output. See the example mentioned here:
How do I start a process from C#?
This has many downsides though, as you'll have to think of every exceptional cause, determine the output for those cases, and then determine how to parse that output. If the program has any level of complexity, before you know it, you'll end up with hard to maintain code.
Conclusion: Build a Service
That's probably your best bet. Build a service that exposes an API that the C# client can call on.
We are using JCOBridge package: it is able to create a bidirectional invocation of Java API from C# (.NET Core/6/Framework).
The templates available on Templates was our good starting point for the needs we had. We reach the goal in few lines of code.
UPDATE 2022: the JNet project on GitHub can be used as a starting point. Another project is KNet, hosted on GitHub and based on JNet, that is a gateway for Apache Kafka Java API.

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

Silverlight - Send/Receive Data at runtime with JAVA

Yesterday I asked about what technology should I use to create dynamic web content here:
PHP, AJAX and Java
The suggested methods were JSP, JQuery, etc. But I thought maybe because I'm a .Net developer and I don't have any experience in web development but I have experience in WPF and C#, maybe I should go with Silverlight but the main problem here would be how can I communicate with the core part of my system which is implemented in Java?
So the main question would be: What is the best [and easiest to learn] method to send a piece of data to the Java part, get the result and use it in silverlight? A tutorial or simple example would be nice.
Thanks a lot in advance.
You should use Java Web Services as stated. Use WCF to invoke the Java WS by adding a Service Reference in Visual Studio by its url, then use the proxy classes generated automatically (located in Reference.cs) to invoke the WS. This is easy but remember SilverLight WS invocations are always asynchronous, so you must cath the OnCompleted event to get the results of the invocation. WS are slow but if the machines are in the same LAN, invocation could take a few milliseconds.
I think pipes are not your solution as SilverLight executes in a Sandbox and have many restrictions on what you can do.
This will depend on many factors, however a relatively easy approach would be to use Java Web Services. On the .NET side, WSDL will be picked up and transformed into proxy class by WSDL.exe from the Windows SDK. If, however, these two systems are on the same server (and intend on staying this way), you may decide to use pipes.

Creating MMC Snap-Ins for console application and windows service

Hi guys I need to have a standard management gui ( look and feel ) and lately I've been looking into mmc. Msdn and other free tutorials provides the installation and simple implementation of a snap in.
I'm confused right now on how I can integrate it with my production applications( console and windows service ). Do I need to set a communication protocol for my snap in and other applications? I'm thinking of doing some classic remoting or via wcf for the communication protocol but not pretty sure if that should be my road map.
What I needed is a sample demo or tutorial that implements a snap-in communicating with a working console application or a windows service. If anyone can give some pointers on how to do it or some real life implementation is very much appreciated.
MMC does not dictate how you talk to your code. It is only the presentation layer. You can do pretty much anything you like (as long as you are well behaved in terms of not blocking the UI thread), you can do network calls, ADSI, RPC, WCF, COM, Win32,....
We using the ironring C# library instead of the mmc3 c# support; it is more stable (and works with older mmc)

Categories