简体   繁体   中英

Using a Wrapper project for a single WCF service added to multiple projects in a solution

I have a solution that includes a number of projects that are connected to a single WCF service. How can I add that single WCF service to a Wrapper project and have other projects referenced only to that Wrapper project? Is there a best practice solution to this?

If we add that WCF service to a Wrapper project, the Wrapper project is the host for the service. This is the structure of my Wrapper project.

enter image description here

If other projects want referenced only to the Wrapper project, the service method can only invoke as a normal method, if we want to invoke the service as a service method, we can invoke it by add a service reference. This is the other project that references the service.

using Project1.ServiceReference1;
using System;
using Wrapper;

namespace Project1
{
    class Program
    {
        static void Main(string[] args)
        {
            DemoService service = new DemoService();           
            Console.WriteLine(service.Say());

            DemoServiceClient client = new DemoServiceClient();
            Console.WriteLine(client.Say());
            Console.Read();
        }
    } 
}

If the Wrapper project calls the WCF service through the service reference and other projects only reference the Wrapper project, the WCF service cannot be called, because the configuration file in the project needs to be read when creating the proxy.

I think the best solution is to call the WCF service through the channel factory. We will create some configuration of the channel factory in the Wrapper project, and then other projects will reference the Wrapper project,here is a Demo:

public  class Channel {
       
     
        private EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8000/GettingStarted/CalculatorService");
        public EndpointAddress endpoint {
            get
            {
                return this.endpointAddress;
            }
            set
            {
                this.endpointAddress = value;
            }

        }
        private BasicHttpBinding binding = new BasicHttpBinding();
        public BasicHttpBinding basic
        {
            get
            {
                return this.binding;
            }
            set
            {
                this.binding = value;
            }

        }

    }

Create a channel class in the Wrapper project. The channel class contains information about the endpoint of the WCF service. We can use this information to create a channel factory to call the service.

In addition, you need to create a service contract in the Wrapper project, which is the interface of the WCF service:

 [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }

Finally, we can directly refer to the Wrapper project in other projects to create a channel factory call service:

ICalculator calculator = ChannelFactory<ICalculator>.CreateChannel(new Channel().basic,new Channel().endpoint);
double res = calculator.Add(1,2);

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