WCF的实现,不使用接口 [英] WCF implementing without using interface

查看:161
本文介绍了WCF的实现,不使用接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个WCF服务,我想实现一些适用于我所有服务的基本类逻辑,例如身份验证和其他基本检查.默认实现创建一个要实现的接口.我显然不能向其添加具体的基类函数.

所以问题是..在实现WCF服务时不使用接口是否有不利之处?

感谢

Hi guys,

I have a WCF Service and I would like to implement some base class logic such as authentication and other basic checks which applies to all my services. The default implementation creates an interface to implement. I can''t obviously then add concrete base class functions to this.

So the question is.. is there any downside to not using an interface when implementing a WCF Service?

thanks

推荐答案

嗨Ashman786,

通常,在没有接口的情况下实现服务是个坏主意,因为接口代表了服务协定.无论如何,我看不出任何原因你都不能做两者".

看一下示例代码"structure".

您可以使用继承-也可以使用Interfaces.

所以你有:

常见服务操作的接口,如下所示:

Hi Ashman786,

In General it is a bad idea to implement Services without Interfaces, cause the Interface represents the Service contract. Anyway I don''t see any reason why you can''t do "both".

Look at this example code "structure".

You can work with inheritance - also with Interfaces.

So you have:

a Interface for common Service operations like this:

[ServiceContract]
public interface ICommonServiceMethods
{
    [OperationContract]
    string WriteLog(string strMessage);
}



从通用操作接口继承的特定服务接口,如下所示:



a specific Service Interface inherited from the common operations Interface like this:

[ServiceContract]
public interface IService1 : ICommonServiceMethods
{
    [OperationContract]
    string GetData(int value);
}



您可以在基类中实现常见的Service操作(可能是抽象的)



You can implement your common Service operations in a base class (could be abstract)

public class BaseService : ICommonServiceMethods
 {
     public string WriteLog(string strMessage)
     {
         return strMessage; //dummy
     }
 }



然后只需在实现特定Service的同时从基类派生特定Service实例即可.



And then just derive your specific Service instance from the base class while implementing the specific Service.

public class Service1 : BaseService, IService1
{
    public string GetData(int value) { return value.ToString(); /* dummy */}
}




因此,无需考虑省略接口"-尽管从技术上讲,您可以...

希望对您有帮助吗?

亲切的问候约翰内斯




So no need to think about omitting "Interfaces" - although technically you could...

I hope this helps?

Kind regards Johannes


这篇关于WCF的实现,不使用接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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