C# SVC 文件和 WCF 项目之间的关系?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2113461/
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
Relationship between SVC files and WCF projects?
提问by Craig Schwarze
When creating a WCF project, the default member files are just ordinary csharp class files, rather than svc files. Are svc files required with a WCF project? When should they be used?
创建 WCF 项目时,默认成员文件只是普通的 csharp 类文件,而不是 svc 文件。WCF 项目是否需要 svc 文件?什么时候应该使用它们?
采纳答案by Cheeso
.svc files are used when you host your WCF service in IIS.
在 IIS 中托管 WCF 服务时会使用 .svc 文件。
See Microsoft's doc hereand here.
There's a module within IIS that handles the .svc file. Actually, it is the ASPNET ISAPI Module, which hands off the request for the .svc file to one of the handler factory types that has been configured for ASPNET, in this case
IIS 中有一个处理 .svc 文件的模块。实际上,它是 ASPNET ISAPI 模块,它将对 .svc 文件的请求传递给已为 ASPNET 配置的处理程序工厂类型之一,在这种情况下
System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
If you are hosting your WCF service in something other than IIS, then you don't need the .svc file.
如果您在 IIS 以外的其他地方托管 WCF 服务,则不需要 .svc 文件。
回答by Anton
It is possible to create a WCF project and host it in IIS without using a .svc file.
可以在不使用 .svc 文件的情况下创建 WCF 项目并将其托管在 IIS 中。
Instead of implementing your DataContract in your svc code-behind, you implement it in a normal .cs file (i.e. no code behind.)
不是在你的 svc 代码隐藏中实现你的 DataContract,而是在一个普通的 .cs 文件中实现它(即没有代码隐藏。)
So, you would have a MyService.cs like this:
所以,你会有一个像这样的 MyService.cs:
public class MyService: IMyService //IMyService defines the contract
{
[WebGet(UriTemplate = "resource/{externalResourceId}")]
public Resource GetResource(string externalResourceId)
{
int resourceId = 0;
if (!Int32.TryParse(externalResourceId, out resourceId) || externalResourceId == 0) // No ID or 0 provided
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
return null;
}
var resource = GetResource(resourceId);
return resource;
}
}
Then comes the thing making this possible. Now you need to create a Global.asax with code-behind where you add an Application_Start event hook:
然后是使这成为可能的事情。现在您需要创建一个带有代码隐藏的 Global.asax,您可以在其中添加一个 Application_Start 事件挂钩:
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
// Edit the base address of MyService by replacing the "MyService" string below
RouteTable.Routes.Add(new ServiceRoute("MyService", new WebServiceHostFactory(), typeof(MyService)));
}
}
One nice thing about this is that you don't have to handle the .svc in your resource URLs. One not so nice thing is that you now have a Global.asax file.
这样做的一个好处是您不必处理资源 URL 中的 .svc。一件不太好的事情是您现在有一个 Global.asax 文件。
回答by Sean B
If you are using .net 4.0 or later, you can now "simulate" the .svc via config with the following:
如果您使用 .net 4.0 或更高版本,您现在可以通过以下配置“模拟”.svc:
<system.serviceModel>
<!-- bindings, endpoints, behaviors -->
<serviceHostingEnvironment >
<serviceActivations>
<add relativeAddress="MyService.svc" service="MyAssembly.MyService"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
Then you don't need a physical .svc file nor a global.asax
那么你不需要物理 .svc 文件,也不需要 global.asax