Friday, March 16, 2012

WCF: Hosting with NetTcp binding

Create  Windows Consol application and add below file to it.


IService.cs
 

 Service.cs


Service1.svc

 
 Program.cs
App config file.





Get the entire Codebase :
App.config
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>

    <services>
      <service name="ConsoleApplicationNetTCP.Service1">
        <endpoint
         address="net.tcp://localhost:1000/Service1.svc"
         contract="ConsoleApplicationNetTCP.IService1"
         binding="netTcpBinding" bindingConfiguration="tcp"
          />
      
    <endpoint address="mex"
                  binding="mexTcpBinding"
                  contract="IMetadataExchange" />

    <host>
      <baseAddresses>     
        <add baseAddress = "net.tcp://localhost:1000/Service1.svc" />
        <!--<add baseAddress = "http://localhost:1000/Service1.svc" />-->
      </baseAddresses>
    </host>
    </service>
    </services>
    <bindings>

      <netTcpBinding>
     
        <binding name="tcp">
            <security mode="None">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
            <message clientCredentialType="Windows" />
          </security>
        </binding>
      </netTcpBinding>

    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="false"/>
          <!-- 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>
 
</configuration>


IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;

using System.Text;

namespace ConsoleApplicationNetTCP
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        string Echo(string s);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
       
        public string Echo(string s)
        {
            string addressIncomingMessageWasSentTo = OperationContext.Current.IncomingMessageHeaders.To.ToString();
            return s + "\n(Message was sent To " + addressIncomingMessageWasSentTo + ")";
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

Service1.svc

<%@ ServiceHost Language="C#" Debug="true" Service="ConsoleApplicationNetTCP.Service1" CodeBehind="Service1.svc.cs" %>

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;

namespace ConsoleApplicationNetTCP
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost serviceHost = new ServiceHost(typeof(Service1)))
            {
                // Open the ServiceHost to create listeners and start listening for messages.
                serviceHost.Open();

                // The service can now be accessed.
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

            }
            Console.ReadKey();
        }
    }
}


Consuming Application

Add a service reference or generate Proxy using svcutil <url> 

  IService1 obj = new Service1Client();
            Response.Write(obj.Echo("Hi"));
        }

No comments:

Post a Comment