• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 521
  • Last Modified:

Adding proxy file to wcf client

I am learning WCF. Do i need to add proxy file on client side. If so then every time i modify the file i had to update the proxy file on client side.. Is there any other solution . The question is what if there are thousands of Client or more?
0
rnsr
Asked:
rnsr
  • 7
  • 3
1 Solution
 
Sathish DVSoftware EngineerCommented:
Hi,

It is not necessary that you need to have proxy file at client side. In WCF, there is option for auto proxies. For eg.) You have a calculator service. So u create contract ike
[ServiceContract]
interface ICalculator
{
   [OperationContract]
   int Add(int a, int b);
}

Open in new window


implement the contract in service class
public class CalulatorService : ICalculator
{
       public int Add(int a, int b)
        (
            return a+b;
        )
}

Open in new window


Configure the end point in the web.config file
<configuration>
  <system.serviceModel>

    <services>
      <service name="TestCalculatorApp.CalculatorService">

        <endpoint address="http://XXXXX/Calc"
                  binding="basicHttpBinding"
                  contract="TestCalculatorApp.ICalculator" />
   </services>
 </system.serviceModel>
</configuration>

Open in new window


At client side, since it is auto proxy u need add reference to the interface ICalculator and include the below code

Creating proxy on the fly by invoking the method CreateChannel() from ChannelFactory. Here ChannelFactory.CreateChannel will create a proxy based on the parameters we passed. HttpCaclBind the name of the binding configuration available at client.config(shown below). Since we are referring ICalculator interface we get the methods exposed by interface via service class.
So adding the below code creates both a proxy object of type ICalculator in the client and an instance of the CalculatorService service object on the server.
using (ICalculator calc = new ChannelFactory<ICalculator>("HttpCalcBind").CreateChannel()) 
      {
           int  Addition = calc.Add (1,2);
           Console.WriteLine ("Sum = {0}", Addition);
      }

Open in new window


Here is client.config

<configuration>
  <system.serviceModel>
    <client>
<endpoint address="http://XXXXX/Calc"
                binding="basicHttpBinding"
                contract="TestCalculatorApp.ICalculator"
                name="HttpCalcBind" />

    </client>
  </system.serviceModel>
</configuration>

Open in new window


By following this approach, u need not to update the proxy every time there is any change at server side. Since it creates the Proxy on the fly, it will create proxy object at client side and service class object at server side and pointer is maintained through binding configuration. If any thing changes happens at server then client will be knowing that and new proxy object instance is created.
0
 
rnsrAuthor Commented:
Sorry, the code didn't work. It displayed error-
Could not find endpoint element with name "BasicHttpBinding_ICalculator" and contract "Icalculator"
in the serviceModel client configuration section.
--------------

 But  In web.config file its already there .  I cannot understand why it is not accessing that.
My service is running. But the problem when i run the client application.
0
 
rnsrAuthor Commented:
I had checked the url but it didn't work
http://www.codeproject.com/KB/WCF/WCF_auto_client_proxies.aspx
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
rnsrAuthor Commented:
need help on this topic.
0
 
Sathish DVSoftware EngineerCommented:
Hi,

BasicHttpBinding_ICalculator endpoint will not be availble under endpoint section. U need to specify the binding type which matches with the type at server web.config. In Web.Config at service binding is configured to use basic http and same has been added in the enpoint section. And at client side web.config u can see binding configured to use basic http. So the client binding should match with server binding type.  See code below
namespace WcfService1
{
  [ServiceContract]
  public interface ICalculator 
  {
    [OperationContract]
    int Add (int a, int b);
  }
}

Open in new window


Interface implementation
namespace WcfService1
{

    // Implementation of CalculatorService

    public class CalculatorService : ICalculator
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
   }
}

Open in new window


Web.config at service
<system.serviceModel>
    <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>
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBinding_AtServer"/>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="CalculatorService">
        <endpoint address="http://localhost:59793/CalculatorService.svc"
                  binding="basicHttpBinding" bindingConfiguration="basicHttpBinding_AtServer"
                  contract="WcfService1.ICalculator"/>
      </service>
      
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Open in new window


Web.config at client side
<system.serviceModel>
    <client>
      <endpoint binding="basicHttpBinding"
                address="http://localhost:59793/CalculatorService.svc"
                contract="WcfService1.ICalculator"
                 name="basicHttpBind"/>
    </client>
  </system.serviceModel>

Open in new window


protected void Page_Load(object sender, EventArgs e)
        {
            ICalculator calc = new ChannelFactory<ICalculator>("basicHttpBind").CreateChannel();
            int A = calc.Add(12, 15);
            
        }

Open in new window

0
 
rnsrAuthor Commented:
Plz let me know if i need proxy file. Because i am getting error
Error      1      The type or namespace name 'ICalculator' could not be found (are you missing a using directive or an assembly reference?)      D:\wcfsecond\client\client\Program.cs      29      25      client

I am not using web but its my console application where i want to run wcf service and client.
So i am using app.config file.
0
 
rnsrAuthor Commented:
i didn't get the solution.
0
 
Sathish DVSoftware EngineerCommented:
Hi,

Did u try adding reference to ICalculator in ur console app? If u dont add reference then the above error will occur. Try adding the same and follow the above solution. It will work even with App.config.
0
 
rnsrAuthor Commented:
I am busy these days and will respond soon.

Regards,
0
 
rnsrAuthor Commented:
Thanks for Help
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now