WCF Service on IIS. How to get rid of the “Service.svc” component in the URL path?

坚强是说给别人听的谎言 提交于 2019-12-08 06:49:54

问题


I have a WCF Service hosted on IIS. For business reason I cannot post the public URL of this instance, but I'm sure you will get what I mean:

The problem is that in order to reach my endpoint it seems I have include the Service.svc as part of the path segment, i.e., I have to use URLs like this:

http://<domain>/<app-name>/Service.svc/<relative-path>

How can I avoid this? I mean, I'd like to access my service simply with:

http://<domain>/<app-name>/<relative-path>

I was perfectly able to do this when I was self-hosting the service during development.


Lastly, but this is not-so-transcendental, browsing to the http://<domain>/<AppName>/Service.svc URL displays the standard service information page:

Is there a way I could also prevent this from being accessible?


回答1:


First part: to not have the "service.svc" part of the URL, you can use the ASP.NET Routing feature, and the ServiceRoute class - the prefix for your route would be the empty string. The post at http://blogs.msdn.com/b/endpoint/archive/2010/01/25/using-routes-to-compose-wcf-webhttp-services.aspx shows how this can be done.

Second part: to prevent that "help" page from being shown, you can disable it via config (or via code using a custom service host factory). Here's how it can be done via config:

<serviceBehaviors>
    <behavior>
        <serviceDebug httpHelpPageEnabled="false" httpsHelpPageEnabled="false"/>
    </behavior>
</serviceBehaviors>

The behavior does not have the "name" attribute, which means that it will be used as the default behavior for services.




回答2:


Step-by-step instructions for .net newbs like myself:

  1. Open your Web.config file and ensure that the aspNetCompatibilityEnabled is set to true:

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    
  2. Right click on the project, click Add->New Item. Find "Global Application Class". This will auto-generate a Global.asax file.

  3. Open Global.asax file and ensure the Application_Start method looks as follows:

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.Add(new ServiceRoute("", new ServiceHostFactory(), typeof(MyServiceImplementation)));
    }
    

(Change MyServiceImplementation with whatever your class inside Service1.svc is called)

  1. Ensure you've added the following references:

    using System.ServiceModel.Activation;
    using System.Web.Routing;
    

If these aren't found and you are using something earlier than .net 4.5, make sure you are using the full install instead of the client install. Also make sure you've added these package references (right click-project, click Add->Reference, find the package name and ensure it's checked).

  1. Restart IIS. The Service1.svc (or whatever yours was called) file should now be hidden.



回答3:


you can reach this by simply not exposing your service metadata - just comment endpoint

 <endpoint address="mex" binding="mexHttpBinding" name="IMetadataExchange_mexHttpBinding" contract="IMetadataExchange" />

According url - I simply agree with @Edmund Y - he provided useful link




回答4:


I'm only curious to know if what I did to implement @carlosfigueira's advice was the best cleanest thing:

I ended up using a Global.asax file and adding a ServiceRoute with empty routePrefix in the Application_Start handler. I'm not too sure why I'm using WebServiceHostFactory vs ServiceHostFactory as I was reading in the article shared by @Edmund Y: ServiceRoute + WebServiceHostFactory kills WSDL generation? How to create extensionless WCF service with ?wsdl.

In any case, I think I managed to resolve what I needed and I appreciate your help.



来源:https://stackoverflow.com/questions/11440562/wcf-service-on-iis-how-to-get-rid-of-the-service-svc-component-in-the-url-pat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!