ASP.NET - Contracts and Endpoints

Asked By goldy gupta on 31-Jan-12 12:41 AM
Helo to all i Read alot about WCF and i also Know what are Contracts and Endpoints..

But i don't know the concept of Both..If possible plz tell with Example while using all type of Contarcts and  Example of endpoints..

I don't know that why we take two Endpoints in WCF web. config
smr replied to goldy gupta on 31-Jan-12 12:44 AM
hi

An endpoint is what a service exposes, and in WCF terms, is made up of three things:

  • Address
  • Binding
  • Contract

Address is the URL by which the endpoint can be reached.

Binding dictates transformations that are applied as well as the shape (to some degree) of the messages sent to the implementation of the Contract at the Address.

Contract dictates what operations are being exposed at the address. It's exactly what it says it is, it's a contract to indicate what calls are permissible.

Most of the time, people remember it as A B C.



follow
http://msdn.microsoft.com/en-us/library/aa480210.aspx
http://msdn.microsoft.com/en-us/library/ms733107.aspx
http://msdn.microsoft.com/en-us/library/aa751841.aspx
smr replied to goldy gupta on 31-Jan-12 12:46 AM
hi

A WCF Contract is a collection of Operations that specifies what the Endpoint communicates to the outside world. Each operation is a simple message exchange, for example one-way or request/reply message exchange.

Endpoints

A Service Endpoint has an Address, a Binding, and a Contract.

The Endpoint's Address is a network address where the Endpoint resides. The EndpointAddress class represents a WCF Endpoint Address.

The Endpoint's Binding specifies how the Endpoint communicates with the world including things like transport protocol (e.g., TCP, HTTP), encoding (e.g., text, binary), and security requirements (e.g., SSL, SOAP message security). TheBinding class represents a WCF Binding. Aa480210.wcfarch_01(en-us,MSDN.10).gif
follow http://msdn.microsoft.com/en-us/library/aa480210.aspx

Jitendra Faye replied to goldy gupta on 31-Jan-12 12:48 AM

All communication with a Windows Communication Foundation (WCF) service occurs through the endpoints of the service. Endpoints provide clients access to the functionality offered by a WCF service.


Each endpoint consists of four properties:

  • An address that indicates where the endpoint can be found.

  • A binding that specifies how a client can communicate with the endpoint.

  • A contract that identifies the operations available.

  • A set of behaviors that specify local implementation details of the endpoin


Here you will get information about this.-

http://msdn.microsoft.com/en-us/library/ms733107.aspx
Suchit shah replied to goldy gupta on 31-Jan-12 12:50 AM

A WCF Service is comprised of the following major components. The diagram below shows how the components are related to each other:

  • Service Contract
  • Operation Contract
  • Data Contract
  • Data Member

Service Contract

Service contract is a contract that specifies the direction and type of the messages in a conversation. It is an interface or a class that defines the service contract in a Windows Communication Foundation (WCF) application. A service contract is the gateway to a service for external applications to make use of the service functions, and at least one service contract should be available in a service. A service contract is defined as follows:

 
// Student ServiceContract 
[ServiceContract]
public interface IStudentService
{
// Define the OperationContact here….
}

The ServiceContract attribute of the interface defines the service contract in the service interface. The service contract defines the operations available in the service, operations like web service methods in a web service. IstudentService is a student service interface which exposes all the operation contracts or methods in this service to external systems.

Operation Contract

An operation contract defines the methods of the service that are accessible by external systems. The OperationContract attribute needs to be applied for all these methods, these are also like web methods in a web service. Operation contracts are defined as follows:


// Student ServiceContract

[ServiceContract]
public interface IStudentService
{
// Define the GetStudentFullName OperationContact here….
[OperationContract]
String
GetStudentFullName (int studentId);
// Define the GetStudentInfo OperationContact here….
[OperationContract]
StudentInformation GetStudentInfo (int studentId); }

Data Contract

A data contract defines a type with a set of data members or fields that will be used as the composite type in a service contract. It is a loosely-coupled model that is defined outside the implementation of the service and accessible by services in other platforms. To define a data contract, apply the DataContract attribute to the class to serialize the class by a serializer, and apply the DataMember attribute to the fields in the class that must be serialized. A StudentInformation data contract can be defined as follows:


[DataContract]
public class StudentInformation
{
// Define the Datamembers here….
}

Data Member

A data member specifies the type which is part of a data contract used as a composite type member of the contract. To define a data member, apply the DataMember attribute to the fields that must be serialized. The DataMember attribute can be applied to private properties, but they will be serialized and deserialized, and will be accessible to the user or process. The code below shows how to define a data member in a data contract:

[DataContract] public class StudentInformation { _studentId = studId; [DataMember] public int StudentId { get { return _studentId; } set { _studentId = value; } } }


Hope you get this examples
Venkat K replied to goldy gupta on 31-Jan-12 12:51 AM
Hi goldy,
The use of multiple endpoints comes into picture where you have mutiple clients with different requirements or with different security needs. [Different endpoints with different protocols or different security methods....]

one endpoint using the net.tcp binding with Windows credentials could be used for company-internal clients that are coming from behind the firewall, which are authenticated against your company Active Directory; this binding is fast, performant, security is relatively painless to set up with AD integration

