微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在WPF 4应用程序中托管Silverlight 4应用程序?

@H_404_0@
@H_404_0@
我有一个Silverlight 4应用程序,我想作为Kiosk应用程序加倍.它必须是全屏,触控功能,最重要的是要完全信任.我正在考虑使用WPF应用程序来托管Silverlight XAP.我知道 WPF/XNA SilverlightViewport,但似乎这个解决方案不提供WPF和Silverlight之间的通信.

这真的可以节省我很多时间.我不需要维护两个完全相同的应用程序,每次进行更改时我都不需要部署到数百个信息亭.

解决方法

我知道这是一个老问题,但我确实有一个解决方案.

我这样做的确如此.我在WPF主机中托管了一个Silverlight应用程序.我使用自托管WCF来提供双工网络tcp服务.要做到这一点,你还必须有一个HTTP托管的clientaccesspolicy.xml,我也可以从同一个服务中自托管.

我希望我能解释得足以让球滚动.我将尽可能包含一些样本,但由于该项目,我无法分享所有内容.

首先,一些考虑因素:
1)netlight在Silverlight和WPF之间不安全,因为Silverlight(从版本4开始 – 仅测试版本5)无法进行安全连接,因此如果要使用此连接,则必须使用自己的加密.

2)有两种方法可以进行双工WCF,一种是使用HTTP. HTTP方法“轮询”服务以查看是否有任何命令来自客户端的主机.这固有地使它比net tcp慢得多.另一个是net tcp(如前所述).只有http方法支持开箱即用的加密.

3)当您在WPF主机中托管Silverlight应用程序时,当您开始调试时,您将无法调试Silverlight应用程序,因为调试器将无法连接到(如它所见)外部Silverlight应用程序,只有WPF.要解决此问题,您必须在启动WPF主机时“激活”Silverlight应用程序的附件.

现在代码

远程服务接口:

using System.ServiceModel;

namespace RemoteService
{
    [ServiceContract(CallbackContract = typeof(IRemoteCallbackService))]
    public interface IRemoteService
    {
        [OperationContract]
        void TestCallback();

        // Todo: Add your service operations here
    }

    [ServiceContract]
    public interface IRemoteCallbackService
    {
        [OperationContract(IsOneWay = true)]
        void OnTestCallbackComplete();

        // Todo: Add your service operations here
    }
}

您的策略侦听器界面:

using System.IO;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace RemoteService
{
    [ServiceContract]
    public interface IPolicyRetriever
    {
        [OperationContract,WebGet(UriTemplate = "/clientaccesspolicy.xml")]
        Stream GetSilverlightPolicy();

        [OperationContract,WebGet(UriTemplate = "/crossdomain.xml")]
        Stream GetFlashPolicy();
    }
}

实际的服务实现:

using System.Text;

namespace RemoteService
{
    public class RemoteService : IRemoteService,IPolicyRetriever
    {
        IRemoteCallbackService client = null;

        public void TestCallback()
        {
            client = OperationContext.Current.GetCallbackChannel<IRemoteCallbackService>();
            client.OnTestCallbackComplete();
        }

        #region Cross Domain Policy Implementation

        private Stream StringToStream(string result)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
            return new MemoryStream(Encoding.UTF8.GetBytes(result));
        }

        //<grant-to>
        //<resource path="/" include-subpaths="true"/>
        //<socket-resource port="4502-4534" protocol="tcp" />
        //</grant-to>

        Stream IPolicyRetriever.GetSilverlightPolicy()
        {
            string result = @"<?xml version=""1.0"" encoding=""utf-8""?><access-policy><cross-domain-access><policy><allow-from http-request-headers=""*""><domain uri=""*""/></allow-from><grant-to><resource path=""/"" include-subpaths=""true""/><socket-resource port=""4502-4534"" protocol=""tcp"" /></grant-to></policy></cross-domain-access></access-policy>";
            return StringToStream(result);
        }

        Stream IPolicyRetriever.GetFlashPolicy()
        {
            string result = @"<?xml version=""1.0""?><!DOCTYPE cross-domain-policy SYstem ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd""><cross-domain-policy><allow-access-from domain=""*"" /></cross-domain-policy>";
            return StringToStream(result);
        }

        #endregion Cross Domain Policy Implementation
    }
}

上面应该在一个dll项目中的单独的类中,然后由WPF主机引用.

并在控制台应用程序中托管它(您需要将其转换为WPF):

using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using LocalService;
using RemoteService;

namespace Host
{
    internal class Program
    {
        /// <summary>
        /// Main host entry point,this starts our listeners
        /// </summary>
        /// <param name="args">The args.</param>
        private static void Main(string[] args)
        {
            // Start our listeners
            StartListeners(80,4504,true);
        }

        /// <summary>
        /// Starts the listeners.
        /// </summary>
        private static void StartListeners(int HttpPort = 80,int NetTcpPort = 4504,bool StartRemoteService = true)
        {
            Console.WriteLine("Starting Policy Listener");

            string policyaddress = "http://" + Environment.MachineName + ":" + HttpPort.ToString();
            string locallistener = "net.tcp://" + Environment.MachineName + ":" + NetTcpPort.ToString();

            // Start our policy listener and (if required) our remote http service:
            using (System.ServiceModel.ServiceHost policyandremoteservicehost = new System.ServiceModel.ServiceHost(typeof(RemoteService.RemoteService),new Uri(policyaddress)))
            {
                policyandremoteservicehost.AddServiceEndpoint(typeof(IPolicyRetriever),new System.ServiceModel.WebHttpBinding(),"").Behaviors.Add(new WebHttpBehavior());

                // if we are to start our remote service here too,then add that endpoint in:
                if (StartRemoteService)
                {
                    policyandremoteservicehost.AddServiceEndpoint(typeof(IRemoteService),new System.ServiceModel.PollingDuplexHttpBinding(),"RemoteService");
                }

                ServiceMetadataBehavior psmb = new ServiceMetadataBehavior();
                psmb.HttpGetEnabled = true;
                psmb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                policyandremoteservicehost.Description.Behaviors.Add(psmb);
                policyandremoteservicehost.open();


                Console.WriteLine("WCF Host Running...");
                Console.WriteLine("Press <enter> to shutdown");
                Console.ReadLine();
                Console.WriteLine("Closing connections,please wait");
                policyandremoteservicehost.Close();
                Console.WriteLine("Closed policy and remote services");
            }
        }
    }
}

哪个应该给你一些工作. startremoteservice bool可用于使其成为策略文件侦听器,然后允许您连接到远程服务,而远程服务不必托管策略文件.

请记住,Silverlight只能连接到HTTP / HTTPS端口,以及4502-4534范围内的TCP端口.包含的clientaccesspolicy.xml非常不受限制.

我希望这对某人有用:).

如果你愿意,请随时问我问题,我会留意一下.

有几点需要注意:您可以从此解决方案中为托管的xap提供服务,因此不需要IIS.您还可以从浏览器中运行Silverlight xap并仍然使用这些服务.

调试答案如下:Debugging Silverlight hosted in WPF

@H_404_0@

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