none
How to configure webHttpBinding programmatically for WCF RRS feed

  • Question

  • Hello everyone; I need help to setup in code what I have in my app.config for a WCF service; this is my app.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
      <system.serviceModel>
        <bindings>
          <webHttpBinding>
            <binding name="BasicHttpBinding_IConfidentialInterview"
                     closeTimeout="00:010:00"
                     openTimeout="00:010:00"
                     receiveTimeout="00:10:00"
                     sendTimeout="00:010:00"
                     hostNameComparisonMode="StrongWildcard"
                     maxBufferPoolSize="2147483647"
                     maxReceivedMessageSize="2147483647">
              <readerQuotas maxDepth="32"
                            maxStringContentLength="2147483647"
                            maxArrayLength="16384"
                            maxBytesPerRead="4096"
                            maxNameTableCharCount="16384" />
              <security mode="Transport" />
            </binding>
          </webHttpBinding>
        </bindings>
        <client>
          <endpoint name="BasicHttpBinding_IConfidentialInterview"
                    address="https://myHostDomain.com/hd/service/Miami/ConfidentialInterview.svc"
                    binding="webHttpBinding"
                    bindingConfiguration="BasicHttpBinding_IConfidentialInterview"
                    contract="CI.IConfidentialInterview"
                    behaviorConfiguration="webhttp">
            <identity>
              <dns value="localhost" />
            </identity>
          </endpoint>
        </client>
        <behaviors>
          <endpointBehaviors>
            <behavior name="webhttp">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>

    Below is my code so far; I can't figure out how to include the endpoint behaviors from the app.config in my code below:

    EndpointAddress oEndpoint = new EndpointAddress(serviceURL);

    WebHttpBinding oBinding = new WebHttpBinding(); oBinding.Name = "BasicHttpBinding_IConfidentialInterview"; oBinding.CloseTimeout = System.TimeSpan.Parse("00:10:00"); oBinding.OpenTimeout = System.TimeSpan.Parse("00:10:00"); oBinding.ReceiveTimeout = System.TimeSpan.Parse("00:10:00"); oBinding.SendTimeout = System.TimeSpan.Parse("00:10:00"); oBinding.HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.StrongWildcard; oBinding.MaxBufferPoolSize = 2147483647; oBinding.MaxReceivedMessageSize = 2147483647; oBinding.ReaderQuotas.MaxDepth = 32; oBinding.ReaderQuotas.MaxStringContentLength = 2147483647; oBinding.ReaderQuotas.MaxArrayLength = 16384; oBinding.ReaderQuotas.MaxBytesPerRead = 4096; oBinding.ReaderQuotas.MaxNameTableCharCount = 16384; oBinding.Security.Mode = WebHttpSecurityMode.Transport;


    Thank you for your help


    Luis

    Monday, February 13, 2017 7:49 PM

Answers

  • Hi Edward, thank you for the clarification and for taking the time. I have a console application that is working and it has the service added as a service reference provided by the web service creator. When I add the service as a reference I can instantiate a client from the service and I can see all the methods, but it doesn't generate an app.config and that is known fact for webHttpBinding as it explains here, but let's forget about all this, all I'm asking is for help to mimic the app.config I have listed above with code. 

    I've search all over the place and other people have done it but from the host point of view, no from the client point of view, at least for webHttpBinding. I know is doable, just don't know yet how to implement the webHttp endpoint behavior part

    Thank you very much for your help,

    Luis


    Luis

    • Marked as answer by LazyCod3r Friday, February 17, 2017 12:37 PM
    Friday, February 17, 2017 12:37 PM
  • Hi Edward, thank you for your reply. I got it to work, I was missing an additional attribute on the interface signature. After I added it, it worked; basically I needed to edit Reference.cs and add [System.ServiceModel.Web.WebGet] to the interface signature. 

    Thank you for your time.


    Luis

    • Marked as answer by LazyCod3r Tuesday, February 21, 2017 1:08 PM
    Tuesday, February 21, 2017 1:08 PM

