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
Related
My .NET application is 32-bit because I use TWAIN inside it. But I need to write 64-bit library or app and call its methods from my program.
I must create a 64-bit library to interact with WMI SMO, and its functions don't work properly if it compiled as 32-bit dll.
How can I resolve this issue? Is it possible to use a 64-bit library from a 32-bit application?
You cannot reference a 32-bit library from a 64-bit executable and viceversa. This is a technical limitation.
However, depending on your other constraints, you may decide to employ a service-based architecture to avoid issues like yours and be more agile in the future.
In fact, you could write "micro" services specialized to solve one single purpose (scanning, interacting with WMI, etc...) and then call them from your application.
This way, you are completely free to choose whichever technology/architecture you require to achieve every single task your application requires. Isolating your "features" in services, allows you to have a different evolutionary path per service, as each service is loosely coupled with each others.
This solution requires writing more code than using a library. First you need to create a windows service (msdn) to
wrap your code, then you need to implement the communication channel. Have a look at WCF and WebApi2 to have an idea of what you can achieve.
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.
I need to create a dll in C sharp which is interoperable with php and Tomcat/Java webservices.
Will the normal C# class library help me for this?
Thanks,
John
Will the normal C# class library help me for this?
You could expose the class library as WCF service using the interoperable basicHttpBinding which could be consumed by PHP and Java.
I'm interpreting that as "I need to consume a web-service", in which case either use wsdl.exe, or use the "Add Web Reference..." / "Add Service Reference..." option in Visual Studio. Either approach results in appropriate proxy classes being created that represent the target service's WSDL.
This is data/protocol interoperability; note that the dll itself won't be directly usable from java / php.
Your question is a bit unclear on which way to "operability" goes. I assume you are talking about making a C# class library that will call these web services, which happens to be implemented in PHP/TomCat/Java.
And yes, the .Net Framework have excellent support for calling http endpoints. Also, if these endpoints happen to return XML data, there is excellent support for working with that too. If the services are RESTful, you even have support for that.
Update: since my initial assumption was wrong, and the interoperability must go from PHP/Java to .Net I suggest looking at how these languages can interop with COM. .Net classes can be exposed as COM classes, which further can be consumed by PHP and Java.
Maybe Im wrong about what is the question, but you can use c# assemblies in php with the DOTNET class:
http://php.net/manual/en/class.dotnet.php
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.
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.