开发者

WCF hosting, configuration and connection address/port issues (including Delphi 7 client)

开发者 https://www.devze.com 2023-03-05 08:23 出处:网络
I have a WCF service (MyWCFService) with base address http://localhost:8732/Design_Time_Addresses/MyWCFService/Service1/

I have a WCF service (MyWCFService) with base address

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

and the following in 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>开发者_如何学Python;
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

This MyWCFService is hosted in a WinForms app (WinFormsServiceHost) with base address

net.tcp://localhost:2202/Service1

and the following code on a 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) ...
    }
}

I test this service with a WCF test client (WinFormsWCFTester) which uses

net.tcp://localhost:2202/Service1

and has the following code on a 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);
        }
    }
}

I also have a Delphi 7 test client (Delphi7WCFTester) which uses address

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

Note: the address comes from the WSDL generated by Add Service Reference when my WinFormsServiceHost is running. I use the updated WSDL Importer to create a Pascal proxy class for Delphi 7.

The situation on my PC (even stranger things happen when I move the code to another PC or worse - eg. a VM!)

When service is running (hosted by WCF Service Host - WcfSvcHost.exe invoked by VS2010), Webbrowser can navigate to

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

and telnet can connect to Localhost 8732. My Delphi client (Delphi7WCFTester) can connect and works properly, but my WCF client (WinFormsWCFTester) cannot connect (Exception message "Could not connect to net.tcp://localhost:2202/Service1 ... TCP error code 10061 ... target machine actively refused it 127.0.0.1:2202")

When service is being hosted by my WinFormsServiceHost and running in the Visual Studio debugger, Webbrowser can navigate to http://localhost:8732/Design_Time_Addresses/MyWCFService/Service1/ and telnet can connect to both ports 8732 or 2202. Both clients (WinFormsWCFTester and Delphi7WCFTester) can connect and work properly.

When service is being hosted by WinFormsServiceHost and running independently as exe (I use Ctrl-F5 from VS2010), neither address can be browsed with Webbrowser. Telnet can connect to 2202 but not 8732. My WinFormsWCFTester client connects and works properly, but my Delphi7WCFTester can't connect.

My Question

As you can probably tell from my exhaustive description of my situation, I'm a little lost. I need some guidance to get back in control of what's happening. Both clients should connect and work properly regardless how the service is being hosted. I know I have a lot to learn. I hope somebody can lead me in the right direction with a few important pointers.


Seems like your are expecting a single client app (Delphi or WinForm) to connect to the different service instances without changing the client configuration. You have the WinForm service instance configured for netTcpBinding and the MyWCFService instance configured for basicHttpBinding. The Delphi client can only connect to the MyWCFService instance because they're both using basicHttpBinding and the WinForm client can only connect to WinForm instance because they both use netTcpBinding.

To achieve what you think should work, you need to configure both service instances to expose endpoints for both basicHttpBinding and netTcpBinding. I've never tried to use basicHttpBinding within a WinForm app but I think that should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号