简体   繁体   中英

can not get to localhost in WCF Self Hosted Winform Service

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

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() .

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 !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM