none
Adding Service Behavior Programmatically

    Question

  • Hi all,

    I want to consume a REST service from my client code, I successfully wrote my app.config to work with my client code. Now I want to further move all these config into my codes programmatically, especially I need WebHttpBehavior with my endpoint in order to consume REST service properly.

    I checked classes all around and could not find how can I add service behavior to endpoint programmatically.

    The corresponding config is
    <?xml version="1.0" encoding="utf-8" ?>
    
    <configuration>
    
    	<system.serviceModel>
    
    		<client>
    
    			<endpoint address="http://server.com/services/rest"
    
    					  behaviorConfiguration="GreatService"
    
    					  binding="webHttpBinding"
    
    					  contract="MyClient.GreatService.IGreatServiceApi"
    
    					  name="GreatServiceREST"
    
    					  />
    
    		</client>
    
    
    
    		<behaviors>
    
    			<endpointBehaviors>
    
    				<behavior name="GreatService">
    
    					<webHttp/>
    
    				</behavior>
    
    			</endpointBehaviors>
    
    		</behaviors>
    
    	</system.serviceModel>
    
    </configuration>
    
    
    How to translate it into C# class? Anybody has clue?

    Thanks!
    Hardy
    Welcome to help me with my open source project at http://code.google.com/p/batch-image-watermark-processor/
    Tuesday, October 06, 2009 1:36 AM

Answers

  • Ups, I've overlooked that part. Then this should do it:

    Binding binding = new WebHttpBinding();
    EndpointAddress address = new EndpointAddress("http://...");
    IEndpointBehavior behavior = new WebHttpBehavior();
    
    ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, address);
    factory.Endpoint.Behaviors.Add(behavior);
    
    IService channel = factory.CreateChannel();

    I didn't compile it but it should work.

    Regards,
    Stipe-Ivan
    • Marked as answer by Hardy Tuesday, October 06, 2009 1:34 PM
    Tuesday, October 06, 2009 1:19 PM

All replies

  • I am trying to understand what you are trying to do here.  You can consume REST services using any http client (eg: HttpWebRequest)
    Why are you creating an app.config to consume REST services?  You need the WebHttp behavior at the server side, not at the client side.

    Amit Sharma
    Tuesday, October 06, 2009 3:13 AM
    Moderator
  • I am using WCF to consume REST service. I can declare in app.config to have all the settings above. I just try to avoid using config file and do everything in the C# code, declare binding/endpoint address/adding behavior.

    Now I just could not figure out how to add  WebHttpBehavior programmatically.

    Thanks
    Welcome to help me with my open source project at http://code.google.com/p/batch-image-watermark-processor/
    Tuesday, October 06, 2009 11:54 AM
  • Hi Hardy,

    maybe this could help:

    ServiceHost host = new ServiceHost(typeof(Service));
    ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "http://...");
    
    WebHttpBehavior behavior = new WebHttpBehavior();
    endpoint.Behaviors.Add(behavior);            

    Regards,
    Stipe-Ivan
    Tuesday, October 06, 2009 12:13 PM
  • Hi Hardy,

    maybe this could help:

    ServiceHost host = new ServiceHost(typeof(Service));
    
    ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "http://...");
    
    
    
    WebHttpBehavior behavior = new WebHttpBehavior();
    
    endpoint.Behaviors.Add(behavior);            
    
    

    Regards,
    Stipe-Ivan

    Hi, thanks for your code sample. It looks like this is server side hosting code, I am trying to find out client side code because I have no control over server implemtantion.

    Thanks
    Hardy
    Welcome to help me with my open source project at http://code.google.com/p/batch-image-watermark-processor/
    Tuesday, October 06, 2009 12:47 PM
  • Ups, I've overlooked that part. Then this should do it:

    Binding binding = new WebHttpBinding();
    EndpointAddress address = new EndpointAddress("http://...");
    IEndpointBehavior behavior = new WebHttpBehavior();
    
    ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, address);
    factory.Endpoint.Behaviors.Add(behavior);
    
    IService channel = factory.CreateChannel();

    I didn't compile it but it should work.

    Regards,
    Stipe-Ivan
    • Marked as answer by Hardy Tuesday, October 06, 2009 1:34 PM
    Tuesday, October 06, 2009 1:19 PM
  • Ups, I've overlooked that part. Then this should do it:

    Binding binding = new WebHttpBinding();
    
    EndpointAddress address = new EndpointAddress("http://...");
    
    IEndpointBehavior behavior = new WebHttpBehavior();
    
    
    
    ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, address);
    
    factory.Endpoint.Behaviors.Add(behavior);
    
    
    
    IService channel = factory.CreateChannel();
    
    

    I didn't compile it but it should work.

    Regards,
    Stipe-Ivan

    Great! I tried this and it worked perfectly!

    Thanks
    Hardy
    Welcome to help me with my open source project at http://code.google.com/p/batch-image-watermark-processor/
    Tuesday, October 06, 2009 1:34 PM