加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 没有.svc的WCF服务相对uri

发布时间:2020-12-15 22:01:33 所属栏目:百科 来源:网络整理
导读:我有3个项目的解决方案: MEProject.WCF.ServiceLayer(服务实现) MEProject.WCF.HostConsole(可以托管服务的控制台应用程序) MEProject.WCF.HostIIS(WCF服务应用程序) 我的目标是我可以在两个项目之间切换,而无需更改客户端项目中的uri(端点配置).好吧,问题
我有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>

source

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读