简体   繁体   English

如何在IIS服务器上运行简单的服务器进程

[英]How do I run a simple server process on an IIS server

I was not quite sure how to phrase this question. 我不太确定如何表达这个问题。

What I want to do is create a simple server type application in C# which simply listens for incoming socket connections and handles them the way I specify. 我想做的是用C#创建一个简单的服务器类型应用程序,该应用程序仅侦听传入的套接字连接并按照我指定的方式处理它们。 It could be a chat application or something like that, very simple. 它可以是聊天应用程序或类似的东西,非常简单。

I looked at the example: http://msdn.microsoft.com/en-us/library/fx6588te.aspx about asynchronous sockets. 我看了一个例子: http : //msdn.microsoft.com/en-us/library/fx6588te.aspx关于异步套接字。

I understand the code in the example except I'm uncertain about how the code would execute. 我理解示例中的代码,但不确定如何执行代码。 Specifically, what would be the entry point of the application in the example? 具体来说,示例中应用程序的入口是什么? How would the server know to start it? 服务器将如何知道如何启动它?

My only experience with IIS is in web forms with C# codebehind. 我对IIS的唯一经验是使用C#代码隐藏的Web表单。 I understand that the code for web forms is executed when the url points to the location and then again on postbacks etc. 我知道网址指向的位置会执行Web表单的代码,然后在回发时再次执行。

Where it gets foggy for me is when I don't want to use a web form but simply a collection of C# class files with a single entry point (similar to a java application). 当我不想使用Web表单,而只是使用具有单个入口点的C#类文件的集合时(对Java应用程序来说),这对我来说就很模糊。

Edit for clarity: 为清楚起见进行编辑:

The goal of what I want to do is to create an application which I can put on my web host which will continuously accept requests from client applications and handle them the way I will specify and then return information to the clients. 我想做的目标是创建一个可以放在我的Web主机上的应用程序,该应用程序将不断接受来自客户端应用程序的请求,并按照我指定的方式处理它们,然后将信息返回给客户端。 I'm just not sure how to tell the server to start the application since I only know about web forms. 我只是不确定如何告诉服务器启动应用程序,因为我只了解Web表单。
Also, it would be fine if I needed to initially direct my browser to a web form and, say, press a button to start the application. 另外,如果我需要首先将浏览器定向到Web表单,例如按一个按钮来启动应用程序,那也很好。 In fact, it would be ideal if I could start and stop the application at will. 实际上,如果我可以随意启动和停止应用程序,那将是理想的选择。 Also, I may have used incorrect terminology. 另外,我可能使用了错误的术语。 I thought IIS servers were what you called a server which can run asp.net applications. 我以为IIS服务器就是您所说的可以运行asp.net应用程序的服务器。 I could be very wrong about this. 我对此可能是非常错误的。

Thanks. 谢谢。

you cannot use IIS to start windows applications, you need a windows service application that constantly listens to the port specified, and that windows service application needs to be scheduled to start either at system start up or at any event of your choice. 您不能使用IIS来启动Windows应用程序,您需要一个Windows Service应用程序来不断侦听指定的端口,并且需要安排Windows Service应用程序在系统启动或您选择的任何事件下启动。 and that windows service applications needs to stay in memory as long as you want your app to function. 并且Windows服务应用程序需要保留在内存中,只要您希望您的应用程序能够运行。

PS: your question seriously needs some editing, but I am also new to SO, so I will let respectable senior users to do what they are best at. PS:您的问题确实需要进行一些编辑,但是我也是SO的新手,所以我会让受尊敬的高级用户来做他们最擅长的事情。

EDIT: If you want to simulate windows service using IIS than here are your closest bets, please follow the links to know what you need to know. 编辑:如果您想使用IIS模拟Windows服务,而不是这是您最近的选择,请点击链接以了解您需要知道的内容。

Simulate Windows Service Using ASP.NET App 使用ASP.NET App模拟Windows服务

