【发布时间】:2015-05-20 15:29:46
【问题描述】:
我在 WinForms 项目中托管了一个简单的 WCF 服务。我正在尝试更新 WebBrowser 控件以导航到服务接收到的指定 url。
namespace DocumentViewer
{
public partial class FormMain : Form, IDocumentViewerService
{
private WfcServiceManager _service = new WfcServiceManager();
public FormMain()
{
InitializeComponent();
this.Load += FormMain_Load;
}
void FormMain_Load(object sender, EventArgs e)
{
_service.Start();
webBrowser1.Navigate("http://www.example1.com");
}
public void ShowDocumentPreview(string path)
{
webBrowser1.Navigate("http://www.example2.com");
}
}
}
当应用加载成功时,它会按预期成功导航到 www.example1.com。
当我通过 WCF 测试客户端发送请求并在 ShowDocumentPreview(string path) 上放置断点时,该方法被命中,但 WebBrowser 控件未导航到 www.example2.com
有什么想法吗?
服务代码:
namespace DocumentViewer
{
[ServiceContract]
public interface IDocumentViewerService
{
[OperationContract]
void ShowDocumentPreview(string path);
}
}
namespace DocumentViewer
{
public class WfcServiceManager
{
private Uri baseAddress = new Uri("http://localhost:30303/DocumentViewerService");
private ServiceHost _host;
public void Start()
{
_host = new ServiceHost(typeof(FormMain), baseAddress);
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
_host.Description.Behaviors.Add(smb);
_host.Open();
}
public void Stop()
{
if (_host != null)
{
_host.Close();
_host = null;
}
}
}
}
注意:我使用 WCF 的原因是因为 WebBrowser 控件是 32 位的,而我正在开发的主应用程序需要是 64 位的,所以我想出的解决方案是拥有一个单独的 32-带有 WebBrowser 控件的位应用程序和 64 位应用程序通过 WCF 将 url 发送到 WebBrowser 控件。
【问题讨论】:
-
你确定UI的表单实例和服务的表单实例是一样的吗?您可以使用
ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]将其强制为单例
标签: c# winforms wcf webbrowser-control 32bit-64bit