All replies

  • Hi Luis,

    For configuring webHttpBinding in Code, you could try below code:

            public static void ConfigureWCFInCode()
            {
                Uri serviceURL = new Uri("https://localhost:5555/");            
                EndpointAddress oEndpoint = new EndpointAddress(serviceURL);
    
                WebHttpBinding oBinding = new WebHttpBinding();
                oBinding.Name = "BasicHttpBinding_IConfidentialInterview";
                oBinding.CloseTimeout = System.TimeSpan.Parse("00:10:00");
    
                oBinding.OpenTimeout = System.TimeSpan.Parse("00:10:00");
                oBinding.ReceiveTimeout = System.TimeSpan.Parse("00:10:00");
                oBinding.SendTimeout = System.TimeSpan.Parse("00:10:00");
    
                oBinding.HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.StrongWildcard;
                oBinding.MaxBufferPoolSize = 2147483647;
    
                oBinding.MaxReceivedMessageSize = 2147483647;
                oBinding.ReaderQuotas.MaxDepth = 32;
                oBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
    
                oBinding.ReaderQuotas.MaxArrayLength = 16384;
                oBinding.ReaderQuotas.MaxBytesPerRead = 4096;
                oBinding.ReaderQuotas.MaxNameTableCharCount = 16384;
                oBinding.Security.Mode = WebHttpSecurityMode.Transport;
    
                //host wcf service
                ServiceHost host = new ServiceHost(typeof(WCFInCode), serviceURL);
                //add service endpoint
                ServiceEndpoint serviceEndpoint= host.AddServiceEndpoint(typeof(IWCFInCode),oBinding, "WCFInCode");
                //add webHttp endpoint Behavior
                serviceEndpoint.EndpointBehaviors.Add(new WebHttpBehavior());
                host.Open();
                Console.WriteLine("web service is open");
                //getdata is the method name
                Console.WriteLine("access wcf rest service from "+ serviceURL+ @"/WCFInCode/getdata");
            }
    

    Based on your code, you configure https for your rest service, if so, you need to create and configure a certificate for the port.

    For setting SSL certificate on a Port, I suggest you refer the link below:

    #Get the SSL Cert Running on a Port
    https://www.codeproject.com/Articles/295831/WCF-NET-Console-Hosted-Json-Rest-Secure-Http-W

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 14, 2017 2:41 AM
  • Thank you very much Edward, but it is the client code I'm struggling with, no the host; I didn't code the service I'm just trying to access it. Specifically I don't know how to to programmatically configure the endpoint behaviors part of my app.config, this part:

     <behaviors>
       <endpointBehaviors>
          <behavior name="webhttp">
              <webHttp/>
          </behavior>
       </endpointBehaviors>
     </behaviors>
    Thank you again for taking the time to answer, 

    Luis


    • Edited by LazyCod3r Tuesday, February 14, 2017 12:22 PM
    Tuesday, February 14, 2017 12:21 PM
  • Hi Luis,

    Is the WCF Rest Service created by you? Could you access WCF Rest Service method from IE?

    For consuming WCF Rest Service, there is no need to configure the bindings. You could try http request to call WCF Rest Service.

    You could refer the link below for more information.

    # How to consume REST services with WCF

    https://blogs.msdn.microsoft.com/pedram/2008/04/21/how-to-consume-rest-services-with-wcf/

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 15, 2017 4:16 AM
  • Hi Edward, thank you for your reply. I didn't write the service. I read thoroughly the article from the link you sent me, it didn't help my scenario, my service requires login. This is my real scenario: 

    When I add the service reference to visual studio it doesn't generate an app.config, this seems to be normal for webHttpBinding and REST according to Carlos Figueira Here. I have a console application; provided by the service writer, that successfully calls the service, so I copied the app.config from that application into my project but that didn't help. I want to call the service from a class since I need to build it into an Interop COM library that will be call from an old application. In my experience, app.config doesn't work well for DLLs; that's why I need to have the binding in the code. I have done this before with BasicHttpBing.

    The question now is, how do you tell your code where to look for the configuration? or how can I implement in code the app.config shown above; specifically the endpoint behaviors?

    Thank you for taking the time


    Luis


    • Edited by LazyCod3r Wednesday, February 15, 2017 2:10 PM added correction
    Wednesday, February 15, 2017 2:07 PM
  • Hi Luis,

    It seems you misunderstood what Rest Service is. Rest service is called by http request. It is different from WCF SOAP Service. Adding Service Reference is used for SOAP Service instead of Rest Service.

    For consuming Rest Service in your own code, you need to confirm with service writer what the service url, service method and service parameter are. No Body would know these information except the writer.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 16, 2017 2:25 AM
  • Hi Edward,  thank you again for your time and yes, I'm very new to this, this service is not rest, is a WCF service using webHttpBinding. I'm able to add a service reference, and I can see all the functionality exposed by the service, but the app.config doesn't get updated; that's when I decided to create the binding in code. As I mentioned I need this service to be called from a DLL, from previous experience app.config doesn't work well or doesn't work at all with a DLL, one more reason to code the configuration. When I run the code I get and error saying "The provided URI scheme 'https' is invalid; expected 'http'. Parameter name: via", so I say ok, change the address to http instead and see what happens, ran it and I got a different error: "Manual addressing is enabled on this factory, so all messages sent must be pre-addressed" Searching the last error led me to the find that I need to add endpoint behaviors; like the working sample has on the app.config. 

    I know this is probably something simple, but I can't understand why  the very simple console application works and this doesn't; even when the code is identical.

    Thank you,


    Luis

    Thursday, February 16, 2017 12:40 PM
  • To make a long story short; I want to mimic in code what my app.config has for the client side of the service. The part I can't figure out is how to set the endpoint behavior as webHttp.

    Thank you


    Luis

    Thursday, February 16, 2017 7:58 PM
  • Hi Luis,

    In general, we call WCF Service with webHttpBinding as WCF Rest Service.

    WCF Rest Service is called by http request, it is not suitable to call Rest Service by Adding Service Reference.

    Without checking service code, I am afraid we could not know how to use WCF Rest Service for that we do not know the address for Rest Service and methods which are defined in Rest Service.

    In addition, I suggest you try decompiler tools for .NET Framework to check whether you could get source code of console application.

    # Decompiler tools for .NET Framework

    https://blogs.msdn.microsoft.com/amb/2011/05/24/decompiler-tools-for-net-framework/

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 17, 2017 7:41 AM
  • Hi Edward, thank you for the clarification and for taking the time. I have a console application that is working and it has the service added as a service reference provided by the web service creator. When I add the service as a reference I can instantiate a client from the service and I can see all the methods, but it doesn't generate an app.config and that is known fact for webHttpBinding as it explains here, but let's forget about all this, all I'm asking is for help to mimic the app.config I have listed above with code. 

    I've search all over the place and other people have done it but from the host point of view, no from the client point of view, at least for webHttpBinding. I know is doable, just don't know yet how to implement the webHttp endpoint behavior part

    Thank you very much for your help,

    Luis


    Luis

    • Marked as answer by LazyCod3r Friday, February 17, 2017 12:37 PM
    Friday, February 17, 2017 12:37 PM
  • Hi Luis,

    As I tried to explain, you cannot get a client side proxy for webHttpBinding, there is just no such thing.

    >> all I'm asking is for help to mimic the app.config I have listed above with code.

    It is impossible to use webHttpBinding at client side. WCF webHttpBinding is called by HTTP Requests instead of client proxy.

    The WCF Web Programming Model allows developers to expose WCF Web services through HTTP requests that use "plain old XML" (POX) style messaging instead of SOAP-based messaging.

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

    In a conclusion, it is impossible to achieve your requirement by configuring webHttp endpoint at client side. I would suggest you refer below link for how to create and how to use WCF with webHttpBinding.

    #WCF Web HTTP Programming Model

    https://msdn.microsoft.com/en-us/library/bb412169(v=vs.110).aspx  

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 20, 2017 4:49 AM
  • Hi Edward, thank you for your reply. I got it to work, I was missing an additional attribute on the interface signature. After I added it, it worked; basically I needed to edit Reference.cs and add [System.ServiceModel.Web.WebGet] to the interface signature. 

    Thank you for your time.


    Luis

    • Marked as answer by LazyCod3r Tuesday, February 21, 2017 1:08 PM
    Tuesday, February 21, 2017 1:08 PM