Free Jobs
Reference




Dot Net Forums: ASPNET (77) C#.NET (39) VB.NET (8) AJAX (21) Silver Light (1) WCF (10) WPF (1) WWF (1) .NET 3.5 (3) SQL SERVER (38) Other (3)

Topic: How to call WCF or Windows Communication Foundation Service from C# Project

To get the Posted Questions details daily in your mail Click Here

Share this Post on Facebook/Twitter/Orkut Bookmark and Share





User: manishav3388 Post Date: 13 May 2012 08:51

Introduction:
How to call WCF Service from C# Project



Main Content:

To Call WCF service SampleService from c# Project :

1.Add the proxy file SampleService.cs

after creating proxy folder in root of c# project.

Suppose namespave in proxy is its namespace is Company.Project.Module.ApplicationHost.Proxy

Suppose you have to call the SampleService from SampleCodeBehine.cs file

add the below line in the using block.

using Sample_Proxy = Company.Project.Module.ApplicationHost.Proxy;


Note that in proxy there will be one class where you

will have all the service methods like as below

public partial class SampleServiceClient

Sample_Proxy.SampleServiceClient Sampleclient = null;

Sample_Proxy.SampleMethodResponse SampleMethod_resp = new Sample_Proxy.SampleMethodResponse();

Sample_Proxy.SampleMethodRequest SampleMethod_Req = new Sample_Proxy.SampleMethodRequest();

Sampleclient = new

Sample_Proxy.SampleServiceClient(SecurityScenario.Custom,"BasicHttpBinding_ISampleService");


bConnection = Sampleclient.SampleMethod(healthcheck_Req);


where SampleMethod is the method of.

iN THE APP.CONFIG OF SampleProject

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

    <system.serviceModel>

        <bindings>

            <basicHttpBinding>

                <binding name="BasicHttpBinding_ISampleService"

                    closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00"

                    sendTimeout="00:01:30" allowCookies="false" bypassProxyOnLocal="false"

                    hostNameComparisonMode="StrongWildcard" maxBufferSize="65536"

                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"

                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"

                    useDefaultWebProxy="true">

                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />

                    <security mode="Transport">

                        <transport clientCredentialType="None" proxyCredentialType="None"

                            realm="" />

                        <message clientCredentialType="UserName" algorithmSuite="Default" />

                    </security>

                </binding>

               

            </basicHttpBinding>

        </bindings>

        <client>

            <endpoint

                address="https://sampleserver/SampleService.svc"

                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISampleService"

                contract="Company.Project.Module.ApplicationHost.Proxy.ISampleService"

name="BasicHttpBinding_ISampleService" />

           

        </client>

    </system.serviceModel>

  

</configuration>

           





Suppose a .asmx service is calling two WCF Service.

How will be the setup:

Add a proxy folder to the asmx Webservice project

Add the proxy of Two WCF Services to Proxy folder.

In the web.config of .asmx service

<system.serviceModel>

    <bindings>     

      <basicHttpBinding>

        <binding name="BasicHttpBinding_ISampleService1"

            closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00"

            sendTimeout="00:01:30" allowCookies="false" bypassProxyOnLocal="false"

            hostNameComparisonMode="StrongWildcard" maxBufferSize="65536"

            maxBufferPoolSize="524288" maxReceivedMessageSize="65536"

            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"

            useDefaultWebProxy="true">

          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

              maxBytesPerRead="4096" maxNameTableCharCount="16384" />

          <security mode="Transport">

            <transport clientCredentialType="None" proxyCredentialType="None"

                realm="" />

            <message clientCredentialType="UserName" algorithmSuite="Default" />

          </security>

        </binding>

 

        <binding name="BasicHttpBinding_ISampleService2"

            closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00"

            sendTimeout="00:01:30" allowCookies="false" bypassProxyOnLocal="false"

            hostNameComparisonMode="StrongWildcard" maxBufferSize="65536"

            maxBufferPoolSize="524288" maxReceivedMessageSize="65536"

            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"

            useDefaultWebProxy="true">

          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

              maxBytesPerRead="4096" maxNameTableCharCount="16384" />

          <security mode="Transport">

            <transport clientCredentialType="None" proxyCredentialType="None"

                realm="" />

            <message clientCredentialType="UserName" algorithmSuite="Default" />

          </security>

        </binding>

       

      </basicHttpBinding>

      </bindings>

 <client>

      <endpoint

          address="https://servername/SampleService1.svc"

          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISampleService1"

          contract="CompanyName.ProjectName.ApplicationHost.Proxy.ISampleService1" name="BasicHttpBinding_ISampleService1" />

      <endpoint address="https://servername/SampleService2.svc"

          binding="basicHttpBinding" bindingConfiguration="EpcsServices"

          contract="CompanyName.ProjectName.ApplicationHost.Proxy.ISampleService2"

          name="BasicHttpBinding_ISampleService2" />

   

 </client>

  </system.serviceModel>

Notes :

1. <client> TAG means the present application(whose web.config the tag is written) is client

