简体   繁体   中英

How to call WCF service from another service method?

I have code in my app:

[ServiceContract(Namespace = "")]
public class Service1
{
    [OperationContract]
    public object SomeMethod1(){}
}

[ServiceContract(Namespace = "")]
public class Service2
{
    [OperationContract]
    public object SomeMethod2()
    {
        var result = new Service1().SomeMethod1();
    }
}

Do I have to use some proxy to call service1 method or is it the right way?

We cannot do this. In WCF, each ServiceContract corresponds to an endpoint, and calling different endpoints requires different proxies because the address information of these endpoints is not the same.

Here is a demo:

 [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
    }
    [ServiceContract]
    public interface ICalculator2
    {
        [OperationContract]
        double Subtract(double n1, double n2);
    }
    public class CalculatorService : ICalculator,ICalculator2
    {
        public double Add(double n1, double n2)
        {
            double result = n1 + n2;
            Console.WriteLine("Received Add({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Subtract(double n1, double n2)
        {
            double result = n1 - n2;
            Console.WriteLine("Received Subtract({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Step 1: Create a URI to serve as the base address.
            Uri baseAddress = new Uri("http://localhost:8000/GettingStarted/");

            // Step 2: Create a ServiceHost instance.
            ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);

            try
            {
                // Step 3: Add a service endpoint.
                selfHost.AddServiceEndpoint(typeof(ICalculator), new BasicHttpBinding(), "CalculatorService");
                selfHost.AddServiceEndpoint(typeof(ICalculator2), new BasicHttpBinding(), "CalculatorService2");
                // Step 4: Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);

                // Step 5: Start the service.
                selfHost.Open();
                Console.WriteLine("The service is ready.");

                // Close the ServiceHost to stop the service.
                Console.WriteLine("Press <Enter> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();
                selfHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
        }
    }

There are two ServiceContracts in the above service, one is ICalculator and the other is ICalculator2. Their endpoint addresses are not the same.

When we add the service reference, it will generate two proxy classes, and the endpoint addresses of the two proxy classes are different:

在此处输入图像描述

在此处输入图像描述

So we cannot use the same proxy class to call different ServiceContracts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM