WCF服务相对uri没有.svc

时间:2014-02-13 09:20:24

标签: c# .net wcf wcf-binding

我有一个包含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>

提前感谢您的回复!

2 个答案:

答案 0 :(得分:1)

要运行没有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>

source

答案 1 :(得分:0)

尝试网址重写:

<system.webServer>
    <!-- Other stuff here -->
    <rewrite>
        <rules>
            <!-- Rewrite requests to /MultipleEndpointService.svc to /MultipleEndpointService -->
            <rule name="MultipleEndpointService" stopProcessing="true">
                <match url="MultipleEndpointService.svc(.*)" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                </conditions>
                <action type="Rewrite" url="/MultipleEndpointService{R:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>