Reputation: 15770
I want to host a WCF 4.0 Service in IIS 7.5, and be able to bind to it with basicHttpBinding
and also RESTfully with webHttpBinding
.
I need to be able to access it like so:
http://server/wcf/service/method/parameters (REST)
and also like so:
http://server/wcf/service.svc (Basic HTTP)
So far, I have this for my Web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="json">
<webHttp defaultOutgoingResponseFormat="Json" helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="SAIF.Services.WCF.Services.CustomerContactService">
<endpoint address="CustomerContact" behaviorConfiguration="json" binding="webHttpBinding" contract="SAIF.Services.WCF.Contracts.ICustomerContactService" />
<endpoint address="CustomerContact.svc" binding="basicHttpBinding" contract="SAIF.Services.WCF.Contracts.ICustomerContactService" />
</service>
<service name="SAIF.Services.WCF.Services.OnlineLoginService">
<endpoint address="OnlineLogin" binding="basicHttpBinding" contract="SAIF.Services.WCF.Contracts.IOnlineLoginService" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
<serviceActivations>
<add relativeAddress="CustomerContact.svc" service="SAIF.Services.WCF.Services.CustomerContactService" />
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
</configuration>
I also have this in my global.asax file for the extension less activation's:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
Routing.RouteTable.Routes.Add(New ServiceRoute("CustomerContact", New ServiceHostFactory, GetType(SAIF.Services.WCF.Services.CustomerContactService)))
Routing.RouteTable.Routes.Add(New ServiceRoute("OnlineLogin", New ServiceHostFactory, GetType(SAIF.Services.WCF.Services.OnlineLoginService)))
End Sub
I have decorated the service's with this:
And my Service Interface's with the UriTemplates
Don't seem to be able to access them both RESTfully and over SOAP.
Thanks! Sam
Upvotes: 1
Views: 1754
Reputation: 7886
Just decorate your method with both OperationContract and WebGet attributes. Now add the following to system.serviceModel element in your servers web.config
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true">
<readerQuotas maxStringContentLength="5242880" maxArrayLength="16384" maxBytesPerRead="4096" />
</standardEndpoint>
</webHttpEndpoint>
</standardEndpoints>
NOTE: Yo You can remove the json endpoint from the above(as we would achieve a clear URL concept using the new REST Api), And in your global.asax just replace the following:
Routing.RouteTable.Routes.Add(New ServiceRoute("CustomerContactService", New WebServiceHostFactory, typeof(SAIF.Services.WCF.Services.CustomerContactService)));
Now once you do the above you should be able to access the same service via SOAP and REST and the URLs would be as follows:
SOAP --> http://localhost/virtualdirectoryname/CustomerContactService.svc
REST --> http://localhost/virtualdirectoryname/CustomerContactService/method/parameters
Now browse to your service in IE and you should see the SOAP server when you browse to .svc file and when you browse to the rest URL you should see either xml coming up in the browser of a file should be downloaded that contains the response in json format.
Upvotes: 2