for end points (services) mentioned in the  <client> tag.

This means this .asmx service is client for the two services whose address are in the wndpoint inside client tag.

There will be two bindings tag and two end point for the two services in the system.serviceModel


2.The most important is the sendTimeout, which says how long the client will wait for a response from your WCF service. You


can specify hours:minutes:seconds in your

settings - in my sample, I set the timeout to 25 minutes.

------------------------------------------------------------

A typical WCF Service solution will look like as below :

------------------------------------------------------------

Below should be ideal Projects in the solution

CompanyName.ServiceName.Services.Web--ASP.NET WEB Project

CompanyName.ServiceName.Services.ApplicationHost--ASP.NET WEB Project

CompanyName.ServiceName.Services.Services --c# Project

CompanyName.ServiceName.Services.Contracts--c# Project

CompanyName.ServiceName.Services.DataTypes--c# Project

CompanyName.ServiceName.Services.Utilities--c# Project


In the Web Project

Properties

References

Proxy-->SampleService.cs(ProxyFile),output.config

Test.aspx

web.config


In the ApplicationHost Project

Properties

References

SampleService.svc

web.config

CompanyName.ServiceName.Services.Services


Properties

References

SampleService.cs

web.config


CompanyName.ServiceName.Services.Contracts


Properties

References

Messages-->SampleService.cs,SoapHeaderData.cs

Operations-->ISampleService.cs

CompanyName.ServiceName.Services.DataTypes

Properties

References

Constants.cs

SampleServcieDTO.cs(DataTransferObjects)

CompanyName.ServiceName.Services.Utilities

Properties

References

Error--ErrorUtilty.cs

Logging--LoggingUtility.cs


--------------------------------------------

Flow :

Web Will call applicationHost (That is hosting .svc )

web is a client for WCF service hosted in ApplicationHost Project which is nothing but a ASP.NET Web Application hosting a


WCF Servcie.

Web is only used for testing WCF service in Application Host.

So web is a client for application host.

Test page will be calling Application host

Web.config of Web Project

<system.serviceModel>

    <client>

      <endpoint address="http://localhost/CompanyName.Services.ApplicationHost/SampleService.svc"

          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISampleService"

          contract="CompanyName.Services.Client.Proxy.ISampleService"

          name="SampleServiceEndPoint" />

    </client>

    <bindings>

      <basicHttpBinding>

        <binding name="BasicHttpBinding_ISampleService"

            closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"


allowCookies="false" bypassProxyOnLocal="false"

            hostNameComparisonMode="StrongWildcard" maxBufferSize="65536"   maxBufferPoolSize="524288"


maxReceivedMessageSize="65536"

            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"  useDefaultWebProxy="true">

          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096"


maxNameTableCharCount="16384" />

          <security mode="None">           

          </security>

        </binding>

      </basicHttpBinding>

    </bindings> 

 

  </system.serviceModel>


------------------------------------------------

ApplicationHost is actual WCF Servcie first point of contact.

There will be single line in SampleService.svc

<%@ ServiceHost Language="C#" Service="CompanyName.Services.SampleService"


The <system.serviceModel> section of web.config will have 5 Parts:

Client -->For the URLS THIS APPLICATION IS CLIENT(Means this application is calling)

Bindings-->Bindings are bindings for client

Services-->The Service this application is exposing

behaviors-->Related to the services which this application is exposing

diagnostics-->For service diagonastics

----------------------------

Client and bIndings will be same as explained above

Services,Behaviours and Diagonastics are shown below:

    <services>

      <service name="sampleServiceConfig" behaviorConfiguration="sampleServiceBehaviour">

        <endpoint binding="basicHttpBinding" contract="CompanyName.Services.Contracts.Operations.ISampleService"

                  bindingNamespace="http://www.yourdomain.com/sampleService/v1" bindingConfiguration="SamServices"/>

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="sampleServiceBehaviour">

          <serviceMetadata httpsGetEnabled="true" />

          <serviceDebug includeExceptionDetailInFaults="true" />

        </behavior>

      </serviceBehaviors>

    </behaviors>

 

<!--This should be set to false for all attributes in production. Enable attributes to generate the request/resposne messages


-->

    <diagnostics  performanceCounters="All" wmiProviderEnabled="true">

      <messageLogging

           logEntireMessage="false"

           logMalformedMessages="false"

           logMessagesAtServiceLevel="false"

           logMessagesAtTransportLevel="false" >       

      </messageLogging>

    </diagnostics>

 

These are some simple concept of WCF Services


Conclusion:
This is the way we call WCF services fromC# PROJECT



About Writer:
hello,I am JAVA Developer from Noida.


Writer's Link: http://

Writer's Email:







Comments:
Post Comment





Similar Articles Top ArticlesLatest Articles Other Articles by this Author







www.freejobsreference.com Copyright © 2009-2011 | All Rights Reserved
All content of this website is protected by copyright laws and should not be reproduced in any form or by any means without written permission
(Best viewed in IE 8.0+ or Firefox 3.0+ at resolution of 1024 * 768 or higher)