c# advanced l08-networking+wcf
DESCRIPTION
C# Advanced L08-Networking+WCFTRANSCRIPT
Mohammad Shaker
mohammadshaker.com
@ZGTRShaker
2011, 2012, 2013, 2014
C# AdvancedL08–Networking and WCF (Windows
Communication Foundation)
Communication and Networking
TCP/UDP Protocols
Socket
TutorialDigging Deep from the First Time / Client-Server Application
TutorialTCP/IP Client-Server Application
TutorialTCP/IP Client-Server Application – Setting up the Server and the Client
TCP/IP Chat Application
private Socket _clientSocket; //The main client socketpublic void InitliazeConnection(String serverIPAddress){
//We are using TCP sockets_clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse(serverIPAddress);
//Server is listening on port 3000IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 3000);
//Connect to the server_clientSocket.BeginConnect(ipEndPoint,
new AsyncCallback(OnConnect), null);}
private Socket _serverSocket;private void InitliazeConnection(){
//We are using TCP sockets_serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
ServerIPAdress = IPAddress.Parse("127.0.0.1");
//Assign the any IP of the machine and listen on port number 3000IPEndPoint ipEndPoint = new IPEndPoint(this.ServerIPAdress, 3000);
//Bind and listen on the given address_serverSocket.Bind(ipEndPoint);_serverSocket.Listen(4);
//Accept the incoming clients_serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
}
Server Client
TCP/IP Chat Application
private Socket _clientSocket; //The main client socketpublic void InitliazeConnection(String serverIPAddress){
//We are using TCP sockets_clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse(serverIPAddress);
//Server is listening on port 3000IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 3000);
//Connect to the server_clientSocket.BeginConnect(ipEndPoint,
new AsyncCallback(OnConnect), null);}
private Socket _serverSocket;private void InitliazeConnection(){
//We are using TCP sockets_serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
ServerIPAdress = IPAddress.Parse("127.0.0.1");
//Assign the any IP of the machine and listen on port number 3000IPEndPoint ipEndPoint = new IPEndPoint(this.ServerIPAdress, 3000);
//Bind and listen on the given address_serverSocket.Bind(ipEndPoint);_serverSocket.Listen(4);
//Accept the incoming clients_serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
}
Server Client
Both should connect through a Socket
TCP/IP Chat Application
private Socket _clientSocket; //The main client socketpublic void InitliazeConnection(String serverIPAddress){
//We are using TCP sockets_clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse(serverIPAddress);
//Server is listening on port 3000IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 3000);
//Connect to the server_clientSocket.BeginConnect(ipEndPoint,
new AsyncCallback(OnConnect), null);}
private Socket _serverSocket;private void InitliazeConnection(){
//We are using TCP sockets_serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
ServerIPAdress = IPAddress.Parse("127.0.0.1");
//Assign the any IP of the machine and listen on port number 3000IPEndPoint ipEndPoint = new IPEndPoint(this.ServerIPAdress, 3000);
//Bind and listen on the given address_serverSocket.Bind(ipEndPoint);_serverSocket.Listen(4);
//Accept the incoming clients_serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
}
Server Client
Client should Connect to the Server
TCP/IP Chat Application
private Socket _clientSocket; //The main client socketpublic void InitliazeConnection(String serverIPAddress){
//We are using TCP sockets_clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse(serverIPAddress);
//Server is listening on port 3000IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 3000);
//Connect to the server_clientSocket.BeginConnect(ipEndPoint,
new AsyncCallback(OnConnect), null);}
private Socket _serverSocket;private void InitliazeConnection(){
//We are using TCP sockets_serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
ServerIPAdress = IPAddress.Parse("127.0.0.1");
//Assign the any IP of the machine and listen on port number 3000IPEndPoint ipEndPoint = new IPEndPoint(this.ServerIPAdress, 3000);
//Bind and listen on the given address_serverSocket.Bind(ipEndPoint);_serverSocket.Listen(4);
//Accept the incoming clients_serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
}
Server Client
Client should connect to the server port with the same IP Address
TCP/IP Chat Application
private Socket _clientSocket; //The main client socketpublic void InitliazeConnection(String serverIPAddress){
//We are using TCP sockets_clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse(serverIPAddress);
//Server is listening on port 3000IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 3000);
//Connect to the server_clientSocket.BeginConnect(ipEndPoint,
new AsyncCallback(OnConnect), null);}
private Socket _serverSocket;private void InitliazeConnection(){
//We are using TCP sockets_serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
ServerIPAdress = IPAddress.Parse("127.0.0.1");
//Assign the any IP of the machine and listen on port number 3000IPEndPoint ipEndPoint = new IPEndPoint(this.ServerIPAdress, 3000);
//Bind and listen on the given address_serverSocket.Bind(ipEndPoint);_serverSocket.Listen(4);
//Accept the incoming clients_serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
}
Server ClientStarting the Server and begin accepting clients
TCP/IP Chat Application
private Socket _clientSocket; //The main client socketpublic void InitliazeConnection(String serverIPAddress){
//We are using TCP sockets_clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse(serverIPAddress);
//Server is listening on port 3000IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 3000);
//Connect to the server_clientSocket.BeginConnect(ipEndPoint,
new AsyncCallback(OnConnect), null);}
private Socket _serverSocket;private void InitliazeConnection(){
//We are using TCP sockets_serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
ServerIPAdress = IPAddress.Parse("127.0.0.1");
//Assign the any IP of the machine and listen on port number 3000IPEndPoint ipEndPoint = new IPEndPoint(this.ServerIPAdress, 3000);
//Bind and listen on the given address_serverSocket.Bind(ipEndPoint);_serverSocket.Listen(4);
//Accept the incoming clients_serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
}
Server ClientConnect to the server!
TutorialTCP/IP Client-Server Application – Sending and Receiving Data
TCP/IP Chat Application
private Socket _clientSocket; //The main client socketpublic void InitliazeConnection(String serverIPAddress){
//We are using TCP sockets_clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse(serverIPAddress);
//Server is listening on port 3000IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 3000);
//Connect to the server_clientSocket.BeginConnect(ipEndPoint,
new AsyncCallback(OnConnect), null);}
private Socket _serverSocket;private void InitliazeConnection(){
//We are using TCP sockets_serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
ServerIPAdress = IPAddress.Parse("127.0.0.1");
//Assign the any IP of the machine and listen on port number 3000IPEndPoint ipEndPoint = new IPEndPoint(this.ServerIPAdress, 3000);
//Bind and listen on the given address_serverSocket.Bind(ipEndPoint);_serverSocket.Listen(4);
//Accept the incoming clients_serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
}
Server ClientEvent handlers
TCP/IP Chat Application
private void OnConnect(IAsyncResult ar){
_clientSocket.EndConnect(ar);
//We are connected so we login into the serverDataUnit duToSend = new DataUnit();duToSend.ClientCmd = ClientCommand.Login;duToSend.ClientName =_clientName;duToSend.MsgText = null;
byte[] b = duToSend.ToByte();
//Send the message to the server_clientSocket.BeginSend(b, 0, b.Length,
SocketFlags.None, new AsyncCallback(OnSend), null);
_clientSocket.BeginReceive(_byteDataUnit,0,_byteDataUnit.Length,SocketFlags.None,new AsyncCallback(OnReceive),null);
}
private void OnAccept(IAsyncResult ar){
Socket clientSocket = _serverSocket.EndAccept(ar);
//Start listening for more clients_serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
//Once the client connects; start receiving commands from himclientSocket.BeginReceive(byteDataUnit, 0,
byteDataUnit.Length, SocketFlags.None,new AsyncCallback(OnReceive), clientSocket);
}
Server Client
private void OnAccept(IAsyncResult ar){
Socket clientSocket = _serverSocket.EndAccept(ar);
//Start listening for more clients_serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
//Once the client connects; start receiving commands from himclientSocket.BeginReceive(byteDataUnit, 0,
byteDataUnit.Length, SocketFlags.None,new AsyncCallback(OnReceive), clientSocket);
}
TCP/IP Chat Application
private void OnConnect(IAsyncResult ar){
_clientSocket.EndConnect(ar);
//We are connected so we login into the serverDataUnit duToSend = new DataUnit();duToSend.ClientCmd = ClientCommand.Login;duToSend.ClientName =_clientName;duToSend.MsgText = null;
byte[] b = duToSend.ToByte();
//Send the message to the server_clientSocket.BeginSend(b, 0, b.Length,
SocketFlags.None, new AsyncCallback(OnSend), null);
_clientSocket.BeginReceive(_byteDataUnit,0,_byteDataUnit.Length,SocketFlags.None,new AsyncCallback(OnReceive),null);
}
Server ClientEvent handlers
TCP/IP Chat Application
private void OnSend(IAsyncResult ar){_clientSocket.EndSend(ar);}
private void OnReceive(IAsyncResult ar){Socket clientSocket = (Socket)ar.AsyncState;clientSocket.EndReceive(ar);
//Transform the array of bytes received from the user into DataUnitDataUnit duReceived = new DataUnit(byteDataUnit);}
Server Client
private void OnReceive(IAsyncResult ar){_clientSocket.EndReceive(ar);DataUnit duReceived = new DataUnit(_byteDataUnit);
_byteDataUnit = new byte[1024];_clientSocket.BeginReceive(_byteDataUnit,
0,_byteDataUnit.Length,SocketFlags.None,new AsyncCallback(OnReceive),null);
}
TCP/IP Chat Application
public void SendDataUnit(String msgToSend, String otherClientName){DataUnit duToSend = new DataUnit();duToSend.MsgText = msgToSend;
byte[] byteDataUnit = duToSend.ToByte();
//Send it to the server_clientSocket.BeginSend(byteDataUnit, 0, byteDataUnit.Length, SocketFlags.None, new AsyncCallback(OnSend), null);}
Client – Send Data
TCP/IP Chat Application
public void SendDataUnit(String msgToSend, String otherClientName){DataUnit duToSend = new DataUnit();duToSend.MsgText = msgToSend;
byte[] byteDataUnit = duToSend.ToByte();
//Send it to the server_clientSocket.BeginSend(byteDataUnit, 0, byteDataUnit.Length, SocketFlags.None, new AsyncCallback(OnSend), null);}
Client – Send Data
WCF
WCFWindows Communication Foundation
WCFis a technology that enables you to create services that you can access from other applications
across process, machine, and network boundaries. You can use these services to share functionality across multiple applications, to expose data sources, or to abstract complicated processes.
Before there was WCFBefore WCF Web Services are used to create service in .NET Framework. Now WCF is the latest programming model for
building and developing service-oriented application. WCF allows applications to communicate with each other in distributed environment. WCF is a set of technologies that covers ASMX web services, Web Services Enhancements (WSE), .NET
Remoting and MSMQ.
SOA Service Oriented ArchitectureRESTRepresentational State TransferSOAPSimple Object Access ProtocolJSONJavascript Object Notation
Communications
Basic, OpenComms
ASMX Ent Services
Secure,
Transactional
WSE
Secure, Open Comms
Sys.Messaging
MSMQ, Txns,
Queuing
Fast, Secure,Binary
Remoting
History
• 2006 – Codename “Astoria”
• 2008 – First release of “ADO.NET Data Services”
• 2009 ...
– Renamed to “WCF Data Services”
– OData announced and released under the
“Open Specification Promise”
• 2011 – March 2011 CTP2 for .NET4 & SL4
The Big Picture
OData(HTTP, AtomPub, JSON)
Protocol
WCF Data Servicesothers ...
Producers
WCF Data Servicesothers ....
Consumers
Mechanics of Communication
“Service”“Client”
EndpointContractBindingAddress
EndpointContractBindingAddress
EndpointContractBindingAddress
Mechanics of Communication
“Service”“Client”
EndpointContractBindingAddress
EndpointContractBindingAddress
EndpointContractBindingAddress
What do I send?
Where do I send it?
How should I send it?
Contract
Address
Binding
Transport?
Encoding?
Security?
WCF Bindings
• A binding describes the transport, protocol, and encoding.
• BasicHttpBinding
– A binding that is suitable for communicating with WS-Basic Profile conformant Web services,
for example, ASP.NET Web services (ASMX)-based services. This binding uses HTTP as the
transport and text/XML as the default message encoding.
• WSHttpBinding
• WS2007HttpBinding
• NetTcpBinding
• NetNamedPipeBinding
• WebHttpBinding
REST• Resources are the center piece
– resources have addresses (URIs)
– no custom behavior
• Uniform interface ...
– fixed set of operations (GET, POST, PUT, DELETE)
OData• Open Data Protocol
• Design goals ...
– Invent as little as possible
– Very low barrier of entry
• OData is a RESTful protocol
• “Representation State Transfer” is the idea behind resource oriented architecture
ResourceClient
http://www.myblog.com/{year}/{month}/{day}
The Client references a Web resource using a URL. A representationof the resource is returned which can be rendered as an HTML.
John Post
REST
RESTfulWeb Services
Basis of Windows Communication Foundationhttp://www.dotnet-tricks.com/Tutorial/wcf/YH3R060312-Introduction-to-WCF.html
Basis of Windows Communication Foundation
• Address (Where)
It specifies the location of the service means, where the service is hosted. The service
hosting URL may be like http://server/wcfService. Clients will use this location to
communicate with your service.
• Binding (How)
It specifies how the client will communicate to the service. We have different protocols (like
http, tcp, named pipe, msmq) for the WCF to communicate to the client.
• Contract (What)
It specifies what the service will do. For this purpose we have different contract like as Data
Contract, Operation Contract, Message Contract, Fault Contract. I will discuss all these later.
WCF Hosting• Self hosting
A WCF service can be self-hosted, which means that the service runs as a standalone application and controls its own
lifetime. This is the most flexible and easiest way of hosting a WCF service, but its availability and features are limited.
• Windows services hosting
A WCF service can also be hosted as a Windows service. A Windows service is a process managed by the operating
system and it is automatically started when Windows is started (if it is configured to do so). However, it lacks some
critical features (such as versioning) for WCF services.
• IIS hosting
A better way of hosting a WCF service is to use IIS. This is the traditional way of hosting a web service. IIS, by nature,
has many useful features, such as process recycling, idle shutdown, process health monitoring, message-based
activation, high availability, easy manageability, versioning, and deployment scenarios. All of these features are required
for enterprise-level WCF services.
• Windows Activation Services hosting
Windows Process Activation Service (WAS) is the new process activation mechanism for Windows Server 2008 that is
also available on Windows Vista. WAS hosting is possible only with IIS 7.0.Additional WCF components also plug into
WAS to provide message-based activation over the other protocols that WCF supports, such as TCP, MSMQ, and named
pipes. This allows applications that use the non-HTTP communication protocols to use the IIS features such as process
recycling, rapid fail protection, and the common configuration systems that were only available to HTTP-based
applications.
Difference between WCF and ASP.NET Web Servicehttp://www.dotnet-tricks.com/Tutorial/wcf/cH1H200314-Difference-between-WCF-and-ASP.NET-Web-Service.html
WCF TutorialRead Chapter 35
Self-Hosted ServiceWCF Live Demo
Self-Hosted Service, Test CaseWe are a very important company and we want everything to be as secure as possible. The IT security personnel is provided with an online WPF application page (System A) that shows all the company doors status at realtime.
We want you to create a WCF self-hosted service (in System A) that will be called when a breach is done. The hardware monitoring system (System B) is responsible for the detection of any security breach through doors or
windows. It will sends an alarm notification to System A by calling the WCF Service through its interface.
Self-Hosted Service System A: Creating a new WCF Service
System A, Adding WCF Service (Add > New Item)
WCF Setupnamespace WPF_RFID3DProject
{
[ServiceContract]
public interface IServiceTest
{
[OperationContract]
void DoWork();
}
}
Self-Hosted Service System A: Setting the Service in app.config
System A, at app.config<system.serviceModel>
<services>
<service name="WPF_RFID3DProject.WCFEngine.AppControlService">
<endpoint address="" binding="netTcpBinding" contract="WPF_RFID3DProject.WCFEngine.IAppControlService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8739/Design_Time_Addresses/WPF_RFID3DProject.WCFEngine/AppControlService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
System A, at app.config<system.serviceModel>
<services>
<service name="WPF_RFID3DProject.WCFEngine.AppControlService">
<endpoint address="" binding="netTcpBinding" contract="WPF_RFID3DProject.WCFEngine.IAppControlService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8739/Design_Time_Addresses/WPF_RFID3DProject.WCFEngine/AppControlService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
System A, at app.config<system.serviceModel>
<services>
<service name="WPF_RFID3DProject.WCFEngine.AppControlService">
<endpoint address="" binding="netTcpBinding" contract="WPF_RFID3DProject.WCFEngine.IAppControlService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8739/Design_Time_Addresses/WPF_RFID3DProject.WCFEngine/AppControlService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
System A, at app.config<system.serviceModel>
<services>
<service name="WPF_RFID3DProject.WCFEngine.AppControlService">
<endpoint address="" binding="netTcpBinding" contract="WPF_RFID3DProject.WCFEngine.IAppControlService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8739/Design_Time_Addresses/WPF_RFID3DProject.WCFEngine/AppControlService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
System A, at app.config<system.serviceModel>
<services>
<service name="WPF_RFID3DProject.WCFEngine.AppControlService">
<endpoint address="" binding="netTcpBinding" contract="WPF_RFID3DProject.WCFEngine.IAppControlService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8739/Design_Time_Addresses/WPF_RFID3DProject.WCFEngine/AppControlService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Self-Hosted ServiceSystem A, etting the WCF Service Function
WCF Setupnamespace WPF_RFID3DProject
{
[ServiceContract]
public interface IServiceTest
{
[OperationContract]
void DoWork();
}
}
WCF Setup
namespace WPF_RFID3DProject
{
[ServiceContract]
public interface IAppControlService
{
[OperationContract]
void FireBreachAlarm(string cardId, int oldHallId, string oldEntryDate, int newHallId, string newEntryDate);
}
}
• Change it to
namespace WPF_RFID3DProject
{
[ServiceContract]
public interface IServiceTest
{
[OperationContract]
void DoWork();
}
}
WCF Setupnamespace WPF_RFID3DProject
{
[ServiceContract]
public interface IAppControlService
{
[OperationContract]
void FireBreachAlarm(string cardId, int oldHallId, string oldEntryDate, int newHallId, string newEntryDate);
}
}
namespace WPF_RFID3DProject.WCFEngine
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class AppControlService : IAppControlService
{
private Page1 hostApp;
public AppControlService(Page1 hostApp)
{
this.hostApp = hostApp;
}
public void FireBreachAlarm(string cardId, int oldHallId, string oldEntryDate, int newHallId, string newEntryDate)
{
hostApp.FireBreachAlarm(cardId, oldHallId, oldEntryDate, newHallId, newEntryDate);
}
}
}
WCF Setupnamespace WPF_RFID3DProject
{
[ServiceContract]
public interface IAppControlService
{
[OperationContract]
void FireBreachAlarm(string cardId, int oldHallId, string oldEntryDate, int newHallId, string newEntryDate);
}
}
namespace WPF_RFID3DProject.WCFEngine
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class AppControlService : IAppControlService
{
private Page1 hostApp;
public AppControlService(Page1 hostApp)
{
this.hostApp = hostApp;
}
public void FireBreachAlarm(string cardId, int oldHallId, string oldEntryDate, int newHallId, string newEntryDate)
{
hostApp.FireBreachAlarm(cardId, oldHallId, oldEntryDate, newHallId, newEntryDate);
}
}
}
WCF Setupnamespace WPF_RFID3DProject
{
[ServiceContract]
public interface IAppControlService
{
[OperationContract]
void FireBreachAlarm(string cardId, int oldHallId, string oldEntryDate, int newHallId, string newEntryDate);
}
}
namespace WPF_RFID3DProject.WCFEngine
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class AppControlService : IAppControlService
{
private Page1 hostApp;
public AppControlService(Page1 hostApp)
{
this.hostApp = hostApp;
}
public void FireBreachAlarm(string cardId, int oldHallId, string oldEntryDate, int newHallId, string newEntryDate)
{
hostApp.FireBreachAlarm(cardId, oldHallId, oldEntryDate, newHallId, newEntryDate);
}
}
}
WCF Setupnamespace WPF_RFID3DProject
{
[ServiceContract]
public interface IAppControlService
{
[OperationContract]
void FireBreachAlarm(string cardId, int oldHallId, string oldEntryDate, int newHallId, string newEntryDate);
}
}
namespace WPF_RFID3DProject.WCFEngine
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class AppControlService : IAppControlService
{
private Page1 hostApp;
public AppControlService(Page1 hostApp)
{
this.hostApp = hostApp;
}
public void FireBreachAlarm(string cardId, int oldHallId, string oldEntryDate, int newHallId, string newEntryDate)
{
hostApp.FireBreachAlarm(cardId, oldHallId, oldEntryDate, newHallId, newEntryDate);
}
}
}
WCF Setupnamespace WPF_RFID3DProject
{
[ServiceContract]
public interface IAppControlService
{
[OperationContract]
void FireBreachAlarm(string cardId, int oldHallId, string oldEntryDate, int newHallId, string newEntryDate);
}
}
namespace WPF_RFID3DProject.WCFEngine
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class AppControlService : IAppControlService
{
private Page1 hostApp;
public AppControlService(Page1 hostApp)
{
this.hostApp = hostApp;
}
public void FireBreachAlarm(string cardId, int oldHallId, string oldEntryDate, int newHallId, string newEntryDate)
{
hostApp.FireBreachAlarm(cardId, oldHallId, oldEntryDate, newHallId, newEntryDate);
}
}
}
private void InitializeWCFService()
{
try
{
service = new AppControlService(this);
host = new ServiceHost(service);
host.Open();
}
catch(Exception e)
{
host.Close();
}
}
In Page1: We simply run the service so that other applications can call it.
Self-Hosted Service System B, Calling the WCF Service in System A
System B, Calling the WCF Service in System A
public void CallWCFServiceFireBreachAlarm(string cardID, int oldHallID, string oldEntryDate, int newHallID, string newEntryDate)
{
IAppControlService client =
ChannelFactory<IAppControlService>.CreateChannel(new NetTcpBinding(),
new EndpointAddress("net.tcp://localhost:8739/Design_Time_Addresses/WPF_RFID3DProject.WCFEngine/AppControlService/"));
client.FireBreachAlarm(cardID, oldHallID, oldEntryDate, newHallID, newEntryDate);
}
System B
System B, Calling the WCF Service in System A
public void CallWCFServiceFireBreachAlarm(string cardID, int oldHallID, string oldEntryDate, int newHallID, string newEntryDate)
{
IAppControlService client =
ChannelFactory<IAppControlService>.CreateChannel(new NetTcpBinding(),
new EndpointAddress("net.tcp://localhost:8739/Design_Time_Addresses/WPF_RFID3DProject.WCFEngine/AppControlService/"));
client.FireBreachAlarm(cardID, oldHallID, oldEntryDate, newHallID, newEntryDate);
}
<system.serviceModel>
<services>
<service name="WPF_RFID3DProject.WCFEngine.AppControlService">
<endpoint address="" binding="netTcpBinding" contract="WPF_RFID3DProject.WCFEngine.IAppControlService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8739/Design_Time_Addresses/WPF_RFID3DProject.WCFEngine/AppControlService/" />
</baseAddresses>
</host>
…..
System B
System A
System B, Calling the WCF Service in System A
public void CallWCFServiceFireBreachAlarm(string cardID, int oldHallID, string oldEntryDate, int newHallID, string newEntryDate)
{
IAppControlService client =
ChannelFactory<IAppControlService>.CreateChannel(new NetTcpBinding(),
new EndpointAddress("net.tcp://localhost:8739/Design_Time_Addresses/WPF_RFID3DProject.WCFEngine/AppControlService/"));
client.FireBreachAlarm(cardID, oldHallID, oldEntryDate, newHallID, newEntryDate);
}
<system.serviceModel>
<services>
<service name="WPF_RFID3DProject.WCFEngine.AppControlService">
<endpoint address="" binding="netTcpBinding" contract="WPF_RFID3DProject.WCFEngine.IAppControlService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8739/Design_Time_Addresses/WPF_RFID3DProject.WCFEngine/AppControlService/" />
</baseAddresses>
</host>
…..
The same data we entered as the WCF service property
System B
System A
Security Breach!