我有3个项目的解决方案:
> MEProject.WCF.ServiceLayer(服务实现)
> MEProject.WCF.HostConsole(可以托管服务的控制台应用程序)
> MEProject.WCF.HostIIS(WCF服务应用程序)
我的目标是我可以在两个项目之间切换,而无需更改客户端项目中的uri(端点配置).好吧,问题是,如果我启动控制台应用程序,端点是
http://localhost:8080/MultipleEndpointService/FirstEndpoint http://localhost:8080/MultipleEndpointService/SecondEndpoint
但是如果我启动WCF服务应用程序,那么端点就是
http://localhost:8080/MultipleEndpointService.svc/FirstEndpoint http://localhost:8080/MultipleEndpointService.svc/SecondEndpoint
如您所见,区别在于“.svc”.现在我的问题是:如何告诉WCF服务应用程序像控制台应用程序一样,而不是在uri中使用“.svc”?
public void ApplydispatchBehavior(ServiceDescription serviceDescription,ServiceHostBase serviceHostBase) { serviceHostBase.Channeldispatchers.ToList().ForEach(channeldispatcher => { Channeldispatcher dispatcher = channeldispatcher as Channeldispatcher; if (dispatcher != null) { dispatcher.Endpoints.ToList().ForEach(endpoint => { endpoint.dispatchRuntime.InstanceProvider = new CallBackInstanceProvider(serviceDescription.ServiceType,InstanceCreator); }); } }); }
这是WCF服务应用程序web.config:
<system.serviceModel> <services> <service name="MEProject.Service.WCF.HostIIS.MultipleEndpointService"> <endpoint name="FirstEndpoint" address="FirstEndpoint" binding="basicHttpBinding" contract="MEProject.Service.WCF.ServiceLayer.IFirstEndpoint"/> <endpoint name="SecondEndpoint" address="SecondEndpoint" binding="basicHttpBinding" contract="MEProject.Service.WCF.ServiceLayer.ISecondEndpoint"/> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8080/MultipleEndpointService" /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
在此先感谢您的回复!
解决方法
要在没有SVC扩展的情况下运行WCF,您需要使用路由
例如,我有一个名为MultipleEndpointService.svc的服务,我想获得如下服务:
… / MultipleEndpointService / FirstEndpoint
我们可以这样做:
Global.asax中:
public class Global : System.Web.HttpApplication { protected void Application_Start(object sender,EventArgs e) { RouteTable.Routes.Add(new ServiceRoute("MultipleEndpointService/FirstEndpoint",new ServiceHostFactory(),typeof(MultipleEndpointService))); } }
MultipleEndpointService.svc.cs:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class MultipleEndpointService : IMultipleEndpointService { public string GetData(int value) { return string.Format("You entered: {0}",value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite == null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } }
Web.config(适用于IIS7):
<system.webServer> <modules runAllManagedModulesForAllRequests="true"> <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule,System.Web,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" /> </modules> <handlers> <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd"/> </handlers> <directorybrowse enabled="true"/> </system.webServer>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。