Saturday, November 13, 2010

WCF Service and WCF Client Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace ServiceApplication
{
    // Service
    [ServiceContract]
    public class MathService
    {
        [OperationContract]
        public double Sum(double x, double y)
        {
            return x + y;
        }
        [OperationContract]
        public double Mul(double x, double y)
        {
            return x * y;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host =
                new ServiceHost(typeof(MathService)); // service
            //host.AddServiceEndpoint(
            //    typeof(MathService), // contract
            //    new WSHttpBinding(), // binding
            //    "http://localhost:8000/MathService"); // address
            //host.AddServiceEndpoint(
            //    typeof(MathService), // contract
            //    new NetTcpBinding(), // binding
            //    "http://localhost:8000/MathService"); // address
            //host.Description.Behaviors.Add(
            //    new ServiceMetadataBehavior()
            //    {
            //        HttpGetEnabled=true,
            //        HttpGetUrl = new Uri("http://localhost:8000/MathService/WSDL")
            //    });
            host.Open();
            Console.WriteLine("Runtime state: " + host.State);
            Console.WriteLine("Press enter to stop runtime...");
            Console.ReadLine();
        }
    }
}
Service:config file

<?
<
<

<
<
xml version="1.0" encoding="utf-8" ?>configuration>system.serviceModel>services>service name="ServiceApplication.MathService"
<
behaviorConfiguration="MathServiceBehavior">endpoint address="http://localhost:8000/MathService"binding="wsHttpBinding"

<
contract="ServiceApplication.MathService" />endpoint address="net.tcp://localhost:9000/MathService"binding="netTcpBinding"
</
</

<
<
<
<

</
</
</

</
</
contract="ServiceApplication.MathService" />service> services>behaviors>serviceBehaviors>behavior name="MathServiceBehavior">serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8000/MathService/WSDL"/>behavior>serviceBehaviors>behaviors>system.serviceModel>configuration>
Client Code:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using ClientApplication.MathServiceReference;namespace
{
ClientApplicationclass Program{

{
MathServiceClient proxy =


}
}
}
static void Main(string[] args)new MathServiceClient("NetTcpBinding_MathService");Console.WriteLine(proxy.Sum(10, 20));
client App config Code:

<?
<
<
<
<
<
xml version="1.0" encoding="utf-8" ?>configuration>system.serviceModel>bindings>netTcpBinding>binding name="NetTcpBinding_MathService" closeTimeout="00:01:00"openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"hostNameComparisonMode="StrongWildcard" listenBacklog="10"maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
<
maxReceivedMessageSize="65536">readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
<
maxBytesPerRead="4096" maxNameTableCharCount="16384" />reliableSession ordered="true" inactivityTimeout="00:10:00"
<
<
<
</
</
</
<
<
enabled="false" />security mode="Transport">transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />message clientCredentialType="Windows" />security>binding>netTcpBinding>wsHttpBinding>binding name="WSHttpBinding_MathService" closeTimeout="00:01:00"openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"maxBufferPoolSize="524288" maxReceivedMessageSize="65536"messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
<
allowCookies="false">readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
<
maxBytesPerRead="4096" maxNameTableCharCount="16384" />reliableSession ordered="true" inactivityTimeout="00:10:00"
<
<
enabled="false" />security mode="Message">transport clientCredentialType="Windows" proxyCredentialType="None"
<
realm="" />message clientCredentialType="Windows" negotiateServiceCredential="true"
</
</
</
</
<
<
algorithmSuite="Default" />security>binding>wsHttpBinding>bindings>client>endpoint address="http://localhost:8000/MathService" binding="wsHttpBinding"bindingConfiguration="WSHttpBinding_MathService" contract="MathServiceReference.MathService"
<
<
</
</
<
name="WSHttpBinding_MathService">identity>userPrincipalName value="Shakeel-PC\Shakeel" />identity>endpoint>endpoint address="net.tcp://localhost:9000/MathService" binding="netTcpBinding"bindingConfiguration="NetTcpBinding_MathService" contract="MathServiceReference.MathService"
<
<
</
</
</
</
</
name="NetTcpBinding_MathService">identity>userPrincipalName value="Shakeel-PC\Shakeel" />identity>endpoint>client>system.serviceModel>configuration>

No comments:

Post a Comment