Winforms Windows窗体应用程序中的WCF

Winforms Windows窗体应用程序中的WCF,winforms,wcf,Winforms,Wcf,我正在尝试在Windows窗体应用程序中运行WCF服务。我复制并修改了Microsoft WCF示例中的代码。运行WCF示例时,该服务显示在我使用的端口监视器(CurrPorts)中。当我运行代码时,我看不到我的服务 这是我的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace NoName.Se

我正在尝试在Windows窗体应用程序中运行WCF服务。我复制并修改了Microsoft WCF示例中的代码。运行WCF示例时,该服务显示在我使用的端口监视器(CurrPorts)中。当我运行代码时,我看不到我的服务

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace NoName.Server
{
    [ServiceContract(Namespace="http://NoName")]
    public interface IApplicationService
    {
        [OperationContract()]
        NoName.Entities.MediaParameter[] GetParametersForMediaObject(string mediaObjectId);
        [OperationContract()]
        NoName.Entities.MediaParameter GetMediaParameter(string parameterId);
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NoName.Server
{
    public class ApplicationService : IApplicationService
    {
        public Entities.MediaParameter[] GetParametersForMediaObject(string mediaObjectId)
        {
            throw new NotImplementedException();
        }

        public Entities.MediaParameter GetMediaParameter(string parameterId)
        {
            throw new NotImplementedException();
        }
    }
}
我正在从一个表单运行它

private void toolStripButton1_Click(object sender, EventArgs e)
{
   using (ServiceHost host = new ServiceHost(typeof(ApplicationService)))
   {
      host.Open();
   }
}
以及app.config中的配置:

<system.serviceModel>
    <services>
      <service name="NoName.Server.ApplicationService" behaviorConfiguration="ApplicationServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/NoName/ApplicationService"/>
          </baseAddresses>
        </host>
        <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/NoName/ApplicationService  -->
        <endpoint address="" binding="wsHttpBinding" contract="NoName.Server.IApplicationService"/>
        <!-- the mex endpoint is exposed at soap.tcp://localhost:8000/NoName/ApplicationService/mex -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>

    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="ApplicationServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>


它编译时没有错误,运行时没有异常。它根本不存在。想法?

好吧-您使用的是
使用
块,这通常是件好事-但是在这里,服务主机将在
使用
块结束时立即关闭-这肯定不是您想要的

using (ServiceHost host = new ServiceHost(typeof(ApplicationService)))
{
    host.Open();   // host is opened here
}                  // host is disposed and closed here
因此,您的
ServiceHost
已打开并准备好接收请求-只需几秒钟。。。然后,由于使用{..}块的
结束,它被关闭并处理掉了

您需要做的是:

  • 将私有成员变量添加到例如主窗体

      private ServiceHost _serviceHost = null;
    
  • 在代码中的某个点打开该服务主机,例如,在您的方法中:

    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        _serviceHost = new ServiceHost(typeof(ApplicationService));
    }
    
  • 保持打开状态,直到Winforms应用程序关闭(或用户选择其他菜单项以实际关闭服务主机)


你说“它根本不存在”是什么意思?您看到它在端口监视器中占用了一个端口,所以它存在。当然是的(有少量的尴尬…),现在一切正常@user1062729:有时候,深入编程,你只是看不到森林中的树木了。我打赌这样的事情发生在任何人身上。