C# 如何生成 .svc 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9925941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:15:35  来源:igfitidea点击:

How do I generate the .svc file?

c#winformswcf

提问by TheGateKeeper

I created my first WCF service and tested it on my computer, and it works.

我创建了我的第一个 WCF 服务并在我的计算机上测试了它,它可以工作。

The files present are an interface, an implementation of that interface, and an app.config file.

存在的文件是一个接口、一个接口的实现和一个 app.config 文件。

Now that it is time to host this on a real server with IIS, I was told IIS looks for a .svc file when receiving incoming calls.

现在是使用 IIS 将其托管在真实服务器上的时候了,有人告诉我 IIS 在接收来电时会查找 .svc 文件。

Here is what I found:

这是我发现的:

WCF services hosted in IIS are represented as special content files (.svc files) inside the IIS application. This model is similar to the way ASMX pages are represented inside of an IIS application as .asmx files. A .svc file contains a WCF-specific processing directive (@ServiceHost) that allows the WCF hosting infrastructure to activate hosted services in response to incoming messages.

IIS 中承载的 WCF 服务表示为 IIS 应用程序内的特殊内容文件(.svc 文件)。此模型类似于 ASMX 页面在 IIS 应用程序内表示为 .asmx 文件的方式。.svc 文件包含 WCF 特定的处理指令 (@ServiceHost),允许 WCF 托管基础结构激活托管服务以响应传入消息。

Can someone please guide me as to how I can create this file so that I may host it?

有人可以指导我如何创建此文件以便我可以托管它吗?

Thanks!

谢谢!

采纳答案by Brad Christie

The thing you need to keep in mind is that IIS is first-and-foremost a web server, and WCF host secondly.

您需要记住的是,IIS 首先是 Web 服务器,其次是 WCF 主机。

The webserver's job is to render data based on an incoming request. Most of this data is content (the request path correlated directly to a file on the server) but in the case of a WCF service IIS needs to know where to go from here (thus the SVC file and the "directives" to IIS to spin up your service).

网络服务器的工作是根据传入的请求呈现数据。这些数据的大部分是内容(请求路径与服务器上的文件直接相关),但在 WCF 服务的情况下,IIS 需要知道从这里去哪里(因此 SVC 文件和到 IIS 的“指令”旋转提升您的服务)。

All the SVC file is doing is saying that at /x/y/z.svcI have a WCF service which is capable of a lotmore than just server-side pages and content files. So please spin it up, make it available and allow my incoming connections to be processed.

所有SVC文件正在做的是说,在/x/y/z.svc我有一个WCF服务,这是一个能的大量不仅仅是服务器端的页面和内容文件。因此,请启动它,使其可用并允许处理我的传入连接。

If this were a WCF service hosted on it's own dedicated port, this would be a different story because it's no longer contending with additional requests for /Styles/base.cssin addition to /MyService/GetSomeObject/.

如果这是托管它自己的专用端口上的WCF服务,这将是一个不同的故事,因为它不再与其他请求争/Styles/base.css除了/MyService/GetSomeObject/

回答by Min Min

IIS Hosted .svcfile consists of the @ServiceHostdirective and attribute, Service.

IIS 托管.svc文件由@ServiceHost指令和属性Service 组成

<% @ServiceHost Service="MyNamespace.MyServiceImplementationTypeName" %>

The value of the Serviceattribute is the CLR type name of your service implementation. Using this directive is basically equivalent to creating a service host using the following code in your self hosting console program.

Service属性的值是服务实现的 CLR 类型名称。使用此指令基本上等同于在您的自托管控制台程序中使用以下代码创建服务主机。

new ServiceHost(typeof(MyNamespace.MyServiceImplementationTypeName ));

And If your self hosted application are using WCF configuration like 'endpoint', 'binding',etc in the app.config, you can also put that in web.config. IIS-hosted service use the same configuration elements and syntax as WCF services hosted outside of IIS. (Except something like you can't control base/endpoint address in IIS-hosted service.) And put your precompiled .dll file to application's \bin directory of your IIS Site.

如果您的自托管应用程序在app.config中使用 WCF 配置,如“端点”、“绑定”等,您也可以将其放在web.config 中。IIS 托管的服务使用与 IIS 外部托管的 WCF 服务相同的配置元素和语法。(除非您无法控制 IIS 托管服务中的基址/端点地址。)并将预编译的 .dll 文件放入 IIS 站点的应用程序 \bin 目录中。

And the address of IIS-hosted service will be the address of .svc file. (http://localhost/Application1/MyService.svc).

IIS 托管服务的地址将是 .svc 文件的地址。( http://localhost/Application1/MyService.svc)

Please check the below msdn - Deploying an IIS-Hosted WCF Service.

请查看以下 msdn -部署 IIS 托管的 WCF 服务

http://msdn.microsoft.com/en-us/library/aa751792.aspx

http://msdn.microsoft.com/en-us/library/aa751792.aspx