moris62
moris62

Reputation: 1045

how to use generated WCF service from svcutil.exe

I'm new in WCF, I have a service which I want to use it in a console application, I run svcutil.exe htt://localhost:58221/myservice.svc/mex and gave me two outputs, one is a cs file and the other one is XML, i copied and pasted the config one in my app setting but i don't know should i use also the CS file? and how?It gave me the example like :

   class Test
  {
    static void Main()
    {
   MyServiceServiceClient client = new MyServiceServiceClient ();

    // Use the 'client' variable to call operations on the service.

    // Always close the client.
    client.Close();
}

Upvotes: 0

Views: 545

Answers (1)

Abraham Qian
Abraham Qian

Reputation: 7522

SVCUtil.exe generates the client proxy class and the service endpoint address. If we just want to call the service by using these files. we only need to add the client proxy class (testService.cs) to the Console application and copy the System.servicemode section in the output.config file to app.config file in the Console application.

    <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_ITestService" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:4386/" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_ITestService" contract="ITestService"
            name="BasicHttpBinding_ITestService" />
    </client>
</system.serviceModel>

Then instantiate the client proxy, with the intelligent hint, we can get the service methods contained in the proxy class. Just as we would call the local methods.

Service1Client client = new Service1Client("BasicHttpsBinding_IService1");
            try
            {
                var result = client.GetData(34);
                Console.WriteLine(result);
            }
            catch (Exception)
            {

                throw;
            }

One more thing we need to pay attention to is ensuring the service is in the running state when making an invocation.
Feel free to let me know if the problem still exists.  

Upvotes: 1

Related Questions