围绕不同进程传递ClientConnections

时间:2014-01-24 10:34:27

标签: c# multithreading wcf

在遇到需要在线程之间传递的Client Connections时遇到了一些小问题。

1。)我们有一个服务类

    public class Service : ServiceBase
    {
        public ServiceHost serviceHost = null;

        public CMLiteService()
        {
            ServiceName = "MyService";
        }

        public static void Main()
        {
            ServiceBase.Run(new Service());
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                if (serviceHost != null)
                {
                    serviceHost.Close();
                }

                Uri baseAddress = new Uri("net.pipe://localhost/Service");

                // Step 2 Create a ServiceHost instance
                serviceHost = new ServiceHost(typeof(Service), baseAddress);


                // Step 3 Add a service endpoint.
                serviceHost.AddServiceEndpoint(typeof(IService), new NetNamedPipeBinding(), "Service");

                serviceHost.Open();

            }
            catch(Exception e)
            {

            }

        }

        protected override void OnStop()
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }
        }
    }

2.)我们有一个界面

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string InitalizeDataStore(string uri1, string uri2);
        [OperationContract]
        string CheckHealth();
        [OperationContract]
        string CreateObject(string parameters);
    }

3.。)我们有一个初始化我们的数据存储的方法

ObjectOperations objectOperations;
public InitalizeDataStore (string uri1, string uri2)
{
    Admin admin = new Admin(uri1, uri2);
    objectOperations = new ObjectOperations(admin.client1, admin.client2);
}

4.。)这是管理类,它实际连接到数据库,客户端都是线程安全的并支持多线程

StorageClient1 client1
StorageClient2 client2
string URI1;
string URI2;

public AdminServices(string uri1, string uri2)
    {
        URI1 = uri1;
        URI2 = uri2;

        InitializeClient1();
        InitializeClient2();
    }

    public StorageClient1 InitializeClient1()
    {
        try
        {
            client1 = new Client(new Uri(URI1));
            client1.Connect();
            return client1;
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    public Client2 InitializeClient2()
    {
        try
        {
            client2 = new Client(new Uri(URI2));
            client2.Connect();
            return client2;
        }
        catch (Exception e)
        {
            throw e;
        }
    }

5.)当我们启动服务并运行初始化方法时,它会连接并运行。但是,当我们启动另一个进程时,客户端连接为空?如果我们在一个控制台应用程序中自上而下运行代码,它可以工作,但我们需要运行一次初始化,然后必须为将来的进程设置客户端连接。

所以过程1:

IService pipeProxy = pipeFactory.CreateChannel();
pipeProxy.InitalizeDataStore(); //Returns "Connected"

Process 2: 
IService pipeProxy = pipeFactory.CreateChannel();
pipeProxy.CheckHealth(); //returns null

我们如何确保在另一个流程中也可以使用客户端连接详细信息。我也很新,所以我不太注重多线程。

1 个答案:

答案 0 :(得分:1)

我相信你想要完成的是WCF Singleton实例模式:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]

使用上述属性实现您的实现类,所有客户端将共享一个实例。