Where to configure the WCF service (REST and SOAP)

I am implementing the WCF web service, which is responsible for publishing data through REST and SOAP using multiple bindings. The service must be hosted on IIS.

I wrote some WCF services earlier and am a little versed in setting them up using web.config and setting up routes in Global.asax files, however I am confused about how to make the most “clean” configuration or best practice for setting up a WCF service.

Here is what I have understood so far:

Web.config can be used to set bindings, endpoints, security, etc. - Is this necessary when hosting the service in IIS or can the configuration be done in IIS?

Using Global.asax, we can configure routes (and many other things). but is this a suitable place for this?

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

I spent some time searching for this topic, and it seems that each link has its own opinion on how to complete the task.

Therefore, I would like to get some information on how to configure / deploy the WCF service to support the following:

For the record, I know how to publish data using SOAP / REST is not a problem. I'm just trying to do the cleanest / minimum configuration for a service.

Any feedback is welcome.

+4
source share
2 answers

Here is how I did it.

Web.config:

 <system.serviceModel> <services> <service name="Service"> <endpoint address="soap" contract="IService" binding="basicHttpBinding"/> <endpoint address="rest" contract="IService" binding="webHttpBinding" behaviorConfiguration="restBehavior"/> </service> </services> <behaviors> <endpointBehaviors> <behavior name="restBehavior"> <webHttp/> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> 

The contract is as follows:

 [ServiceContract] public interface IService { [OperationContract] [WebInvoke(UriTemplate="/Update/{id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] void Update(string id, Entity entity); } 
+6
source

With .net 4.5 you can omit any configuration in the .config file at all. [ServiceContract], [OperationContract], [DataContract] will be needed. They don’t say that the documentation is explicit, but it works :)

".NET Framework 4.5 simplifies the configuration of a WCF service by eliminating the requirement for a service element. If you do not add a service section or add any endpoints to a service section and your service does not program any endpoints, then the default set of endpoints is automatically will be added to your service, one for each base address of the service and for each contract executed by your service. " - from

https://msdn.microsoft.com/en-us/library/ee358768%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

0
source

Source: https://habr.com/ru/post/1401138/


All Articles