简体   繁体   English

在多个Web应用程序工作进程中运行自托管的WCF

[英]Running WCF self-hosted in multiple web application worker processes

In a web application I run a simple wcf host : 在Web应用程序中,我运行一个简单的wcf主机

Uri baseAddress = new Uri("http://localhost:8080/hello");

using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
{
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
    host.Description.Behaviors.Add(smb);

    host.Open();
}

I wrote it at Global.asax/Start . 我在Global.asax/Start编写了它。 So it opens and listens on the port 8080 at the beginning of the my web application and I can invoke any methods defined in my HelloWorldService through http://localhost:8080/hello . 因此,它将在Web应用程序的开头打开并监听端口8080 ,并且我可以通过http://localhost:8080/hello调用HelloWorldService定义的任何方法。 I have published it on IIS and it works too and everything is ok. 我已经在IIS上发布了它,它也可以正常运行,并且一切正常。

The problem arises when I increase the Maximum Worker Processes value to 2 or more than 1 . 当我将Maximum Worker Processes增加到2或大于1时,就会出现问题。

The point is that every worker process is a separated process which want to obtain port 8080 but the OS does not allow to more than one process to get it. 关键是每个工作进程都是一个单独的进程,要获取端口8080,但OS不允许一个以上的进程来获取端口。

It seems the problem will solve by handling exception, so just one of processes will get the port. 看来问题可以通过处理异常来解决,因此只有一个进程可以获取端口。 but that is not true! 但这不是真的! When recycle happens for the process which have the port, there is no guarantee a new worker process be executed immediately. 当具有端口的进程发生回收时,不能保证立即执行新的辅助进程。

Notice: Any solutions which separates host and my web application is not acceptable, because the all business inside of service will be injected and controlled by my web application. 注意:不能将主机和Web应用程序分开的任何解决方案,因为服务内部的所有业务都将由我的Web应用程序注入和控制。

I have an idea. 我有个主意。 I will take different ports in every process and run them. 我将在每个进程中使用不同的端口并运行它们。 Also I have read Application Request Routing to use load balancing feature to disperse one particular port to several different ports which has obtained by a worker process. 我也阅读了《 应用程序请求路由》,以使用负载平衡功能将一个特定端口分散到工作进程获得的多个不同端口。

在此处输入图片说明

please appreciate me or give me a solution to do it. 请感谢我或给我解决方案。

You can use port pool. 您可以使用端口池。 On global.asax choose one of the unused port. 在global.asax上,选择未使用的端口之一。

Web application may not a good choice to host WCF service. Web应用程序可能不是托管WCF服务的不错选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM