开发者

How to run WCF service method with WebGet attribute on the root of the host

I do have this WCF service contract:开发者_如何学Go

[ServiceContract]
public interface IPolicyRetriever
{
    [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
    Stream GetSilverlightPolicy();
}

with this Web.config section:

  <service behaviorConfiguration="policyRetrieverServiceBehavior" 
      name="WebService.PolicyRetriever">
    <endpoint address="" binding="webHttpBinding"
        behaviorConfiguration="policyRetrieverEndpointBehavior"
        contract="WebService.IPolicyRetriever" />
  </service>

The server is running on localhost, with Visual Studio web hosting, on port 8080 and the web service file is named WebService.svc.

The above code will make the GetSilverlightPolicy() method be exposed on http://localhost:8080/WebService.svc/clientaccesspolicy.xml.

What I need is to expose the file on the root of the webserver instead of the WebService.svc sub-path, but I could not find a way to accomplish this.

Setting the endpoint address property to / or http://localhost:8080/ did not work.

Neither adding a host section to the service node:

<host>
  <baseAddresses>
    <add baseAddress="http://localhost:8080/"/>
  </baseAddresses>
</host>

Did anyone find a solution?


You can achieve this as shown below:

Set your WCF project to support AspNetCompatiblityMode to true as shown below:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

Now open your code behind (class that implements the interface) and add the following attribute above your service class:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

Now Open your global.asax if you have one (else add one) and in the Application_Start method add the following line:

 RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(RestService)));

You can get rid of the svc file now. Once you do the above steps build your project and deploy to IIS. Now the URL of your web service would be

http://localhost/VirtualDirectoryName/clientaccesspolicy.xml 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