繁体   English   中英

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

[英]can not get to localhost in WCF Self Hosted Winform Service

使用此示例... 在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();
    }

应用程式设定...

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

接口...

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

    [OperationContract]
    Tickers[] BTestShort(string dte);
}

您永远不会打开主机:

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

由于您拥有if (Host==null)作为检查对象,并且您只是构造了Host ,因此检查将始终失败,并且永远不会调用.Open()

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

暂无
暂无

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

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