1

using this example...Hosting WCF service inside a Windows Forms application ... with a Winform Application in 4.5 VS2012 in the form_Load() it loads okay but can not access in Browser ... error can not access 'localhost'

    private ServiceHost Host;

    private void frmAdmin_Load(object sender, EventArgs e)
    {
        Host = new ServiceHost(typeof(bklvmain_v4.BTestService));
        if (Host == null)
            Host.Open();
    }

    private void closeToolStripMenuItem_Click(object sender, EventArgs e)
    {
            if (Host != null)
                Host.Close();
    }

App config...

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="bklvmain_v4.BTestServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="bklvmain_v4.BTestServiceBehavior"
          name="bklvmain_v4.BTestService">
        <endpoint address="" binding="wsHttpBinding" contract="bklvmain_v4.IBTestService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/BTestService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel> 

interface...

[ServiceContract]
public interface IBTestService
{
    [OperationContract]
    Tickers[] BTestLong(string dte);

    [OperationContract]
    Tickers[] BTestShort(string dte);
}
Community
  • 1
  • 1
CraigJSte
  • 912
  • 6
  • 17
  • 33

2 Answers2

0

You're never opening the Host:

private void frmAdmin_Load(object sender, EventArgs e)
{
    Host = new ServiceHost(typeof(bklvmain_v4.BTestService));
    // if (Host == null)  
    // Could be if (Host != null), but the check is not required here anyways
    Host.Open();
}

Since you had if (Host==null) as your check, and you just constructed the Host, the check will always fail and you'll never call .Open().

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • huh-huh..:)..! well that did get me to browser page but there is nothing displayed at all in Chrome and IE give me a sandbox violation ?? But I think that is their SWF file for New Years causing this.. I am not running flash -- anyway I am still a little miffed...as to why no Service Contracts I have included my IBTestService reference above – CraigJSte Jan 01 '14 at 21:03
  • Is there any suggestion why a web page does not show up? I kind of wonder why it would since it is not a web application.. but nevertheless I continue... – CraigJSte Jan 01 '14 at 21:19
  • @CraigJSte That depends on a lot of things - You're not showing the bindings/transports/etc that you're using, so it may not use http at all. WCF supports a LOT of different options there. – Reed Copsey Jan 01 '14 at 21:39
0

Thanks @Reed -- again.. and it seems like I may have been actually working but its difficult to tell - b'cause as I stated there were no web accessible service methods.... upon further experimentiation... (new word for 2014)... !! .ps.. my 2014 resolution is to NOT use 'but' 'however', 'I tell you what..', 'guess what', 'you know what', 'look', 'so..(beginning statement), or other overlooked dumb terminology... like 'anyway'... so, here goes again...upon further experimentiationology...(!!).. testing a wcf service in winform may be easier than we think but it's very difficult to find the right documentation.. how I know MS says just fire up a vs cmd prompt and load WCFTestClient.exe but why would anyone even know where a VS cmnd prompt existed.?? In deference to this, the location is in the 'Start -> 'Programs' bar under VS Tools and in fact, by running C:\WcfTestClient.exe and then including a modified endpoint address for the 'mex' configuration above <'http://localhost/mexBTestService'> is actually able to verify the service was running successfully !!! Have a Great and Wonderful New Year to All!! And happy coding !!

CraigJSte
  • 912
  • 6
  • 17
  • 33