[ServiceContract]
Features | Web Service | WCF |
---|---|---|
Hosting | It can be hosted in IIS | It can be hosted in IIS, windows activation service, Self-hosting, Windows service |
Programming | [WebService] attribute has to be added to the class | [ServiceContraact] attribute has to be added to the class |
Model | [WebMethod] attribute represents the method exposed to client | [OperationContract] attribute represents the method exposed to client |
Operation | One-way, Request- Response are the different operations supported in web service | One-Way, Request-Response, Duplex are different type of operations supported in WCF |
XML | System.Xml.serialization name space is used for serialization | System.Runtime.Serialization namespace is used for serialization |
Encoding | XML 1.0, MTOM(Message Transmission Optimization Mechanism), DIME, Custom | XML 1.0, MTOM, Binary, Custom |
Transports | Can be accessed through HTTP, TCP, Custom | Can be accessed through HTTP, TCP, Named pipes, MSMQ,P2P, Custom |
Protocols | Security | Security, Reliable messaging, Transactions |
[ServiceContract]
interface IMyContract
{
[OperationContract]
string MyMethod();
}
class MyService : IMyContract
{
public string MyMethod()
{
return "Namaste";
}​
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
public class Student
{
private string _Name;
private string _City;
[DataMember]
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
}
[MessageContract]
public class AutherRequest
{
public string AutherId;
}
[MessageContract]
public class AutherRequest
{
[MessageHeader]
public string AutherId;
}
[MessageContract]
public class AuthorResponse
{
[MessageBodyMember]
public Auther Obj;
}
[ServiceContract]
public interface IGetDetailsService
{
[OperationContract]
[FaultContract(typeof (Student))]
Student GetDetails(string Name);
}
[DataContract]
public class Student
{
private string _Name;
private string _City;
[DataMember]
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
[DataMember]
public string City
{
get
{
return _City;
}
set
{
_City = value;
}
}
}
DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(MyTestClassType));
Attribute / Property | Description |
---|---|
maxConcurrentCalls | This specifies the maximum number of messages processed across the service host. The default value for this property is 16 (WCF 4.0 is improved to default is 16 * Processor Count). |
maxConcurrentInstances | This specifies the maximum number of instances of a context object that executes at one time with the service. The default is Int32.MaxValue. |
maxConcurrentSessions | This specifies the maximum number of sessions at one time within the service host object. The default value is 10 (WCF 4.0 increases that to 100 * Processor Count). |
<configuration>
<system.serviceModel>
<services>
<service .... .... .... </service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceThrottling maxConcurrentCalls="16" maxConcurrentSessions="100" maxConcurrentInstances="10" />
<serviceMetadata httpGetEnabled="true" /> </behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
DataContractSerializer | XMLSerializer |
---|---|
For WCF, DataContractSerializer is the default serializer | XMLSerializer is not a default serializer |
Compare to XMLSerializer it is faster | XMLSerializer is slower |
It is used for simple schemes | It is used for complex schemes |
<serviceMetadata>
tag is used in web.config file without specifying the endpoint.<serviceMetadata>
tag is used in web.config file with specifying the defined endpoint.public void CreateUser(User user)
{
if(user.isValid())
{
//Create User
}
else
{
throw new ApplicationException(“User Inavalid.”);
}
}
public class DBManagerService: IDBManagerService
void Save(Employee emp)
{
try
{
//Code to store an employee object to the database
}
catch (Exception ex
{
throw new Exception(“Error occurred
while saving data…”);
}
}
}
Criteria | BasicHttpBinding | WsHttpBinding |
---|---|---|
Security support | This supports the old ASMX style, i.e., WS-BasicProfile 1.1. | This exposes web services using WS-* specifications. |
Compatibility | This is aimed for clients who do not have .NET 3.0 installed and it supports wider ranges of clients. Many of the clients like Windows 2000 still do not run .NET 3.0. So an older version of .NET can consume this service. | As it is built using WS-* specifications, it does not support wider ranges of clients and it cannot be consumed by older .NET versions less than 3 version. |
SOAP version | SOAP 1.1 | SOAP 1.2 and WS-Addressing specification. |
Reliable messaging | Not supported. In other words, if a client fires two or three calls you really do not know if they will return back in the same order. | Supported as it supports WS-* specifications. |
Default security options | By default, there is no security provided for messages when the client calls happen. In other words, data is sent as plain text. | As WsHttBinding supports WS-*, it has WS-Security enabled by default. So the data is not sent in plain text. |
Security options | • None • Windows – default authentication • Basic • Certificate |
• None • Transport • Message • Transport with message credentials |