Full Trust European Hosting

BLOG about Full Trust Hosting and Its Technology - Dedicated to European Windows Hosting Customer

WCF Hosting UK - HostForLIFE.eu :: Binding and Behavior in WCF

clock June 28, 2019 11:26 by author Peter

In this article I will clarify about the Binding and Behavior of WCF. It is one of the principal of WCF. Binding represents how the client can communicate with the service.

Assume we need to make the service for two clients , first client will access SOAP using HTTP and second client access Binary using TCP. Utilizing webservice it is impossible yet utilizing WCF we can do by including additional endpoint in the configuration file. Example: in web.config.
<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" strict="false" explicit="true"targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="MathService"
        behaviorConfiguration="MathServiceBehavior">
        <endpoint
          address="http://localhost:9080/Service1.svc"contract="IMathService"
           binding="wsHttpBinding"/>
<endpoint address="net.tcp://localhost:8080/MyService/MathService.svc"
       contract="IMathService"
                  binding="netTcpBinding"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
     </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

And now, I’ll tell you about the Behaviour at the service level. In the service behavior, I have mention the servieMetadata node with attribute httGetEnabled='true'. This attribute will specifies the publication of the service metadata. Similarly we can add more behavior to the service. <system.serviceModel>
  <services>
    <service name="MathService"
      behaviorConfiguration="MathServiceBehavior">
      <endpoint address="" contract="IMathService"
        binding="wsHttpBinding"/>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="MathServiceBehavior">
        <serviceMetadata httpGetEnabled="True"/>
        <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

HostForLIFE.eu European WCF Hosting

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



WCF Hosting UK - HostForLIFE.eu :: Optional Parameters In WCF Service URI Template

clock December 21, 2018 10:09 by author Peter
WebGet(UriTemplate = "TestMessage/{p1}?firstname={firstname}&lastname={lastname}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Xml)]  
[OperationContract]  
string TestMessage(string message, string firstname = "", string lastname = ""); 

Whereas TestMessage is my service name and p1 parameter is compulsory whereas firstname and lastname parameters are optional but u have to specify in the URI Template but keep one thing in mind while declaring the parameters as optional u have to first preceed them with ? and then parameter name and then use & to assign the second parameter as option as per your requirement. If u see in the function declaration also i have used the optional parameter feature of .Net 4.0

Step 2 - Function Definition

public string TestMessage(string message,string firstname="",string lastname="")  
{  
   string responsedata = string.Empty;   
   responsedata = message + "" + "FirstName: " + firstname + "" + "LastName: " + lastname;  
   return responsedata;  
}  

Note
How to call the Service,

Input1
http://localhost:49785/Service.svc/parameter1

Output1 - parameter1 Firstname: LastName

In the above output, only parameter1 will be returned as u didnot specify the other 2 parameters so they will get their value as "Empty"

Input2

http://localhost:49785/Service.svc/parameter1?firstname=peter&lastname=scott

Output2 - parameter1 Firstname:kamal LastName:rawat

so u will see in the above output u passed both the parameters so you got all the values.

Input2

http://localhost:49785/Service.svc/parameter1?firstname=peter

Output2 - parameter1 Firstname:kamal LastName:
In the above output parameter1 is passed i.e firstname=peter but you wont get lastname parameter, because you didn't pass and its optional if u dont pass the parameter value and will take value as empty.

HostForLIFE.eu European WCF Hosting

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



About HostForLIFE

HostForLIFE is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2019 Hosting, ASP.NET 5 Hosting, ASP.NET MVC 6 Hosting and SQL 2019 Hosting.


Tag cloud

Sign in