WCF托管,配置和连接地址/端口问题(包括Delphi 7客户端)

时间:2011-05-13 05:03:13

标签: wcf delphi wsdl wcf-binding wcf-client

我有一个基地址为

的WCF服务(MyWCFService)
http://localhost:8732/Design_Time_Addresses/MyWCFService/Service1/

以及app.config中的以下内容

  <system.serviceModel>
    <diagnostics performanceCounters="Default">
      <messageLogging logEntireMessage="true" logMalformedMessages="true"
        logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" />
    </diagnostics>
    <extensions>
      <behaviorExtensions>
        <add name="wsdlExtensions" type="WCFExtras.Wsdl.WsdlExtensionsConfig, WCFExtras, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </behaviorExtensions>
    </extensions>
    <services>
      <service name="MyWCFService.Service1" behaviorConfiguration="MyWCFService.Service1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/MyWCFService/Service1/"   />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <!--<endpoint address ="" binding="wsHttpBinding" contract="MyWCFService.IService1">
          --><!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          --><!--
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>-->
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <endpoint address="" binding="basicHttpBinding" behaviorConfiguration="singleFileEndpointBehavior" contract="MyWCFService.IService1"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="singleFileEndpointBehavior">
          <wsdlExtensions singleFile="True" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MyWCFService.Service1Behavior">
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

此MyWCFService托管在WinForms应用程序(WinFormsServiceHost)中,基地址为

net.tcp://localhost:2202/Service1

以及WinForm上的以下代码

namespace MyWCFService
{
    public partial class Form1 : Form
    {
        bool serviceStarted = false;
        ServiceHost myServiceHost = null;
        Uri baseAddress = new Uri("net.tcp://localhost:2202/Service1");
        NetTcpBinding binding = new NetTcpBinding();
        public Form1() { InitializeComponent(); }

        private void StartService(object sender, EventArgs e)
        {
            if (!serviceStarted)
            {
                myServiceHost = new ServiceHost(typeof(Service1), baseAddress);
                myServiceHost.AddServiceEndpoint(typeof(IService1), binding, baseAddress);
                myServiceHost.Open();
                serviceStarted = true;
                RefreshServiceStatus(this, null);
            }
        }
        private void RefreshServiceStatus(object sender, EventArgs e) ...
        private void StopService(object sender, EventArgs e) ...
    }
}

我使用WCF测试客户端(WinFormsWCFTester)测试此服务,该客户端使用

net.tcp://localhost:2202/Service1

并在WinForm上有以下代码

namespace MyWCFService
{
    public partial class Form1 : Form
    {
        IService1 IService1 = null;
        public Form1() { InitializeComponent(); }

        private void Form1_Load(object sender, EventArgs e)
        {
            EndpointAddress address = new EndpointAddress(new Uri("net.tcp://localhost:2202/Service1"));
            NetTcpBinding binding = new NetTcpBinding();
            ChannelFactory<IService1> factory = new ChannelFactory<IService1>(binding, address);
            IService1 = factory.CreateChannel();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            lblResponse.Text = IService1.GetData(txtSomeText.Text);
        }
    }
}

我还有一个使用地址的Delphi 7测试客户端(Delphi7WCFTester)

http://localhost:8732/Design_Time_Addresses/MyWCFService/Service1/

注意:当我的WinFormsServiceHost运行时,该地址来自Add Service Reference生成的WSDL。我使用更新的WSDL Importer为Delphi 7创建Pascal代理类。

我的PC上的情况(当我将代码移动到另一台PC或更糟糕的时候甚至更奇怪的事情发生 - 例如VM!)

当服务正在运行时(由WCF服务主机托管 - 由VS2010调用WcfSvcHost.exe),Webbrowser可以导航到

http://localhost:8732/Design_Time_Addresses/MyWCFService/Service1/

和telnet可以连接到Localhost 8732.我的Delphi客户端(Delphi7WCFTester)可以连接并正常工作,但我的WCF客户端(WinFormsWCFTester)无法连接(异常消息“无法连接到net.tcp:// localhost:2202 / Service1 ... TCP错误代码10061 ...目标机器主动拒绝它127.0.0.1:2202“)

当我的WinFormsServiceHost托管服务并在Visual Studio调试器中运行时,Webbrowser可以导航到http://localhost:8732/Design_Time_Addresses/MyWCFService/Service1/,telnet可以连接到两个端口8732或2202.两个客户端(WinFormsWCFTester)和Delphi7WCFTester)可以正常连接和工作。

当服务由WinFormsServiceHost托管并作为exe独立运行时(我使用VS2010的Ctrl-F5),Webbrowser都不能浏览任何地址。 Telnet可以连接到2202而不是8732.我的WinFormsWCFTester客户端连接并正常工作,但我的Delphi7WCFTester无法连接。

我的问题

正如你可以从我对我的情况的详尽描述中所说的那样,我有点失落。我需要一些指导来重新控制正在发生的事情。无论托管服务如何,两个客户端都应该连接并正常工作。我知道我有很多东西需要学习。我希望有人可以带着一些重要的指示引导我朝着正确的方向前进。

1 个答案:

答案 0 :(得分:2)

好像您希望单个客户端应用程序(Delphi或WinForm)连接到不同的服务实例而不更改客户端配置。您具有为netTcpBinding配置的WinForm服务实例和为basicHttpBinding配置的MyWCFService实例。 Delphi客户端只能连接到MyWCFService实例,因为它们都使用basicHttpBinding,而WinForm客户端只能连接到WinForm实例,因为它们都使用netTcpBinding。

要实现您认为应该工作的内容,您需要配置两个服务实例以公开 basicHttpBinding和netTcpBinding的端点。我从来没有尝试过在WinForm应用程序中使用basicHttpBinding,但我认为这应该可行。