c# - 使用 ServiceHost 类及其构造函数了解 WCF 自托管方法

标签 c# .net wcf service

我对 WCF 中的服务托管有一些疑问。

我想使用自托管方法来托管服务。 ServiceHost 类可以派上用场。 通过使用它,可以托管可直接访问 Windows 托管框架的服务。 那么,请考虑以下方法:

0) 常见假设:所有情况都假设配置文件 App.config 用于设置端点的 ABC。所以在下面的代码中没有提到端点,只是不要理会它。 我们也会考虑此服务:

[ServiceContract]
public interface IMyService {
   [OperationContract]
   string MyOp1(int myint);
   [OperationContract]
   string MyOp2(int myint);
}
public class MyService : IMyService {
   // This service needs to be constructed providing at least a string or an integer, if an attempt to construct it wothout passing any of these is done, the service should raise an error.
   MyService(string strparam) { ... }
   MyService(int intparam) { ... }
   MyService(string strparam, int intparam) { ... }
   public string MyOp1(int myint) { ... }
   public string MyOp2(int myint) { ... }
}
public class MyStandaloneService : IMyService {
   // This service does not need to be constructed.
   MyStandaloneService() { ... }
   public string MyOp1(int myint) { ... }
   public string MyOp2(int myint) { ... }
}

1) 案例 1:可以使用 ServiceHost 类的重载来托管服务:

public ServiceHost(
   Type serviceType,
   params Uri[] baseAddresses
)

通过使用它,可以让框架管理服务实例化,因为只需要服务类型。当然, build 是服务的基础 build 。将调用无参数构造函数。在处理不需要特殊构造的服务时,这种重载很好......某种独立服务:

using (ServiceHost host = new ServiceHost(typeof(MyStandaloneService))) {
   host.Open();
   ...
   host.Close();
}

2) 案例 2:可以使用 ServiceHost 类的重载来托管服务:

public ServiceHost(
   Object singletonInstance,
   params Uri[] baseAddresses
)

通过使用它,可以实例化一个服务,然后在不让框架处理这个的情况下托管它……这种方法在处理需要特殊处理且不是完全独立的服务时非常有用:

MyService MS = new MyService("the string");
using (ServiceHost host = new ServiceHost(MS)) {
   host.Open();
   ...
   host.Close();
}

好吧,我想了解以下内容:

A) 在案例 1 中,可以通过提供类型自动托管服务。如果我尝试创建另一个相同类型的服务 (MyStandaloneService),是否会因为尝试创建两个相同的服务而导致错误?在这种情况下,我可能应该对端点配置进行硬编码,因为使用配置文件将导致在同一地址托管两个相同的服务。

B) 在 CASE 2 中,MSDN 文档说这会创建服务的单例实例。因此,如果我尝试以这种方式托管另一项服务:

MyService MS = new MyService("the string");
MyService MS2 = new MyService(23);
ServiceHost host = new ServiceHost(MS));
ServiceHost host2 = new ServiceHost(MS2));
host.Open();
host2.Open();
...
host.Close();
host2.Close();

我会得到一个错误吗?

C) 如果我想避免单例实例化,我应该怎么做?

谢谢

最佳答案

首先,您可能需要查看 ServiceBehaviorAttribute MSDN 文章。

我在这里不做详细介绍,但是您可以创建一个服务对象实例来处理ALL 请求(按顺序,即在其他),或者让 ServiceHost 对象为每个请求 创建一个服务对象,并在不同的线程中同时 处理它们。

一旦您决定了哪种方法最适合您的应用程序,您就会了解要使用哪个 ServiceHost 构造函数重载。您的CASE 1 对应于多实例同时处理方法,CASE 2 对应于“一个实例处理所有实例”的方法。

ServiceHost 重载必须与 MyService 类上的 [ServiceBehavior] 属性密切相关。所以,请查看我上面提供的链接。

编辑:现在回答您的问题:

A) If I attempt to create another service of the same type (MyStandaloneService), does it result in an error because trying to create two same services?

不,这将由 ServiceHost 完成:它将为每个请求创建一个服务实例(事实上,每个 session ,但再次阅读 MSDN)

B) In CASE 2 the MSDN documentation says this creates a singleton instance of the service. So If I attempt to host another service in this way (...) Would I get an error?

您不能同时托管两个具有相同 ABC 的服务,所以是的。如果您将它们托管在不同的端点上,没关系。这里的“单例”意味着一个单个服务实例将处理所有请求。

C) If I wanted to avoid singleton instantiation what should I do?

使用CASE 1方法:)

关于c# - 使用 ServiceHost 类及其构造函数了解 WCF 自托管方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6033435/

相关文章:

c# - 你如何使用C#从pdf文件中提取图像

c# - 页面加载时打印 pdf

c# - 脚手架上的 MVC 防伪 token 错误

c# - 获取客户端机器名

c# - Caliburn - 子 ViewModel 的 PropertyChanged

c# - 如何将多个对象序列化为现有的 XmlDocument,而无需在每个组件上设置 namespace ?

c# - 为什么我不能将 boxed int 转换为可为 null 的 decimal?

c# - 具有 void 返回类型的异常和 WCF 服务

c# - 我需要做什么才能在 WCF 服务中使用 System.Windows.Forms.WebBrowser 控件?

wcf - Azure 中的单一并发模式 WCF 服务