Reputation: 47
I created my WCF service that make a connection to SQL Server and returns the results of queries.
My problem is: how can I save the request from the client and not make a connection for each request from client?
The scenario I want is:
Thanks
Upvotes: 2
Views: 1123
Reputation: 1666
From http://msdn.microsoft.com/en-us/magazine/cc163590.aspx, You can use Per-Session Services :
[AttributeUsage(AttributeTargets.Interface|AttributeTargets.Class, Inherited=false)]
public sealed class ServiceContractAttribute : Attribute
{
public bool Session {get;set;}
... // More members
}
Session defaults to false. To support sessions, you need to set Session to true at the contract level: [ServiceContract(Session = true)] interface IMyContract {...}
To complete the configuration, you need to instruct Windows Communication Foundation to keep the service instance alive throughout the session and to direct the client messages to it. This local behavior facet is achieved by setting the InstanceContextMode property of the ServiceBehavior attribute to InstanceContextMode.PerSession, as shown in the following:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
class MyService : IMyContract {...}
Per-Session Service and ClientService Code
[ServiceContract(Session = true)]
interface IMyContract
{
[OperationContract]
void MyMethod();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
class MyService : IMyContract,IDisposable
{
int m_Counter = 0;
MyService()
{
Trace.WriteLine("MyService.MyService()");
}
public void MyMethod()
{
m_Counter++;
Trace.WriteLine("Counter = " + m_Counter);
}
public void Dispose()
{
Trace.WriteLine("MyService.Dispose()");
}
}
Client Code
MyContractProxy proxy = new MyContractProxy();
proxy.MyMethod(); proxy.MyMethod();
proxy.Close();
Both the client and the service can configure a different timeout by setting a different value in the binding. The bindings that support reliable transport-level session provide the ReliableSession property with the InactivityTimeout property used for configuring the idle timeout. For example, the following shows the code that is required to programmatically configure an idle timeout of 30 seconds for the TCP binding:
NetTcpBinding tcpSessionBinding = new NetTcpBinding();
tcpSessionBinding.ReliableSession.Enabled = true;
tcpSessionBinding.ReliableSession.InactivityTimeout = TimeSpan.FromSeconds(30);
Here is the equivalent configuration setting using a config file:
<netTcpBinding>
<binding name="TCPSession">
<reliableSession enabled="true" inactivityTimeout="00:00:30"/>
</binding>
</netTcpBinding>
Upvotes: 1
Reputation: 64248
Connections to SQL Server are cached by the clients. Assuming you are using HTTPS to secure the transport, then you should have the client send the credentials with every request. Provided that you compose the same connection string you will likely be using a cached connection.
Honestly I would avoid trying to capture it in the session; however, this is also possible. Client-server protocols should always remain stateless whenever possible.
If you are not using HTTPS then you are completely insecure and you might as well remove the password requirement all together and just allow anyone to query whatever data they want.
Upvotes: 0