Returning an instance of a class and then using this object
-
3. prosince 2006 20:53
I am using NetTcpBinding to return an instance of class to the client and then use this instance to do the operations. But I am getting error that says "The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded...".
Here is the code:
//////////////////////Create a project of type class lib named common with the following interfaces
using
System;using
System.ServiceModel;namespace
Company.Common.Interfaces{
[
ServiceContract( SessionMode = SessionMode.Required)] public interface ICompanySystem{
[
OperationContract] IDatabaseSystem CreateDatabaseSystem(String token);}
}
using
System;using
System.ServiceModel;namespace
Company.Common.Interfaces{
[
ServiceContract( SessionMode = SessionMode.Required)] public interface IDatabaseSystem{
[
OperationContract] bool IsCorrectDB();}
}
//////////////////////////////////////////////Create a project of type class lib named Facade with following classes
using
System;using
System.ServiceModel;using
Company.Common.Interfaces;namespace
Company.BusinessFacade.Factories{
[
ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] public class CompanySystem : ICompanySystem{
public CompanySystem(){
}
public IDatabaseSystem CreateDatabaseSystem(String token){
return new DatabaseSystem(token);}
}
}
using
System;using
Company.Common.Interfaces;using
System.ServiceModel;namespace
Company.BusinessFacade{
[
ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class DatabaseSystem:IDatabaseSystem{
private String configObj = null; public DatabaseSystem(String config){
configObj = config;
}
public bool IsCorrectDB(){
return true;}
}
}
///////////////////////////////////////////SERVER
ServiceHost
sh; private void Form1_Load(object sender, EventArgs e){
//create a service host for MathServicesh =
new ServiceHost(typeof(Company.BusinessFacade.Factories.CompanySystem)); NetTcpBinding binding = new NetTcpBinding();binding.TransferMode =
TransferMode.Buffered;sh.AddServiceEndpoint(
typeof(Company.Common.Interfaces.ICompanySystem), //contract typebinding,
//one of the built-in bindings "net.tcp://Edison:8090//CompanySystem"); //the endpoint's addresssh.Open();
}
/////////////////////////Client, create windows project, add a form and then in the form's code press CTRL+A and paste this code
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;using
System.ServiceModel;using
Company.Common.Interfaces;namespace
Test{
public partial class Form1 : Form{
private static ChannelFactory<Company.Common.Interfaces.ICompanySystem> companySystemFactory = null; private static ICompanySystem companySystem = null; public Form1(){
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e){
}
public static ICompanySystem CompanySystem{
get{
if (companySystem == null){
String endPoint = "CompanySystem"; String server = "Edison";companySystemFactory =
new ChannelFactory<Company.Common.Interfaces.ICompanySystem>( new NetTcpBinding(), new EndpointAddress("net.tcp://" + server + ":8090" + "/" + endPoint));companySystem = companySystemFactory.CreateChannel();
}
return companySystem;}
}
public static IDatabaseSystem DatabaseSystem{
get{
try{
return CompanySystem.CreateDatabaseSystem("4234");}
catch (Exception ex){
throw ex;}
}
}
private void button1_Click(object sender, EventArgs e){
MessageBox.Show(DatabaseSystem.IsCorrectDB().ToString());}
}
}
Všechny reakce
-
5. prosince 2006 16:12
This is the same problem as discussed on the other thread. You cannot return an object reference (an interface) from a service method. -
5. prosince 2006 16:28Thanks for help. We are exposing all the classes now. But we want one single class to take care of all the connection that are made by different clients. Do we need to make it as Single or PerSession? What exactly is difference between these two is vague.
-
5. prosince 2006 16:40
Following the Singleton design-pattern, Single means that there is a single service instance in memory (a single CLR object) that services all the calls. You can either provide the instance to use, or just specify the type and have the infrastructure create the instance for you on the first call. This mode is useful when there is no state associated with the individual client.
PerSession means that a new service instance would be created for each client connection and is a little like client activated objects in .NET Remoting in the sense that a new instance of the service is created by the system to serve each new client connection. This is useful for the cases where you want to manage some transient per-client state at the object that implements the service.
-
5. prosince 2006 19:00
Thanks for the help.
Our systems are composed of a Factory class which handles login and all the other classes which deal with data. In the Factory class there is a static array which logs all the login by the clients. I am wondering if I should make the Factory class Single and all the other classes PerSession or just make all Single. As you have said PerSession is for handling state, and classes do not log states. How will I be able to know when a client sigs out and logs off?
-
5. prosince 2006 21:59This thread seems to be straying from WCF issues and into general application design, which is outside the scope of this forum. While the specific answer to your specific question will depend on several things that we cannot get into on this forum (security model, authentication model, deployment and hosting model, application functionality, threading model, # of clients and client usage patterns, etc.) the general guidelines indicated above should hold. The decision on what things should be shared and how to share them is your own, and it's specific to the app you're building.