Second endpoint could be using wsHttpBinding and enforce transport-level security (https://) - so certain clients could be calling your service on a secured link

Third endpoint might be using unsecured basicHttpBinding for maximum backward compatibility - e.g. all sorts of clients (also a lot of non .NET clients like Ruby, PHP, other scripting languages etc.) could connect to this endpoint; maybe, your unsecured endpoint wouldn't be allowed to call all methods, or it might have other limitations (e.g. be handled as last priority call, only if capacity allows it)

Thanks
Venkat K replied to goldy gupta on 31-Jan-12 12:52 AM
If you are looking some examples i think msdn has the best than all the other:
http://msdn.microsoft.com/en-us/library/ms752036(v=vs.90).aspx

Thanks
Suchit shah replied to goldy gupta on 31-Jan-12 12:55 AM

EndPoint

WCF Service is a program that exposes a collection of Endpoints. Each Endpoint is a portal for communicating with the world.

All the WCF communications are take place through end point. End point consists of three components.

Address

Basically URL, specifies where this WCF service is hosted .Client will use this url to connect to the service. e.g

http://localhost:8090/MyService/SimpleCalculator.svc

Binding

Binding will describes how client will communicate with service. There are different protocols available for the WCF to communicate to the Client. You can mention the protocol type based on your requirements.

A binding has several characteristics, including the following:

  • Transport -Defines the base protocol to be used like HTTP, Named Pipes, TCP, and MSMQ are some type of protocols.
  • Encoding (Optional) - Three types of encoding are available-Text, Binary, or Message Transmission Optimization Mechanism (MTOM). MTOM is an interoperable message format that allows the effective transmission of attachments or large messages (greater than 64K).
  • Protocol(Optional) - Defines information to be used in the binding such as Security, transaction or reliable messaging capability

The following table gives some list of protocols supported by WCF binding.

Binding Description
BasicHttpBinding Basic Web service communication. No security by default
WSHttpBinding Web services with WS-* support. Supports transactions
WSDualHttpBinding Web services with duplex contract and transaction support
WSFederationHttpBinding Web services with federated security. Supports transactions
MsmqIntegrationBinding Communication directly with MSMQ applications. Supports transactions
NetMsmqBinding Communication between WCF applications by using queuing. Supports transactions
NetNamedPipeBinding Communication between WCF applications on same computer. Supports duplex contracts and transactions
NetPeerTcpBinding Communication between computers across peer-to-peer services. Supports duplex contracts
NetTcpBinding Communication between WCF applications across computers. Supports duplex contracts and transactions

Contract

Collection of operation that specifies what the endpoint will communicate with outside world. Usually name of the Interface will be mentioned in the Contract, so the client application will be aware of the operations which are exposed to the client. Each operation is a simple exchange pattern such as one-way, duplex and request/reply.

Below figure illustrate the functions of Endpoint

Example:


Endpoints will be mentioned in the web.config file on the created service.

<system.serviceModel>
<services>
    <service name="MathService"
      behaviorConfiguration="MathServiceBehavior">
     <endpoint
     address="http://localhost:8090/MyService/MathService.svc" contract="IMathService"
      binding="wsHttpBinding"/>
    </service>
    </services>
    <behaviors>
    <serviceBehaviors>
      <behavior name="MathServiceBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>
    </serviceBehaviors>
    </behaviors>
  </system.serviceModel>



Personally speaking once go through this tutorial it helps me a lot..
definately it will also helps you to better understand it

http://wcftutorial.net/EndPoint.aspx
http://www.codeproject.com/KB/WCF/WCFServiceSample.aspx

http://www.codeproject.com/KB/WCF/WCFOverview.aspx

Chintan Vaghela replied to goldy gupta on 31-Jan-12 01:15 AM

Hello,

 

End Points

The metadata (wsdl) from <serviceMetadata httpGetEnabled> always lives at the base address, and so that's always where you can point svcutil/Add Service Reference.

The <endpoint address> is where the application endpoint lives.  So when you create the client and it talks to the service using the ITestService contract, it does so at that address.

Put another way, the base address is the address of the 'get wsdl' metadata endpoint, and the endpoint address is the address of the application endpoint (with your service's [OperationContract] operations).

 

<system.serviceModel>

 <bindings />

 <behaviors>

  <serviceBehaviors>

   <behavior name="DefaultBehavior">

    <serviceMetadata httpGetEnabled="true" />

   </behavior>

  </serviceBehaviors>

</behaviors>

 <services>

  <service behaviorConfiguration="DefaultBehavior" name="Test.TestService">

   <clear />

   <host>

    <baseAddresses>

   <add baseAddress=http://localhost:8081/Test/ />

    </baseAddresses>

   </host>

   <endpoint address=http://localhost:8082/Test/

   binding="basicHttpBinding" bindingConfiguration="" name="Web"

   contract="Test.ITestService" />

  </service>

 </services>

</system.serviceModel>

The service host is created the following way:

ServiceHost host = new ServiceHost(typeof(TestService));

host.Open();

 

 

 

 

Hope this is helpful !

Thanks

 

 

 

 

 

goldy gupta replied to Suchit shah on 31-Jan-12 01:15 AM
Ok Thanks Suchit i will get back to You if i will have any query..
goldy gupta replied to Chintan Vaghela on 31-Jan-12 01:16 AM
ok Thanks
Suchit shah replied to goldy gupta on 31-Jan-12 01:16 AM
Sure YOU ARE MOST WELCOME
Riley K replied to goldy gupta on 31-Jan-12 01:21 AM


In order to host a service you create a ServiceHost instance and define a collection of service endpoints.

A service endpoint specifies an address, a binding, and a contract to use for communication.
Windows Communication Foundation needs this information to build the necessary messaging runtime, also known as the channel stack, and to provide metadata in the form of Web Services Description Language (WSDL) 

the below article discussed about WCF addressing in depth

http://msdn.microsoft.com/en-us/magazine/cc163412.aspx

Regards

Chintan Vaghela replied to goldy gupta on 31-Jan-12 03:59 AM
Welcome :)