Creating New Webservice with Available methods in DLL - c#

I have a solution file ( i.e DLL file). i want to create webservice/WCF Service which exposes the methods of DLL file. so that other team can use webservice instead of DLL reference
simply we cannot Add DLL as a reference to another project because another project is using in java..
so i have been provided DLL file and asked me to create one webservice( WCF is also fine) by using DLL file related methods.
please help me and my question is how can i expose DLL methods in Newly created webservice?
webservice/wcf any thing should be fine .

You can try creating a WCF Service, which has reference to this DLL file, You can call the functions in DLL from Operation contract() in your service contract.
And these operation contracts can be called from your other java project.

Of course you can!
If you can't edit the DLL:
Just create a normal webservice solution, and create the web methods that you want to expose in the DLL.
Then just call the appropriate DLL method in each web method.
If you can edit the DLL, just turn the project into a webservice project and expose the appropriate methods

This Beginners tutorial is excellent and should get you pointed in the right direction.

You can expose all dlls from the WebService Application. Add reference to WebService project
[WebMethod]
public bool CheckLogin(string username, string password)
{
bool status = false;
SqlCommand Command = new SqlCommand();
try
{
Command.CommandText="Select count(*) from CM_Users where username='"+username+"' and passwd='"+password+"'";
Command.Connection=DbConnection.OpenDbConnection();
// this is Assembly Loaded from Application
int count=(int)Command.ExecuteScalar();
if(count>0)
status=true;
else
status=false;
DbConnection.CloseDbConnection(Command.Connection);
}
catch (SqlException expmsg)
{
DbConnection.CloseDbConnection(Command.Connection);
}
return status;
}

Related

calling c# dll with (service) references from python

This question is similar to this one Including a service reference from a class library.
In my case I work with python and C#. In my C# ClassLibrary Project I use a service reference and a dll which provides a specific client as a class, used for calling the webservice. This code works fine if I try it in my console application:
class ws_class
{
[DllExport("webservice", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static string TestExport()
{
//Calling request
return response;
}
}
Now I want to use the .dll in python using this code:
api = ctypes.cdll.LoadLibrary(path)
api.webservice.retsype = ctypes.c_wchar_p
print(api.webservice())
In other cases this code also works fine for receiving string returned by a C# Class Library.
But as soon as I use the .dll reference for the client in my C# code, it doesn't work anymore. It seems like python cannot handle this issue.
Regarding the question I mentioned above, the config files must be identical but still it won't work.

ConnectionString in app.config API with ASP.NET

I am currently on a solution in ASP.net containing two project. One MVC project and the other is a class library serving as an API.
Currently I have a connection string like this in the web config of my project MVC.
I read it with the following code in my API:
public ConnectionProvider()
{
this.connectionString = ConfigurationManager.ConnectionStrings[Connection.Name].ConnectionString.ToString();
factory = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings[Connection.Name].ProviderName.ToString());
}
The problem is that I would like to move the connection string in the app.config of my API and by default but every time it starts, it will read in the web.config.
Using a resource file is a bad idea.
Just copy the the connection string from the api and have it in the web.config. By using the resource file there is no way to update that connection string without having to recompile.
The web application will use web.config and the connection string will be accessible from there. If the same api is to be reused in another project, say a desktop application, again just copy the connection string to the config file of the entry point.
As I suggested via comment, I would use a resource file in the class library.
While #Nkosi is not wrong that the connectionstring will be static, it's the easiest way to carry the connectionstring across w/o having an actual API or database call.
However, it's not the entire application that needs to be republished.
If the functionality in the code of the class library does not change, you can simply build the class library and overwrite the .dll of it.
Another valuable solution would be to save the connectionstring in a separate text file which all solutions will use.
Then you can replace that piece of code in all your applications while having 1 centralized point of access which can be replaced w/o building and replacing anything :).
(Posted solution on behalf of the OP).
Thanks to Dieter B!
With a resource file:
And for read it in the API:
public ConnectionProvider()
{
ResourceManager rm = new ResourceManager("Bank.Project.API.resources", GetAssemblyByName("Bank.Project.API"));
this.connectionString = rm.GetString(Connection.Name);
this.factory = DbProviderFactories.GetFactory(rm.GetString(Connection.Factory));
}
Assembly GetAssemblyByName(string name)
{
var Myassembly = AppDomain.CurrentDomain.GetAssemblies().
SingleOrDefault(assembly => assembly.GetName().Name == name);
return Myassembly;
}

Configure log4net logging in dll

I'm setting up a dll to be used as a third party dll for a different application. I want this dll to have it's own logging so the external application doesn't have to deal with setting up anything (I don't believe they use the same logging as we do). I've read that may not be the best solution but it's the task I've been given. We want to use log4net with this. I've looked at a few of the other questions on here and they mention that it is configurable via code, however, the main issue I'm having is that there is no clear cut entry point into our code to configure log4net. I'm curious if I should just abandon having the dll configure itself and have a method that is called by the secondary application that configures the dll's logging or if there is a better way to go about this. Any input would be much appreciated
You can configure log4net programmatically. Perhaps add this code to the constructor of your DLL.
if (!log4net.LogManager.GetRepository().Configured)
{
// my DLL is referenced by web service applications to log SOAP requests before
// execution is passed to the web method itself, so I load the log4net.config
// file that resides in the web application root folder
var configFileDirectory = (new DirectoryInfo(TraceExtension.AssemblyDirectory)).Parent; // not the bin folder but up one level
var configFile = new FileInfo(configFileDirectory.FullName + "\\log4net.config");
if (!configFile.Exists)
{
throw new FileLoadException(String.Format("The configuration file {0} does not exist", configFile));
}
log4net.Config.XmlConfigurator.Configure(configFile);
}

