When we get a connection using datasource say oracleDatasource .
Lets say during programming, some developer does not close the connection
explicity. Will that connection be returned to connection or will it
be considered as connection leak?
As it is said, datasource is created, deployed, and managed separately(by appserver) from the applications that use it. There are two ways in which we can create datasource
From appserver admin console.(I understand that datasource can be managed by appserver here)
Second way is to create datasource in your pragramme like
Datasource ds = new OracleDataSource();
But how will datasource be managed by appserver api in this case as we are creating it in our programme?
Related
I need to create a single connection for the entire session of a user in MVC.
Below code wrote in the Login controller for achieving the same
OracleConnection con = new OracleConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["OracleConnection"].ConnectionString;
con.Open();
Session["oracle_con"] = con;
then whenever I need the connection will check as below
OracleConnection or_con = (OracleConnection)Session["oracle_con"];
if (or_con.State == ConnectionState.Closed)
or_con.Open();
This is working fine in my local system, sessin sate mode is 'InProc'
But in server the session state mode is "StateServer" , so the conversion of Oracle connection to session is giving the below error
Unable to serialize the session state. In 'StateServer' and
'SQLServer' mode, ASP.NET will serialize the session state objects,
and as a result non-serializable objects or MarshalByRef objects are
not permitted. The same restriction applies if similar serialization
is done by the custom session state store in 'Custom' mode.
[SerializationException: Type
'Oracle.ManagedDataAccess.Client.OracleConnection' in Assembly
'Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral,
PublicKeyToken=89b483f429c47342' is not marked as serializable.]
Is this the right way or any other way to achieve my requirement?
OracleConnection doesn’t implement Serializable so that won’t work for what you’re trying to do. Why do you want to put the connection in a session? ODP.NET uses a connection pool by default so a new connection request doesn’t have to be created from scratch.
If the connection is slow to create, it can be because of missing log folders that has to be created, C:\oracle_client\log\diag\clients.
See here, Creating 32-bit ODP.Net OracleConnection object is very slow, and here, http://milagaia.blogspot.com/2012/04/odpnet-slow-first-connection.html?m=1.
In my server side console program, multiple ServiceHost objects are created and working in PerSession mode, not singleton.
Therefore, a new server side object will be created when a new session started.
The problem is: How can I know which is the right ServiceHost object linked with the new created server side object? I do need this link information for further operations.
Can I get the ServiceHost information from the new created server side object? By converting it into another interface or class?
Or, can I hook the instance creating process of ServiceHost?
Any idea?
Thanks a lot.
Try the OperationContext - it has a .Host property:
var host = OperationContext.Current.Host;
See the MSDN documentation for more details.
I'm using WCF to create a connection beween a server app and client app.
Because I'm using NetTcpBinding, I want to keep alive the channel created by:
T proxy = ChannelFactory<T>.CreateChannel();
I bring the proxy instance into my app to communicate with server. At some moment, I renew this proxy, with a different factory. But I want to release resources from the first factory, by disposing it. But I don't wan't to manage factories and keep a reference to.
Is there a way with the proxy instance to retrive the channel associate to dispose it ?
Like ...
var commObj = (ICommunicationObject)proxy;
commObj.Factory.Dispose();
Thanks
Unfortunately, it is not possible, because Channels and Factories are not linked. But I think your approach is not correct. You should keep a reference to the factory and create Channel as needed. Re-using Channel can be a problem in some cases. A channel can be faulted, but the inner factory will still be valid. Creating a factory has a real cost, and some .net 3.5 SP1, factories are internally stored in a static cache.
I am new to WCF. Is the client connected when the service is instantiated
SampleService client = new SampleService();
Or does it connect and disconnect when a method is called on the client?
client.sampleMethod();
I want to connect to the client and send multiple messages across the same connection. Is this possible?
To answer your second question: the connection is kept open by default.
For the first, and as an clarification to above statement: it depends on the binding. For starters, there is no Connect() method in ClientBase<TChannel> (which a WCF proxy client inherits from), so there'll be no use calling it since that'll throw a compiler error.
A connection to the service is made upon the first call to the service; the constructor does nothing more than bring the client object in a usable state, it does not connect to the service.
It is connected when you use the service. Also you would need to call client.Close() if you would write it like that. We mostly write our service calls like this:
using(var client = new SampleServiceClient()){
client.MethodA();
client.MethodB();
}
So yes you can call multiple methods on one instance of the service. By writing the using statement you make sure that the service is disposed after usage.
Or if you like:
var client = new SampleServiceClient();
client.MethodA();
client.MethodB();
client.Close();
You will need to call the generated proxy it's Connect method.
e.g.
SampleServiceProxy client = new SampleServiceProxy();
client.Connect()
client.SampleMethod()
I have a method in my web service which returns a DataView,
I have setup a proxy which talks to this service but when i make this method in the proxy
public DataView GetSales(DateTime SalesDate)
{
ServiceClient client = new ServiceClient();
return client.GetSalesForDay(SalesDate);
}
I get the error "Cannot implicitly convert type 'object[]' to 'System.Data.DataView', i have tried googling this but not getting anywhere, any help would be much appreciated.
Thanks
You can't do this - you cannot and should not return something like a DataView from a WCF service ever. A WCF service would only ever return data - not objects with behavior (DataView contains a lot of behavior - sorting, filtering, etc.).
Instead, in your service code, do this:
query your database with a SqlDataReader
parse out the relevant info you really need (only those fields you're really interested in) into DTO's (Data Transfer Objects) - basically just plain objects holding nothing but the properties of a "sale" that are important to you
return a List from your WCF service
Instead of doing steps 1 and 2 yourself, you could also use Linq-to-SQL, NHibernate, or any other capable ORM to handle that conversion from row/columns in the database to an object for you.