绑定在WCF中用作远程备选方案

时间:2014-01-24 11:47:06

标签: wcf remoting

如果我们想要将远程处理替换为WCF,使用什么绑定? 如果我们在远程处理的两个应用程序之间使用了共享dll来发送消息。 如何在WCF中完成此任务。

2 个答案:

答案 0 :(得分:1)

答案取决于您的基础设施。如果您的服务都在同一台计算机上,则可以使用NetNamedPipeBinding。如果服务和消费者位于不同的计算机上,您可以使用NetTcpBindingBasicHttpBinding

答案 1 :(得分:1)

This solved my purpose... 

private const int RF_TESTMESSAGE = 0xA123;
        const int WM_USER = 0x0400;
        const int WM_CUSTOM_MESSAGE = WM_USER + 0x0001;

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int SendMessage(IntPtr hwnd, [MarshalAs(UnmanagedType.U4)] int Msg, IntPtr wParam, int lParam);

        public FormVideoApp()
        {
            InitializeComponent();
            lblProcID.Text = string.Format("This process ID: {0}", Process.GetCurrentProcess().Id);
        }     

        private void button1_Click(object sender, EventArgs e)
        {
            //get this running process
            Process proc = Process.GetCurrentProcess();
            //get all other (possible) running instances
            //Process[] processes = Process.GetProcessesByName(proc.ProcessName);
            Process[] processes = Process.GetProcessesByName("Application");
            int numberToSend = 1500;
            string str = string.Empty;
            str = "start";
            switch (str)
            {
                case "start":
                    numberToSend = 101;
                    break;
                case "stop":
                    numberToSend = 102;
                    break;
                case "error":
                    numberToSend = 103;
                    break;
            }

            if (processes.Length > 0)
            {
                //iterate through all running target applications
                foreach (Process p in processes)
                {
                    if (p.Id != proc.Id)
                   {
                        //now send the RF_TESTMESSAGE to the running instance
                        //SendMessage(p.MainWindowHandle, RF_TESTMESSAGE, IntPtr.Zero, IntPtr.Zero);
                       SendMessage(p.MainWindowHandle, WM_CUSTOM_MESSAGE, IntPtr.Zero, numberToSend);
                   }
                }
            }
            else
            {
                MessageBox.Show("No other running applications found.");
            }
        }
        protected override void WndProc(ref Message message)
        {
            //filter the RF_TESTMESSAGE
            if (message.Msg == WM_CUSTOM_MESSAGE)
            {
                int numberReceived = (int)message.LParam;
                string str=string.Empty;
                //Do ur job with this integer

                switch (numberReceived)
                {
                    case 101: str = "start";                       
                        break;
                    case 102: str = "stop";
                        break;
                    case 103: str = "error";
                        break;
                }
                this.listBox1.Items.Add(str + "Received message RF_TESTMESSAGE");
            }
            else
            {
                base.WndProc(ref message);
            }

            ////filter the RF_TESTMESSAGE
            //if (message.Msg == RF_TESTMESSAGE)
            //{
            //    //display that we recieved the message, of course we could do
            //    //something else more important here.
            //    this.listBox1.Items.Add("Received message RF_TESTMESSAGE");
            //}
            //be sure to pass along all messages to the base also
            //base.WndProc(ref message);
        }