3

I have a simple WCF service hosted in a WinForms project. I am trying to update a WebBrowser control to navigate to a specified url received by the service.

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");
        }
    }
}

When the app loads it successfully navigates to www.example1.com as expected.

When i send a request via the WCF Test Client and put a breakpoint on ShowDocumentPreview(string path) the method is hit but the WebBrowser control does not navigate to www.example2.com

Any ideas what the issue is here?

Code for the Service:

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;
            }
        }
    }
}

Note: The reason I'm using WCF is because the WebBrowser control is 32-bit and the main app I'm developing needs to be 64-bit so the solution i have come up with is to have a separate 32-bit app with the WebBrowser control and the 64-bit app sends url's to the WebBrowser control via WCF.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Beach
  • 93
  • 5
  • Are you sure that the form instance from UI and the form instance for service are the same? You can force it to be a singleton using `ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]` – abatishchev Mar 18 '15 at 00:27

1 Answers1

2

Firstly, running an instance of WebBrowser control inside a WCF service is not a good idea, in the first place. It doesn't scale well and has security implications. WebBrowser control has never been intended for server-side use. You may want to look at some alternatives, like PhantonJS.

That said, if you still want to use WebBrowser from a WCF service environment (or just a console app), you need to create and run the instance of WebBrowser inside an STA thread that pumps windows messages. Currently you don't do that, and that's why the navigation doesn't work. You can use my MessageLoopApartment from this related answer.

Also note, WebBrowser control comes in both 32-bit and 64-bit versions, same as the Internet Explorer itself. Compile your .NET as 64-bit, and the 64-bit version of WebBrowser ActiveX control will be used.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • 1
    Thanks for your detailed answer, this caused me to re-investigate our original problem that pdf's would not open in the browser control when our app was running in 64-bit mode. We found that un-installing acrobat reader and installing acrobat reader 11 full install then running the update to 11.0.10 fixed the problem so this complex work around is no longer needed which is great!! Thanks once again. – Beach Mar 19 '15 at 19:28