none
How can I communicate between a windows service and a windows form ? RRS feed

  • Question

  • Hi, 

    I want to communicate from my windows service and my windows form for differents cases.

    1. my windows form to work have to ask the windows service a key
    2. the windows service have to send the key
    3. the windows form have to get the key and give it back
    4. Have a timeout in my windows service which will ask to my windows form every 5 minutes for example if the windows form have a key

    I just want to do the case 4 because with it I will be able to do all cases.

    Someone suggest me to use duplex service but I have an error.
    And maybe there is an other solution to communicate between my windows service and my windows form without duplex service... I don't know.

    Someone have any suggestion ?

    Servicer-side Interface

    [ServiceContract(CallbackContract = typeof(IInteractCallBack))]
        public interface IInteract
        {
    
            [OperationContract]
            void ProgressReport();
        }
    
        public interface IInteractCallBack
        {
            [OperationContract]
            void TaskProgress(int progress);
        }


    Server-side service

    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
        public class Interact : IInteract
        {
            public void ProgressReport()
            {
                IInteractCallBack callBack = OperationContext.Current.GetCallbackChannel<IInteractCallBack>();
                for (int i = 1; i <= 100; i++)
                {
                    Thread.Sleep(50);
                    callBack.TaskProgress(i);
                }
    
    
        }

    Server-side host on start of my windows service

                
    ServiceHost host = new ServiceHost(typeof(Interact));       
    host.Open();

    Server-side app.config

    <configuration>
      <configSections>
            <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
        </startup>
        <system.serviceModel>
            <behaviors>
                <serviceBehaviors>
                    <behavior name="hostService">
                        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                        <serviceDebug includeExceptionDetailInFaults="true" />
                    </behavior>
                </serviceBehaviors>
            </behaviors>
            <services>
                <service behaviorConfiguration="hostService" name="ProjectName.Interact">
                    <endpoint address="Interact" binding="netTcpBinding" contract="ProjectName.IInteract" bindingConfiguration="">
                        <identity>
                            <dns value="localhost" />
                        </identity>
                    </endpoint>
                    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                    <host>
                        <baseAddresses>
                            <add baseAddress="http://localhost:8733/Service/Interact/" />
                            <add baseAddress="net.tcp://localhost:8734/Service/Interact/" />
                        </baseAddresses>
                    </host>
                </service>
            </services>
        </system.serviceModel>
        <entityFramework>
            <providers>
                <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
                <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
            </providers>
            <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, EntityFramework6.Npgsql" />
        </entityFramework>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    <system.data>
        <DbProviderFactories>
          <remove invariant="Npgsql" />
          <add name="Npgsql Provider" invariant="Npgsql" description=".NET Framework Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=4.0.10.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
        </DbProviderFactories>
      </system.data></configuration>

    In client side I add my windows service in service reference.

    Client-side (Windows form)

    [CallbackBehavior(UseSynchronizationContext = false)]
        public partial class Form1 : Form, IInteractCallback
        {
    
            public Form1()
            {
                InitializeComponent();
    
            }
    
            public void TaskProgress(int percentageCompleted)
            {
                progressBar1.Value = percentageCompleted;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                InstanceContext context = new InstanceContext(this);
                InteractClient client = new InteractClient(context);
                client.ProgressReport();
            }
        }

    When i run my windows form and click on the button I have this error

    System.ServiceModel.CommunicationException: 'The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.'

    

    In reference.cs file for the method :

    public void ProgressReport() {           
        base.Channel.ProgressReport();       
    }


    Friday, October 25, 2019 2:15 PM

All replies

  • I Copied your code, it works properly.

    https://i.stack.imgur.com/T7MKx.gif

    I suggest you attempt the below actions.
    By default, nettcpbinding use windows authentication, we should provide windows credential.

    ServiceReference1.InteractClient client = new InteractClient(context);
                client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
                client.ClientCredentials.Windows.ClientCredential.Password = "abcd1234!";

    Besides, please use the below configuration on the server-side (I specify the security mode to None, thereby there is no need to specify the credential on the client-side)

        <system.serviceModel>
          <services>
            <service name="ConsoleApp1.Interact">
              <endpoint address="" binding="netTcpBinding" contract="ConsoleApp1.IInteract" bindingConfiguration="mybinding" />
              <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
              <host>
                <baseAddresses>
                  <add baseAddress="net.tcp://localhost:18888"/>
                </baseAddresses>
              </host>
            </service>
          </services>
          <bindings>
            <netTcpBinding>
              <binding name="mybinding">
                <security mode="None">
                </security>
              </binding>
            </netTcpBinding>
          </bindings>
          <behaviors>
            <serviceBehaviors>
              <behavior>
                <serviceMetadata />
                <serviceDebug includeExceptionDetailInFaults="true"/>
              </behavior>
            </serviceBehaviors>
          </behaviors>
    </system.serviceModel>

    Finally, I would like to know the auto-generated configuration on the client-side, the system.servicemodel section in the appconfig in Winform application).

    Feel free to let me know if the problem still exists.
    Best Regards
    Abraham Qian


    Friday, November 1, 2019 9:57 AM
    Moderator
  • hi, I reply with another account I forget my previous account.

    So, I try and I still have my error.

    My auto-generated configuration on the client-side :

    <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding name="NetTcpBinding_IInteract">
              <security mode="None" />
            </binding>
          </netTcpBinding>
        </bindings>
        <client>
          <endpoint address="net.tcp://localhost:8734/Service/Interact/Interact"
            binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IInteract"
            contract="Interact.IInteract" name="NetTcpBinding_IInteract">
            <identity>
              <dns value="localhost" />
            </identity>
          </endpoint>
        </client>
    </system.serviceModel>
    I don't know why you don't have error and the project works maybe you do something different to me.


    • Edited by Soleyne1 Tuesday, November 5, 2019 9:04 AM
    Monday, November 4, 2019 3:27 PM