创建WCF项目时,默认成员文件只是普通的csharp类文件,而不是svc文件.WCF项目是否需要svc文件?什么时候应该使用它们?
Che*_*eso 39
在IIS中托管WCF服务时使用.svc文件.
IIS中有一个处理.svc文件的模块.实际上,它是ASPNET ISAPI模块,它将.svc文件的请求移交给已为ASPNET配置的处理程序工厂类型之一,在这种情况下
System.ServiceModel.Activation.HttpHandler,System.ServiceModel,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089
如果您在IIS之外的其他东西托管您的WCF服务,那么您不需要.svc文件.
Sea*_*n B 19
如果您使用的是.net 4.0或更高版本,现在可以使用以下命令通过配置"模拟".svc:
<system.serviceModel>
<!-- bindings, endpoints, behaviors -->
<serviceHostingEnvironment >
<serviceActivations>
<add relativeAddress="MyService.svc" service="MyAssembly.MyService"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
然后,您不需要物理.svc文件或global.asax
Ant*_*ton 16
有点古老的问题,但是对于Google员工来说......
实际上,可以创建一个WCF项目并在IIS中托管它而不使用.svc文件.
不是在svc代码隐藏中实现DataContract,而是在普通的.cs文件中实现它(即没有代码.)
所以,你会有这样的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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后是让这成为可能的事情.现在,您需要创建一个带有代码隐藏的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)));
}
}
Run Code Online (Sandbox Code Playgroud)
关于这一点的一个好处是您不必处理资源URL中的.svc.一个不太好的事情是你现在有一个Global.asax文件.
| 归档时间: |
|
| 查看次数: |
33997 次 |
| 最近记录: |