none
关于Metro App使用WCF与Desktop App通信问题 RRS feed

  • 问题

  • 版主你好:

    我的程序中有使用WCF使得Metro和Desktop进行通信,可我遇到了个问题是Metro端->Create AppPackage打包后的程序在

    有些PC上正常运行,有的却无法正常运行;我是通过Output窗口中提示的:“Send Message Successfuly”来看是否通信成功,

    我想不通为什么有的PC上可以正常运行有的却不能?版主Copy这段代码运行下,请帮我一起解决这个Bug,感激不尽!!!

    以下是程序:

    Metro端程序:

    using System.Diagnostics;
    using System.ServiceModel;
    using MetroClient.ServiceReference1;
    namespace MetroClient
    {
        [ServiceContract]
        public interface IHelloWCFService
        {
            [OperationContract]
            void MetroToDesktop(string para);
        }
    
        public sealed partial class MainPage : Page
        {
            private HelloWCFServiceClient client;
            private DispatcherTimer timerMetroToDesktop = new DispatcherTimer();
            public static string sendData = string.Empty;   //Metro向Desktop发送的命令
    
            public MainPage()
            {
                this.InitializeComponent();
    
                sendData = "Send Message Successful! ";
    
                this.client = new HelloWCFServiceClient();
    
                this.timerMetroToDesktop.Interval = TimeSpan.FromSeconds(2);
                this.timerMetroToDesktop.Tick += new EventHandler<object>(MetroToDesktop);//Metro->Desktop
                this.timerMetroToDesktop.Start();//Metro->Desktop
            }
    
            private async void MetroToDesktop(object sender, object e)//Metro->Desktop
            {
                timerMetroToDesktop.Stop();
                if (sendData != string.Empty)
                {
                    await this.client.MetroToDesktopAsync(sendData);
                    sendData = string.Empty;
                }
                else
                {
                    await this.client.MetroToDesktopAsync(string.Empty);
                }
                timerMetroToDesktop.Start();
            }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
        }
    }

    Desktop端程序:
    using System.ServiceModel;
    using System.ServiceModel.Description;
    
    namespace WPFServer
    {
        [ServiceContract]
        public interface IHelloWCFService
        {
            [OperationContract]
            void MetroToDesktop(string para);
        }
        public class ServiceProvider : IHelloWCFService
        {
            public void MetroToDesktop(string para)
            {
                MainWindow.DesktopReceivedParam = para;
                Console.WriteLine(MainWindow.DesktopReceivedParam);
            }
        }
        public partial class MainWindow : Window
        {
            public static string DesktopReceivedParam;
    
            public MainWindow()
            {
                InitializeComponent(); 
                
                try
                {
                    ServiceHost host = new ServiceHost(typeof(ServiceProvider), new Uri("http://localhost:8000/HelloWCFService"));
                    host.AddServiceEndpoint(typeof(IHelloWCFService), new BasicHttpBinding(), "");
    
                    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                    smb.HttpGetEnabled = true;
    
                    host.Description.Behaviors.Add(smb);
                    host.Open();
                }
                catch { }
            }
        }
    }

    2012年7月2日 1:31

答案