简体   繁体   English

无法在WCF自托管Winform服务中访问localhost

[英]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' 使用此示例... 在Windows窗体应用程序中托管WCF服务 ...在4.5 VS2012中使用Winform应用程序在form_Load()中加载可以,但无法在浏览器中访问...错误无法访问“本地主机”

    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() . 由于您拥有if (Host==null)作为检查对象,并且您只是构造了Host ,因此检查将始终失败,并且永远不会调用.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)... !! 再次感谢@Reed-..看来我可能实际上已经在工作,但很难说-因为我说没有网络可访问的服务方法....经过进一步试验...(新词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.?? .ps ..我在2014年的解决方案是不要使用“但是”,“但是”,“我告诉你什么..”,“猜测什么”,“你知道什么”,“看起来”,“等等”(开头声明)。 ,或其他被忽略的愚蠢术语...像“无论如何” ...所以,这里又来了...进一步的实验...(!!)..在winform中测试wcf服务可能比我们想象的要容易,但是很难找到正确的文档..我怎么知道MS所说的只是启动一个vs cmd提示符并加载WCFTestClient.exe,但为什么有人甚至知道VS cmnd提示符存在于何处? 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 !!! 与此相对应,该位置位于VS Tools下的“开始->'程序”栏中,并且实际上是通过运行C:\\ WcfTestClient.exe,然后在<'http://localhost/mexBTestService'>上方包含针对'mex'配置的修改后的端点地址<'http://localhost/mexBTestService'>实际上能够验证服务是否成功运行! Have a Great and Wonderful New Year to All!! 祝大家新年快乐!! And happy coding !! 和快乐的编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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