Forcing your Application to Stay alive 强制您的应用程序继续运行

These two links will help you keep alive your application and bamm, you can create any number of classes (Java style as you quoted) to perform whatever tasks you want it to perform 这两个链接将帮助您保持应用程序和bamm的生命力,您可以创建任意数量的类(引用的Java样式)来执行希望其执行的任何任务

It sounds to me like you don't want to use IIS at all. 在我看来,您根本不想使用IIS。 IIS handles the listening for you and is the "service" to manage requests. IIS为您处理侦听,它是管理请求的“服务”。 If you are lookng to manually listen for incoming connections, then you will need the following components: 如果要手动侦听传入的连接,则将需要以下组件:

  1. A windows service running that has.. 正在运行的Windows服务。

  2. A http listener built into it. 内置了一个HTTP侦听器。

Take a look at the C# HttpListener class and look at the process for building a window service that can run in the background of your server. 看一下C#HttpListener类,看一下构建可在服务器后台运行的窗口服务的过程。

This isn't all that difficult, but I'm not so sure it's what you need. 这并不困难,但是我不确定这就是您所需要的。 If you don't want to use webforms, you can have a web application that resolves requests straight to custom handlers which i THINK is what you're actually looking for and makes having your own listener overkill. 如果您不想使用Web表单,则可以使用一个Web应用程序将请求直接解析为自定义处理程序,而我认为这正是您真正要查找的内容,并且使您自己的侦听器过大了。

EDIT: Additional info on custom handlers 编辑:有关自定义处理程序的其他信息

Here is a start on what a custom handler is and how to use it: http://support.microsoft.com/kb/308001 这是一个关于自定义处理程序的含义以及使用方法的开始: http : //support.microsoft.com/kb/308001

I would also look at some beginner articles on MVC3 from .net. 我还将看看.net上有关MVC3的一些初学者文章。 MVC is a framework that doesn't have any of the fluff (such as viewstate) that webforms has and allows you to route a URL (request) to a Controller (class) and return pretty much anything you want. MVC是一个框架,它没有webforms具有的任何绒毛(例如viewstate),并且允许您将URL(请求)路由到Controller(类)并返回几乎任何您想要的东西。 There are a lot of advantages to using MVC and if you are coming from a java/pure http background it will make much more sense than webforms. 使用MVC有很多优点,如果您来自Java /纯HTTP背景,那么它比Webforms有意义得多。

You can get started with that one by searching around for "getting started with .net MVC3" or even start with www.asp.net for beginner resources. 您可以搜索“ .net MVC3入门”,甚至从www.asp.net开始获取初学者资源,从而开始使用它。

Unless your goal is to learn low level TCP communication, writing and configuring services, dealing with code changes, writing own serialization/request parsing than sticking with IIS and HTTP maybe easier approach. 除非您的目标是学习底层TCP通信,编写和配置服务,处理代码更改,编写自己的序列化/请求解析,否则比坚持IIS和HTTP可能更简单。

There is no need to use WebForms - WebService (ASP.Net or even WCF) or MVC Web API will give you ability to implement methods you want without need to write your own custom serialization and request parsing infrastructure. 无需使用WebForms-WebService(ASP.Net甚至WCF)或MVC Web API将使您能够实现所需的方法,而无需编写自己的自定义序列化和请求解析基础结构。 You can even do long poll if/when needed (ie SignalR ). 您甚至可以在需要时进行长轮询(即SignalR )。

I'd start with basic MVC application reporting JSON results as server side as it will still show all issues involved with writing chat client (persistence, discovery of other clients, quick status updates) while allowing you to focus on communication itself (including easily see all traffic if need). 我将从基本的MVC应用程序作为服务器端报告JSON结果开始,因为它仍将显示编写聊天客户端所涉及的所有问题(持久性,发现其他客户端,快速更新状态),同时允许您专注于通信本身(包括轻松查看)所有流量(如果需要)。

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

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