
这真的可以节省我很多时间.我不需要维护两个完全相同的应用程序,每次进行更改时我都不需要部署到数百个信息亭.
解决方法@H_403_13@ 我知道这是一个老问题,但我确实有一个解决方案.我这样做的确如此.我在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
总结以上是内存溢出为你收集整理的如何在WPF 4应用程序中托管Silverlight 4应用程序?全部内容,希望文章能够帮你解决如何在WPF 4应用程序中托管Silverlight 4应用程序?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)