如何在单个wcf服务应用程序中维护2个接口? [英] How to maintain 2 interfaces in single wcf service application?

查看:36
本文介绍了如何在单个wcf服务应用程序中维护2个接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在单个wcf服务应用程序中维护2个接口?



如IService1

IService2

How to maintain 2 interfaces in single wcf service application?

like IService1
IService2

推荐答案

您好

您的问题的解决方案取决于您尝试通过

将服务合同分成两个不同的接口来实现的解决方案。

在下面的示例中,我选择了对该问题的简单答案

当前的解决方案取决于WCF基于服务合同通过接口实现并暴露给世界的事实通过端点(具有ABC属性)

基本上,服务实现不会向最终用户公开。

因此我们可以在同一服务实现中实现2个不同的服务契约

并将其暴露为2个终点

检查以下代码:



Hi
The solution for your question is depend in the solution you are trying to achieve by
separating the service contract to 2 different interfaces.
In the following example i have chosen the simple answer to that problem
The current solution depend on the fact that WCF based on Service Contracts Implemented via Interfaces and exposed to the world via Endpoints (with the ABC properties)
Basically the Service Implementation does not exposed to the end user.
So we can implement the 2 different Service Contract in the same service Implementation
And expose it as 2 End Points
Check the following code:

[ServiceContract]
    public interface IFooService
    {
        [OperationContract]
        void Foo();

    }

    [ServiceContract]
    public interface  IBarService
    {
        [OperationContract]
        void Bar();
    }
    
//Service Implementation 
    public class Service : IFooService, IBarService
    {
        //Foo Service Implementation
        public void Foo()
        {
            Console.WriteLine("Foo Service Method Call"); 
        }

        //Bar Service Implementation
        public void Bar()
        {
            Console.WriteLine("Bar Service Method Call");
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (ServiceHost host = new ServiceHost(typeof (Service)))
                {
                    host.Open();

                    Console.WriteLine("S E R V E R");

                    Console.WriteLine("Press any key to shutdown");
                
                    Console.ReadKey();

                    host.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }


<configuration>
  <system.servicemodel>
        <behaviors>
              <servicebehaviors>
                    <behavior name="ServiceBehavior">
                        <servicemetadata httpgetenabled="true" />
                    </behavior>
                    <behavior name="">
                          <servicemetadata httpgetenabled="true" httpsgetenabled="true" />
                          <servicedebug includeexceptiondetailinfaults="false" />
                    </behavior>
              </servicebehaviors>
        </behaviors>
        <services>
              <service behaviorconfiguration="ServiceBehavior" name="ConsoleApplication2.Service">
                    <endpoint address="Foo" binding="basicHttpBinding" name="Foo">
                          contract="ConsoleApplication2.IFooService" />
                    <endpoint address="Bar" binding="basicHttpBinding" name="Bar">
                          contract="ConsoleApplication2.IBarService" />
                    <host>
                          <baseaddresses>
                                <add baseaddress="http://localhost:8733/Design_Time_Addresses/ConsoleApplication2/Service" />
                          </baseaddresses>
                    </host>
              </endpoint></endpoint></service>
             
        </services>
    </system.servicemodel>
</configuration>


这篇关于如何在单个wcf服务应用程序中维护2个接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