public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
private static void RegisterRoutes(ICollection<RouteBase> routes)
{
routes.Add(new ServiceRoute("Calculator", new WebServiceHostFactory(), typeof(CalculatorService)));
}
}
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true">
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory"
relativeAddress="Calculator.svc"
service="MyServer.CalculatorService"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
当我这样做并转到http://localhost/MyApp/Calculator.svc时,我收到一条错误消息,提示我需要 AspNetCompatability。所以我添加了[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
它并且它有效,唯一的事情是我担心如果我想使用替代的非 http 绑定,我会希望我的服务使用替代的传输方式(不仅仅是 http)。
奇怪的是,如果我没有将属性设置为 Allowed 或 Required,那么当我重建我的页面时,我会收到该错误。收到该错误后,我点击刷新,一切都很好。不仅是我通过网络浏览器查询 svc,而且如果我有一个应用程序,它在第一次连接时崩溃(如果服务器重新启动),然后它就可以工作了。是什么赋予了?