1

In .NET (meaning .NET Core and .NET), compared to the .NET Framework, this approach has been changed, and, apparently, significantly.

In the .NET Framework, a client-server application was organized like this:

For the server:

[ServiceContract]
    interface IContract
    {
        [OperationContract]
        void say_something(string s);

        [OperationContract]
        int sum(int a, int b);
    }

class Service: IContract
    {

        public int sum(int a, int b)
        {
            return a + b;
        }

        void IContract.say_something(string s)
        {
            MessageBox.Show(s);
        }
    }

class Server
      {
        ServiceHost host;

        public void open_host()
        {
            Uri address = new Uri(Global.uri_local);                    // A

            BasicHttpBinding binding = new BasicHttpBinding();          // B

            Type contract = typeof(IContract);                          // C

            host = new ServiceHost(typeof(Service));

            host.AddServiceEndpoint(contract, binding, address);

            host.Open();
        }
}

Settings
class Global
    {
        public static string uri_local = "http://localhost:4000/IContract";
    }

For client:

class Client
    {
        IContract channel;


        public Client()
        {
            create_channel();
        }

        public void create_channel()
        {
            Uri address = new Uri(Global.uri_local);

            BasicHttpBinding binding = new BasicHttpBinding();

            EndpointAddress endpoint = new EndpointAddress(address);

            ChannelFactory<IContract> factory = new ChannelFactory<IContract>(binding, endpoint);

            channel = factory.CreateChannel();
        }

        public void send_message_to_server(string text)
        {
            channel.say_something(text);
        }


        public void find_sum_of_3_and_2()
        {
            MessageBox.Show(channel.sum(2, 3).ToString());
        }
    }

[ServiceContract]
    interface IContract
    {
        [OperationContract]
        void say_something(string s);

        [OperationContract]
        int sum (int a, int b);
    }

Settings
class Global
    {
        public static string uri_local = "http://localhost:4000/IContract";
    }

However, in .NET, everything is organized differently. I have been searching and could not find examples on the Internet. Maybe I'm looking in the wrong place. MSDN has an example for the .NET Framework, but not for .NET. At the same time, I saw this question on some forums, but the answers to it were only a statement of fact: in .NET, Microsoft changed the approach to organizing client-server interaction. I think if you give an example, it will be useful for many seekers.

How is client-server interaction organized in .NET?

Please, give an example of a simple client-server application.

Edward
  • 23
  • 3
  • Going Asp.Net Core and using the REST approach? – Ralf Jun 21 '22 at 13:07
  • Are you trying to implement calling wcf service in ASP.NET Core? You can use [the WCF Web Service Reference Provider Tool](https://learn.microsoft.com/en-us/visualstudio/data-tools/how-to-add-update-or-remove-a-wcf-data-service-reference?view=vs-2022#to-add-a-reference-to-an-external-service-net-core-projects-including-net-5-and-later). – Lan Huang Jun 22 '22 at 08:38
  • @LanHuang Ok. Thank you! But how to set the address for the service ("HostBuilder" class), so that later it can be used for connection? – Edward Jul 12 '22 at 16:44
  • I think you want to set , you can refer to the documentation.https://learn.microsoft.com/en-us/dotnet/framework/wcf/samples/addressing – Lan Huang Jul 13 '22 at 06:34

0 Answers0