WCF 服务可以有构造函数吗? [英] Can WCF Service have constructors?

查看:19
本文介绍了WCF 服务可以有构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在我的解决方案中新建一个 WCF 服务时,我可以执行以下操作,有一个带参数的构造函数传入吗?如果是,运行时如何、何时以及在何处填充我所需的 IBusinessLogic 对象?

When I new a WCF service in my solution, can I do the following, have a constructor with parameter to pass in? If yes, how, when and where does the runtime fill in my required IBusinessLogic object?

[ServiceContract]
public interface IServiceContract
{
    [OperationContract]
    ...
}

public class MyService : IServiceContract
{
    IBusinessLogic _businessLogic;
    public ServiceLayer(IBusinessLogic businessLogic)
    {
        _businessLogic = businessLogic;
    }
    ...
}

推荐答案

开箱即用的 WCF 将仅使用默认构造函数,您不能使用参数化构造函数.您必须做一些额外的工作才能使 WCF 调用参数化构造函数.

Out of the box WCF will only use the default constructor, you can't use parameterised constructors. You have to do a bit of extra work to make WCF call parameterised constructors.

你可以试试这个:

如何我如何将值传递给 wcf 服务的构造函数?

这篇关于WCF 服务可以有构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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