Communicating between C# and Visual C++ over IPC port (OR: Sharing a type between a Visual C++ DLL and a C# application)

I have a DLL written in Visual C++ and an application written in C#. The application that is written in C# already uses IPC between multiple instances of itself so that it only ever runs one instance (it attempts to start an IPC server, and if it can't assumes there's already one running, in which case it sends the command line arguments over IPC to the existing application).
Now I want to send commands from a Visual C++, however, even when I create a type definition in Visual C++ that matches the one in C# (on an implementation level), it rejects the connection because they are fundamentally still two different types (from two different assemblies).
I thought about using Reflection in Visual C++ to fetch the type from the C# assembly, but I can't do that because then I'd have to ship the assembly along side the DLL (which defeats the purpose of the DLL being an API to the application).
I'm not sure of any other way I could really do it, other than store the class in yet another DLL and make both the application and the API DLL reference the class in that, but this is also not an ideal solution as I'd like a single API DLL to distribute.
Are there any suggestions as to how I can connect over IPC (other forms of communication like TCP are not permitted) to send requests to the application?
The solution was to place the InterProcess class in the API DLL and simply make the C# application use the DLL as a reference to bring in the class.
It is also important to note that in order to initialize the shared object correctly, I had to initialize the server side of the sharing in a separate AppDomain and make the C# application a client like so (this is a new version of the previous paste):
try
{
// Set up an interprocess object which will handle instructions
// from the client and pass them onto the main Manager object.
this.m_ServerDomain = AppDomain.CreateDomain("roketpack_server");
this.m_ServerDomain.DoCallBack(() =>
{
// We must give clients the permission to serialize delegates.
BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
IpcServerChannel ipc = new IpcServerChannel("roketpack", "roketpack", serverProv);
ChannelServices.RegisterChannel(ipc, true);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(API.InterProcess),
"InterProcessManager",
WellKnownObjectMode.Singleton);
});
// Now initialize the object.
IpcClientChannel client = new IpcClientChannel();
ChannelServices.RegisterChannel(client, true);
this.m_InterProcess = (API.InterProcess)Activator.GetObject(
typeof(API.InterProcess),
"ipc://" + name + "/InterProcessManager");
InterProcessHandle.Manager = this;
this.m_InterProcess.SetCalls(InterProcessHandle.CallURL,
InterProcessHandle.IsLatestVersion,
InterProcessHandle.RequestUpdate);
return true;
}
catch (RemotingException)
{
// The server appears to be already running. Connect to
// the channel as a client and send instructions back
// to the server.
IpcClientChannel client = new IpcClientChannel();
ChannelServices.RegisterChannel(client, true);
API.InterProcess i = (API.InterProcess)Activator.GetObject(
typeof(API.InterProcess),
"ipc://" + name + "/InterProcessManager");
if (i == null)
{
Errors.Raise(Errors.ErrorType.ERROR_CAN_NOT_START_OR_CONNECT_TO_IPC);
return false;
}
if (Environment.GetCommandLineArgs().Length > 1)
i.CallURL(Environment.GetCommandLineArgs()[1]);
return false;
}
I hope this solution helps someone else :)

How to call wcf methods from client proxy

I have created one WCF service , which is working fine, now i want to consume it in a client application.
using SVCutil.exe i have generated proxy and aap.settings for that service and added that to the client sln(console application)
But the problem is i am unable to access the wcf methods.
using System.ServiceModel;
namespace WCFClient
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p. // not getting the wcf methods
}
}
}
what I am doing wrong?
Depends on how your service is called. When you created the service reference, you gave it a namespace name - in that namespace, there should be a class called (yourservicename)Client - instantiante one of those and get going.
You should find those files under the Service Reference - if you click on the "show all files" button in the Solution Explorer, you'll start seeing a ton of files under your service reference - one in particular should be Reference.cs. Those classes are defined in that file - you can check it out, it's a regular C# file.
Update: If you create your proxy using svcutil.exe, depending on your options used with svcutil, you should also get a .cs file that contains the classes needed.
svcutil http://yourserver/yourservice
would create a file called (your WSDL name).cs and an output.config in that directory where you run this command.
You can also specify a file name for the C# file:
svcutil http://yourserver/yourservice /out:MyService.cs
and then your file is called MyService.cs.
SvcUtil has a ton of options - can't explain them all to you, play around with them, read up on the MSDN docs for it.
Again, one of them will be called (your service name)Client. Include that *.cs file in your project, check the namespace, create an instance of the .....Client class and use it to call the WCF service.
Example:
Grab info from URL
svcutil http://www.ecubicle.net/iptocountry.asmx?wsdl /out:IP2CountryClient.cs
Include the resulting IP2CountryClient.cs in your project; by default, the classes in that file are in no particular namespace, so they're globally visible
Instantiate the client class iptocountrySoapClient
iptocountrySoapClient client = new iptocountrySoapClient();
Call methods - e.g. this one here:
string result = client.FindCountryAsString("82.82.82.82");

Categories

Resources