托管在IIS上一个WCF Web服务时显式调用服务的构造WCF、IIS、时显式、Web

2023-09-03 20:34:50 作者:少年的初恋

我想举办我的微软IIS(IIS托管)。

I want to host a WCF service of mine on Microsoft IIS (IIS hosting).

要做到这一点,我创建了我的服务:

To do this, I created my service:

// The service
public class MyService : IMyService {
    // Ctors
    public MyService() {
        // Def ctor: I don't want to call it
    }
    public MyService(...) : this() {
        // Parametric ctor, I want to call it!
    }
   ...
}

// The contract
[ServiceContract]
public interface IMyService {
    ...
}

我创建了一个SVC文件(一个好方法,给基地址到我的服务):

I created a svc file (a good approach to give a base address to my service):

<@ServiceHost Service="MyService" @>

但这样做,我的托管服务时(只是建立在IIS指向在我的应用程序所在的文件夹中的虚拟目录,通常是项目目录),IIS将调用默认的构造函数。

But doing so, when hosting my service (simply creating a virtual directory in IIS pointing to the folder where my application resides, the project directory usually), IIS will call the default constructor.

如何使IIS调用不同的构造函数?

How do I make IIS call a different constructor?

PS:我知道这是可以指定一个 HostServiceFactory 。有什么事情我应该在这里使用?这使我回厂的主机。对于主机,我不能作用于主机传递的参数。但是,我该如何解决这个问题?

PS: I know that it is possible to specify a HostServiceFactory. Is it something I should use here? It gives me back the factory and a host. For the host, I cannot act on the host passed parameters. However, how do I solve this problem?

注:我了解,很多解决方案都基于控制 反转(IoC)和几个如团结,的城堡项目和 Spring.NET 。不过,我不会真的使用它们。可以 WCF 自己做呢?我无法接受的是WCF不能让一个程序员主机服务,通过构建它以适当的方式......

NOTE: I understood that many solutions are based on Inversion of control (IoC) and several IoC frameworks like Unity, Castle Project and Spring.NET. However I would not really use them at all. Can WCF make this on its own? I cannot accept that WCF cannot let a programmer host a service by constructing it in the appropriate manner...

推荐答案

您必须编写一些自定义的类来支持与参数创建服务:

You must write several custom classes to support creating service with parameters:

在自定义类实现 IInstanceProvider 。这个类将负责与您的非默认构造函数创建服务实例。 在自定义类实现 IServiceBehavior接口。这个类将负责添加自定义实例提供到终点调度。 在自定义的ServiceHost 将适用你的行为。 在自定义 ServiceHostFactory 将实例化您的自定义服务主机。你会从.svc文件中引用这个工厂。 Custom class implementing IInstanceProvider. This class will be responsible for creating your service instance with your non default constructor. Custom class implementing IServiceBehavior. This class will be responsible for adding custom instance provider into endpoint dispatcher. Custom ServiceHost which will apply your behavior. Custom ServiceHostFactory which will instantiate your custom service host. You will reference this factory from .svc file.

这是大致相同,构建依赖注入的支持。您可以检查,例如this文章。

This is generally the same as building support for dependency injection. You can check for example this article.

 
精彩推荐
图片推荐