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

标签 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 无法连接。

我的问题

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

最佳答案

似乎您期望单个客户端应用程序(Delphi 或 WinForm)在不更改客户端配置的情况下连接到不同的服务实例。您已经为 netTcpBinding 配置了 WinForm 服务实例,并为 basicHttpBinding 配置了 MyWCFService 实例。 Delphi 客户端只能连接到 MyWCFService 实例,因为它们都使用 basicHttpBinding,而 WinForm 客户端只能连接到 WinForm 实例,因为它们都使用 netTcpBinding。

要实现您认为应该工作的效果,您需要配置两个服务实例以公开 的端点。两个 basicHttpBinding 和 netTcpBinding。我从未尝试在 WinForm 应用程序中使用 basicHttpBinding,但我认为应该可以。

关于WCF 托管、配置和连接地址/端口问题(包括 Delphi 7 客户端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5987433/

相关文章:

c# - 由于内部错误,服务器无法处理请求

delphi - 为什么System.SetLength(Str, Len)会导致Str的地址发生变化?

delphi - 如何锁定文件直到下次重新启动?

delphi - 如何通过 USB 访问游戏 Controller ?

wsdl - 使用 JAX-WS 的 XML 序列

php - 更改 SOAP 请求格式

wcf - ACS 联盟问题

c# - wcf服务的奇怪问题

c# - 使用 Windsor CaSTLe 和 WcfFacility 创建具有 Message Security 和用户名凭据的 WCF 代理

java - 给定一个 WSDL 文件,通过 Internet 使用 Web 服务的步骤是什么?