Uruchamianie serwera i klienta na roznych komputerach w sieci

0

cześć, jestem początkujący, mam napisanego hosta, klienta i kontrakt i wszystko ładnie śmiga na moim komputerze, jednak mam za zadanie tak to zmodyfikować żeby można było uruchomić serwer i klienta na dwóch rożnych komputerach w sieci i kompletnie nie mam na to pomysłu, z góry dziękuje za pomoc

MOJE KODY

WcfServiceContract

  • IService1
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfServiceContract
{
    [ServiceContract]
    public interface IKalkulator
    {
        [OperationContract]
        double Dodaj(double n1, double n2);
        [OperationContract]
        double Odejmij(double n1, double n2);
        [OperationContract]
        double Pomnoz(double n1, double n2);
        [OperationContract]
        double Podziel(double n1, double n2);
    }
}
 
  • Service1
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfServiceContract
{
    public class mojKalkulator : IKalkulator
    {
        public double Dodaj(double n1, double n2)
        {
            double result = n1 + n2;
            Console.WriteLine("Warotsci: ({0},{1}) ", n1, n2);
            // Code added to write output to the console window.
            Console.WriteLine("Wynik: {0}", result);
            return result;
        }

        public double Odejmij(double n1, double n2)
        {
            double result = n1 - n2;
            Console.WriteLine("Warotsci: ({0},{1}) ", n1, n2);
            Console.WriteLine("Wynik: {0}", result);
            return result;
        }

        public double Pomnoz(double n1, double n2)
        {
            double result = n1 * n2;
            Console.WriteLine("Warotsci: ({0},{1}) ", n1, n2);
            Console.WriteLine("Wynik: {0}", result);
            return result;
        }

        public double Podziel(double n1, double n2)
        {
            double result = n1 / n2;
            Console.WriteLine("Warotsci: ({0},{1}) ", n1, n2);
            Console.WriteLine("Wynik: {0}", result);
            return result;
        }
    }
}
 

WcfServiceHost

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using WcfServiceContract;
using System.ServiceModel.Description;

namespace GettingStartedHost
{
    class Program
    {
        static void Main(string[] args)
        {
            // Step 1 Create a URI to serve as the base address.
            Uri baseAddress = new Uri("http://localhost:7000/KalkulatorSerwis/");

            // Step 2 Create a ServiceHost instance
            ServiceHost mojHost = new ServiceHost(typeof(mojKalkulator), baseAddress);

            try
            {
                // Step 3 Add a service endpoint.
                WSHttpBinding mojBanding = new WSHttpBinding();
                mojHost.AddServiceEndpoint(typeof(IKalkulator),
                    mojBanding,
                    "Usluga1");

                // Step 4 Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                mojHost.Description.Behaviors.Add(smb);

                // Step 5 Start the service.
                mojHost.Open();
                Console.WriteLine("Serwis uruchomiony");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

                // Close the ServiceHostBase to shutdown the service.
                mojHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("Wystapil wyjatek {0}", ce.Message);
                mojHost.Abort();
            }
        }
    }
}

WcfServiceClient

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WcfServiceclient.ServiceReference1;

namespace WcfServiceClient
{
    class Program
    {
        static void Main(string[] args)
        {
            KalkulatorClient mojKlient = new KalkulatorClient();

            double n1 = 100;
            double n2 = 15;
            double result = mojKlient.Dodaj(n1, n2);
            Console.WriteLine("Dodaj({0},{1}) = {2}", n1, n2, result);

            n1 = 145;
            n2 = 76;
            result = mojKlient.Odejmij(n1, n2);
            Console.WriteLine("Odejmij({0},{1}) = {2}", n1, n2, result);

            n1 = 9;
            n2 = 81;
            result = mojKlient.Pomnoz(n1, n2);
            Console.WriteLine("Pomnoz({0},{1}) = {2}", n1, n2, result);

            n1 = 22;
            n2 = 7;
            result = mojKlient.Podziel(n1, n2);
            Console.WriteLine("Podziel({0},{1}) = {2}", n1, n2, result);

            mojKlient.Close();

        }
    }
}
1

Twój serwer ma zdefiniowane na jakim porcie i hoście nasłuchuje: Uri baseAddress = new Uri("http://localhost:7000/KalkulatorSerwis/");. Możesz to zmienić np. na *:7000/KalkulatorSerwis/ i powinien nasłuchiwać na wszystkich adresach IP serwera.

A w kliencie, tam gdzie masz KalkulatorClient mojKlient = new KalkulatorClient(); możesz dodać:

mojKlient.Endpoint.Address = new EndpointAddress(new Uri("tutaj nowy URL z IP serwera"), client.Endpoint.Address.Identity, client.Endpoint.Address.Headers);
mojKlient.Open();

Albo zmienić w konfiguracji aplikacji (pliku .config, tam powinno chyba być też z tą opcją).

1 użytkowników online, w tym zalogowanych: 0, gości: 1