c# advanced l08-networking+wcf

63
Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 C# Advanced L08Networking and WCF (Windows Communication Foundation)

Upload: mohammad-shaker

Post on 13-Jan-2015

270 views

Category:

Software


2 download

DESCRIPTION

C# Advanced L08-Networking+WCF

TRANSCRIPT

Page 1: C# Advanced L08-Networking+WCF

Mohammad Shaker

mohammadshaker.com

@ZGTRShaker

2011, 2012, 2013, 2014

C# AdvancedL08–Networking and WCF (Windows

Communication Foundation)

Page 2: C# Advanced L08-Networking+WCF

Communication and Networking

Page 3: C# Advanced L08-Networking+WCF

TCP/UDP Protocols

Page 4: C# Advanced L08-Networking+WCF

Socket

Page 5: C# Advanced L08-Networking+WCF

TutorialDigging Deep from the First Time / Client-Server Application

Page 6: C# Advanced L08-Networking+WCF

TutorialTCP/IP Client-Server Application

Page 7: C# Advanced L08-Networking+WCF

TutorialTCP/IP Client-Server Application – Setting up the Server and the Client

Page 8: C# Advanced L08-Networking+WCF

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

Page 9: C# Advanced L08-Networking+WCF

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

Page 10: C# Advanced L08-Networking+WCF

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

Page 11: C# Advanced L08-Networking+WCF

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

Page 12: C# Advanced L08-Networking+WCF

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

Page 13: C# Advanced L08-Networking+WCF

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!

Page 14: C# Advanced L08-Networking+WCF

TutorialTCP/IP Client-Server Application – Sending and Receiving Data

Page 15: C# Advanced L08-Networking+WCF

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

Page 16: C# Advanced L08-Networking+WCF

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

Page 17: C# Advanced L08-Networking+WCF

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

Page 18: C# Advanced L08-Networking+WCF

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);

}

Page 19: C# Advanced L08-Networking+WCF

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

Page 20: C# Advanced L08-Networking+WCF

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

Page 21: C# Advanced L08-Networking+WCF

WCF

Page 22: C# Advanced L08-Networking+WCF

WCFWindows Communication Foundation

Page 23: C# Advanced L08-Networking+WCF

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.

Page 24: C# Advanced L08-Networking+WCF

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.

Page 25: C# Advanced L08-Networking+WCF

SOA Service Oriented ArchitectureRESTRepresentational State TransferSOAPSimple Object Access ProtocolJSONJavascript Object Notation

Page 26: C# Advanced L08-Networking+WCF

Communications

Basic, OpenComms

ASMX Ent Services

Secure,

Transactional

WSE

Secure, Open Comms

Sys.Messaging

MSMQ, Txns,

Queuing

Fast, Secure,Binary

Remoting

Page 27: C# Advanced L08-Networking+WCF

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

Page 28: C# Advanced L08-Networking+WCF

The Big Picture

OData(HTTP, AtomPub, JSON)

Protocol

WCF Data Servicesothers ...

Producers

WCF Data Servicesothers ....

Consumers

Page 29: C# Advanced L08-Networking+WCF

Mechanics of Communication

“Service”“Client”

EndpointContractBindingAddress

EndpointContractBindingAddress

EndpointContractBindingAddress

Page 30: C# Advanced L08-Networking+WCF

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?

Page 31: C# Advanced L08-Networking+WCF

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

Page 32: C# Advanced L08-Networking+WCF

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

Page 33: C# Advanced L08-Networking+WCF

• “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

Page 34: C# Advanced L08-Networking+WCF

RESTfulWeb Services

Page 35: C# Advanced L08-Networking+WCF

Basis of Windows Communication Foundationhttp://www.dotnet-tricks.com/Tutorial/wcf/YH3R060312-Introduction-to-WCF.html

Page 36: C# Advanced L08-Networking+WCF

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.

Page 37: C# Advanced L08-Networking+WCF

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.

Page 38: C# Advanced L08-Networking+WCF

Difference between WCF and ASP.NET Web Servicehttp://www.dotnet-tricks.com/Tutorial/wcf/cH1H200314-Difference-between-WCF-and-ASP.NET-Web-Service.html

Page 39: C# Advanced L08-Networking+WCF

WCF TutorialRead Chapter 35

Page 40: C# Advanced L08-Networking+WCF

Self-Hosted ServiceWCF Live Demo

Page 41: C# Advanced L08-Networking+WCF

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.

Page 42: C# Advanced L08-Networking+WCF

Self-Hosted Service System A: Creating a new WCF Service

Page 43: C# Advanced L08-Networking+WCF

System A, Adding WCF Service (Add > New Item)

Page 44: C# Advanced L08-Networking+WCF

WCF Setupnamespace WPF_RFID3DProject

{

[ServiceContract]

public interface IServiceTest

{

[OperationContract]

void DoWork();

}

}

Page 45: C# Advanced L08-Networking+WCF

Self-Hosted Service System A: Setting the Service in app.config

Page 46: C# Advanced L08-Networking+WCF

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>

Page 47: C# Advanced L08-Networking+WCF

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>

Page 48: C# Advanced L08-Networking+WCF

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>

Page 49: C# Advanced L08-Networking+WCF

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>

Page 50: C# Advanced L08-Networking+WCF

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>

Page 51: C# Advanced L08-Networking+WCF

Self-Hosted ServiceSystem A, etting the WCF Service Function

Page 52: C# Advanced L08-Networking+WCF

WCF Setupnamespace WPF_RFID3DProject

{

[ServiceContract]

public interface IServiceTest

{

[OperationContract]

void DoWork();

}

}

Page 53: C# Advanced L08-Networking+WCF

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();

}

}

Page 54: C# Advanced L08-Networking+WCF

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);

}

}

}

Page 55: C# Advanced L08-Networking+WCF

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);

}

}

}

Page 56: C# Advanced L08-Networking+WCF

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);

}

}

}

Page 57: C# Advanced L08-Networking+WCF

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);

}

}

}

Page 58: C# Advanced L08-Networking+WCF

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.

Page 59: C# Advanced L08-Networking+WCF

Self-Hosted Service System B, Calling the WCF Service in System A

Page 60: C# Advanced L08-Networking+WCF

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

Page 61: C# Advanced L08-Networking+WCF

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

Page 62: C# Advanced L08-Networking+WCF

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

Page 63: C# Advanced L08-Networking+WCF

Security Breach!