Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create WCF EndPointBehaviors in Code rather than the configuration?

Tags:

I have the following Xml Configuration

<system.serviceModel>     <services>          <service name="MyService.MyServiceREST" behaviorConfiguration="MyServiceTypeBehaviors">             <host>                 <baseAddresses>                     <add baseAddress="http://localhost:1234/MyService/xml"/>                 </baseAddresses>             </host>             <endpoint address="" binding="webHttpBinding" behaviorConfiguration="xmlBehavior" contract="MyService.IMyService" />         </service>     </services>     <behaviors>         <serviceBehaviors>             <behavior name="MyServiceTypeBehaviors" >                 <serviceMetadata httpGetEnabled="true" />                 <serviceDebug includeExceptionDetailInFaults="True"/>             </behavior>         </serviceBehaviors>         <endpointBehaviors>             <behavior name="xmlBehavior">                 <webHttp/>             </behavior>         </endpointBehaviors>     </behaviors> </system.serviceModel> 

I want to implement in C# code rather than using the config.

I cannot figure out who to do the EndPoint with webHttp to expose this service as a REST service.

ServiceHost serviceHost = new ServiceHost(singletonInstance, "http://localhost:1234/MyService/xml");  // Create Meta Behavior ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); behavior.HttpGetEnabled = true;  serviceHost.Description.Behaviors.Add(behavior);  Binding mexBinding = MetadataExchangeBindings.CreateMexHttpBinding();  serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), mexBinding, "mex");  WSHttpBinding httpBinding = new WSHttpBinding(SecurityMode.None);  serviceHost.AddServiceEndpoint(typeof(MyService.IMyService), httpBinding, "rest"); 
like image 501
David Basarab Avatar asked Jan 26 '10 21:01

David Basarab


People also ask

What is WCF configuration?

A Windows Communication Foundation (WCF) service is configurable using the . NET Framework configuration technology. Most commonly, XML elements are added to the Web. config file for an Internet Information Services (IIS) site that hosts a WCF service.

Where do I put bindings in web config?

Let us examine how to setup binding for an endpoint in the web. config file. Step 1: Choose basicHttpBinding as a value in the binding attribute of an endpoint. Step 2: This step is optional and is only required if the binding's default properties need to be modified, as shown in the example below.

What is endpoint behavior WCF?

Endpoint behaviors, which implement IEndpointBehavior, are the primary mechanism by which you modify the entire service or client run time for a specific endpoint. There are two mechanisms for adding endpoint behaviors to a service. Add the behavior to the Behaviors property.

Where are the behaviors and bindings commonly set for WCF?

Windows Communication Foundation (WCF) configures behaviors in two ways: either by referring to behavior configurations -- which are defined in the <behavior> section of a client application configuration file – or programmatically in the calling application.


1 Answers

Typically, when doing REST with WCF, you can either use the <webHttp> behavior in config, or you can use the WebServiceHost class (instead of the "plain vanilla" ServiceHost). Using the WebServiceHost includes all the necessary tweaks and bits and pieces to make the REST stuff work - no more webHttp behavior needed.

Of course, this means, you need a separate WebServiceHost (in System.ServiceModel.Web), which hosts a service as REST exclusively. This may or may not be what you're looking for:

WebServiceHost webServiceHost =      new WebServiceHost(singletonInstance, "http://localhost:1234/MyService/xml");  WebHttpBinding webBinding = new WebHttpBinding(); webServiceHost.AddServiceEndpoint(typeof(MyService.IMyService), webBinding, "rest"); 

The other option you have is the add a service endpoint to your regular service host, and just configure the web http behavior on that endpoint - endpoint (and service) behaviors are just regular .NET classes, after all, which you can instantiate, and add to the appropriate Behaviors collection (on the service or the individual endpoint):

WebHttpBinding restBinding = new WebHttpBinding();  ServiceEndpoint restSEP =      serviceHost.AddServiceEndpoint(typeof(MyService.IMyService),                                     restBinding, "rest"); restSEP.Behaviors.Add(new WebHttpBehavior()); 

Both ways should bring you to your goal, I hope! (or at least get your closer :-)

like image 169
marc_s Avatar answered Sep 28 '22 01:09

marc_s