Using windows services [written in C#] using C# - c#

I wrote a windows service in C# that does some functionality (a public method) after reading a file at regular intervals. Is it possible for me to use this functionality from another C# application ? If possible, please give a solution based on .net 2.0 as well as .net 3.0
Thanks

The cleanest option would be to write that functionality in a separate assembly (dll) that you use from both the service and the second .NET application.
You can reference the service assembly (even if it is an exe, in VS2008 at least) - but it seems a little overkill.
If you want to execute the method in the context of the running service, then you'll need some IPC (perhaps WCF or sockets).

Just expose it using WCF and call it from your other app. They are in different processes so you have to use IPC

Depends hwo you want to reuse.
If you want to access the running window service then expose then functionality via WCF. Of you want to just use the code then place it in a assembly and reference from another project/program.
I'd say the .net 2.0 and 3.0 would be the same from the latter but since .net 2.0 fmk does not have WCF I'd suggest a windows remoting endpoint for the earlier if you're using .net 2.0 only or WCF as I stated.

Related

Caching usable in both WCF and ASP .NET applications (.NET 3.5)

I need caching facility which should work from both WCF and from within an ASP .NET app. I have seen:
System.Web.Caching.Cache which is not usable from WCF (as I understand it is usable only if service is hosted in IIS, which is not always the case)
System.Runtime.Caching which is usable from both of them, but only available from .NET 4, but our .NET version is 3.5.
Any suggestions?
You can absolutely use System.Web.Caching.Cache outside of IIS. Just add a reference to System.Web and away you go - it is absolutely not a feature of IIS.
There were issues in .NET 1.0 and 1.1 but apparently not in 2.0 and upwards - see this Knowledge Base article.
But like Davide Piras I would also recommend considering AppFabric to share the cache between your two applications, if that would be useful. I'm not sure what you mean by "my requirement cannot afford something like AppFabric" - by "afford" are you talking about performance, memory, or what? Whilst AppFabric is designed to be used on a cache server it does not by any means require dedicated hardware, you can run the service on the same box as your application if you need to.
new solution use Windows Server AppFabric Caching
https://github.com/geersch/AppFabric
http://dotnet.dzone.com/articles/caching-wcf-services-part-2
http://channel9.msdn.com/Events/TechEd/NorthAmerica/2012/AAP314
***2.in .NET 1.0 and 1.1 http://support.microsoft.com/kb/917411

Allowing my application to be used by other applications

I would like to know how to allow my C# application to be used from others' own applications.
Is making the relevant classes public enough for this purpose? Shall they be able to put a reference in their projects to my .exe and use my public classes freely? Shall they only be able to use it from .NET applications?
What else should I take into account? Any security issues maybe?
Your .Net dlls can be used by other .Net applications. You can separate the logic part of your code from the interface, and put the logic in "Library" projects that will be compiled as Dll files that can be used in a .Net application by adding references to them.
If you want to allow non .Net apps to used you can use COM Interop:
Wikipedia - COM Interop
COM Interop C# Tutorials
You can also use WCF services as CSharpVJ says.
Create a wrapper WCF service and expose your exe application thru' those WCF services, and it will be available to almost all clients based on even other platforms like Java, Python, Ruby and more depending on which settings you use inside your WCF Service..
WCF provides a service based model to communicate with other applications based on .Net and other platforms.
WCF is explained at this article in MSDN
You could always just make the sourcecode public on https://github.com/ for example.
That would other people allow to view and edit your sourcecode compeltly, which might be even better than a WCF service

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.

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.

C#: Correct project template for REST API library?

I want to consume a series of REST services from a provider. But there are a lot of functions I can call and send to the server, so I think it would be a good idea to create a separate library that my C#/MVC2 project can reference and call.
In VS2010, what is the correct project I should select to create this new library? Just plain old "Class library?" It's grouped under "Windows" so I don't know if the correct template to use for a web project.
Thanks.
"Class Library" would be fine. The Class library template is not tied to anyone particular type of project, so they can be used for Web, Console, Windows, Wpf etc.
Of course the functionality you provide in the Class library might be limited to a specific execution evironment because of the functionality you might put into the library, for example if you develop a bunch of functions that expect to be run in an ASP.NET environment then the functionality of the class library might not be applicable to a Console application.
Technically, yes, a "Class library" will give you what you want. Consider, however, whether there are any potential benefits for you in creating a proxy Web Service that you use as an intermediary between your own application(s) and the remote provider. Doing so allows additional management options that can be performed separately from the calling application.
You could also try the MSDN REST Starter Kit. It contains VS templates that help you do all the RESTful things you could ever imagine doing.

Categories