forked from shangerxin/BookNotes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fdbe960
Showing
525 changed files
with
133,406 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,338 @@ | ||
WCF Jumpstart=Brain Noyes;Note=Erxin | ||
|
||
# Hello WCF | ||
- Course Outline | ||
- WCF Overview | ||
+ windows communication foundation | ||
+ platform for building distributed, service-oriented applications | ||
* define services and hosts for those services | ||
* define clients to connect to services | ||
|
||
+ SOAP messaging, xml based message protocol, not required to http protocol | ||
+ abstracts your code from wire-level protocol details | ||
focus on business operations instead of plumbing | ||
+ make it easy for .net object-oriented programmer to build distributed service-oritendted applications | ||
* secure | ||
* scalable | ||
* robust | ||
* flexible | ||
|
||
- WCF Building Blocks | ||
+ service contract | ||
* data contract | ||
* service | ||
+ graphic | ||
|
||
+--------------+ +--------------------+ | ||
|consumer | |service host | | ||
| | | | +-----------------+| | ||
| V | | |service contract || | ||
| proxy--------------------> || | ||
| | | | +-------------+ || | ||
| | | | |data contract| || | ||
| | | | +-------------+ || | ||
| | | +-----------------+| | ||
| | | +-----------------+| | ||
| | | |service || | ||
| | | +-----------------+| | ||
| | | +-----------------+| | ||
| configuration| | |configuration || | ||
| | | +-----------------+| | ||
+--------------+ +--------------------+ | ||
|
||
- wcf configuration | ||
+ endpoints, compose ABC, | ||
* address, how client get to service | ||
* binding, how communication works at wire level | ||
* contract, defines what's expose | ||
|
||
+ example of phone call | ||
* number -> address | ||
* conversation content -> contract | ||
* same language -> binding | ||
|
||
- Demo hello wcf service | ||
+ debug start external program | ||
|
||
- Demo hello wcf client | ||
+ check the project option, wcf server option WCF Options| Start WCF Service Host when debugging another project in the same solution | ||
|
||
- Demo service app solution tour | ||
|
||
|
||
# Implementing Services | ||
- Introduction | ||
+ service implementation overview | ||
+ define service contract | ||
+ defining data contract | ||
+ implementation | ||
|
||
- Service implementation Overview | ||
+ create a wcf service library project, for contain service definition | ||
+ define service contract | ||
+ define data contract for operation parameters and return types | ||
+ define the service implementation class | ||
service layer -> dispatch to business layer -> data layer method | ||
- demo service contract | ||
+ create a new wcf service library | ||
+ delete automatic generated service file | ||
+ remote the auto generated service configuration from app.config file | ||
<services> | ||
<service name ... ></service> //remove | ||
</services> | ||
|
||
+ go to WCF Options and turn off Start WCF Service Host when debugging another project in the same solution | ||
+ go to Debug tab comment out the command line library that the wcf test client | ||
--/client "WcfTestClient.exe" | ||
|
||
+ additional reference of wcf compare to normal class library | ||
System.Runtime.Serialization | ||
System.SeriviceModel | ||
|
||
+ add service contract | ||
[ServiceContract] | ||
public interface IZzaService | ||
{ | ||
[OperationContract] | ||
List<product> GetProducts(); | ||
} | ||
|
||
- data contract | ||
[DataContract] | ||
public class product | ||
{ | ||
[DataMemeber] | ||
public int Id {get;set;} | ||
} | ||
|
||
with out data contract is implicit data contract, you don't have much control on wire level | ||
|
||
with DataContract you could control the expose name, namespace etc. | ||
[DataContract(NameSpace="...", Name="newName")] | ||
|
||
- demo service implementation, service is a class implementation a interface | ||
[ServiceBehavior(InstanceContextModel=InstanceContextMode.PerCall/PerSession/Single)] | ||
public class ZzaService:IZzaService, IDisposable | ||
{ | ||
readonly ZzaDbContext _Context = new ZzaDbContext(); | ||
public void Dispose() | ||
{ | ||
_Context.Dispose(); | ||
} | ||
|
||
[OperationBehavior(TransactionScopeRequired=true)] | ||
public void SubmitOrder(Order order) | ||
{ | ||
_Context.Orders.Add(order); | ||
_Context.SaveChanges(); | ||
} | ||
} | ||
|
||
use entity framework data contract | ||
|
||
define and implementation IDisposable to ask wcf host to dispose the service | ||
|
||
use instanceContextMode to determine the lifetime of your service interface | ||
|
||
the default is percall based wcf instance mode | ||
|
||
wcf support transaction set TransactionScopeRequired behavior could ensure the data is inserted integrated | ||
|
||
|
||
# Hosting Services | ||
- introduction | ||
- hosting overview | ||
- service configuration, the most important and complex | ||
- wcf service library hosting | ||
+ self hosting | ||
* any .net process | ||
* use ServiceHost class | ||
+ IIS Host | ||
* hosted in a web site on an iis server | ||
* defined through an asp.net web application project | ||
* can only hand HTTP traffic | ||
* Get the benefit of iis scalability, robustness, and configurability | ||
* can handle other protocols through "WAS" | ||
window process activation service | ||
|
||
+ WCF service library | ||
* ok for quick smoke testing | ||
* generally want to debug and develop with a self or IIS host | ||
|
||
- demo configuring endpoints | ||
+ configuring endpoints, supply the protocol and address and port | ||
<system.serviceModel> | ||
<services> | ||
<service name="Zza.Services.ZzaService"> | ||
<endpoint address="http://localhost:2112/Zza" binding="basicHttpBinding" contract="Zza.Services.IZzaSerivce"/> | ||
<endpoint address="net.tcp://localhost:2113/Zza" binding="netTcpBinding" contract="Zza.Services.IZzaSerivce"/> | ||
</service> | ||
</services> | ||
</system.serviceModel> | ||
|
||
- demo, configuring behaviors | ||
<system.serviceModel> | ||
<services> | ||
<host> | ||
<baseAddresses> | ||
//with base address could direct use relative address references | ||
<add baseAddress="http://localhost:2112"/> | ||
</baseAddresses> | ||
</host> | ||
<service name="Zza.Services.ZzaService"> | ||
<endpoint address="Zza" binding="basicHttpBinding" contract="Zza.Services.IZzaSerivce"/> | ||
|
||
... | ||
</service> | ||
</services> | ||
<behaviors> | ||
//define a behavior without a name will apply it to all the service for the server | ||
<behavior> | ||
<serviceDebug includeExceptionDetaillInFaults="true"/> | ||
//support get the wisdle information of the service | ||
<serviceMetadata httpGetEnable="true"/> | ||
</behavior> | ||
</behaviors> | ||
</system.serviceModel> | ||
|
||
- demo configuring binding, every binding have lots of configuration aspect to drive the communication happen at wire level | ||
Could use binding configuration to tweak the bindings | ||
<system.serviceModel> | ||
<services> | ||
<service> | ||
... | ||
</service> | ||
</services> | ||
<behaviors> | ||
//define a behavior without a name will apply it to all the service for the server | ||
<behavior> | ||
... | ||
</behavior> | ||
</behaviors> | ||
<bindings> | ||
<basicHttpBinding> | ||
<binding maxReceivedMessageSize="21431" maxBufferSize="2141234"> | ||
<readerQuotas maxArrayLength="2147483647" maxStringContextLength="2147483647"/> | ||
</binding> | ||
</basicHttpBinding> | ||
<netTcpBinding> | ||
<binding maxReceivedMessageSize="21431" maxBufferSize="2141234"> | ||
<readerQuotas maxArrayLength="2147483647" maxStringContextLength="2147483647"/> | ||
</binding> | ||
</netTcpBinding> | ||
</bindings> | ||
</system.serviceModel> | ||
|
||
- hosting in wcf service library | ||
+ connection string configuration | ||
<connectionStrings> | ||
<add name="ZzaDbContext" connectionString="server=.;database=Zza;trusted_connection=true" providerName.../> | ||
</connectionStrings> | ||
<system.serviceModel> | ||
<services>...</services> | ||
<behaviors>...</behaviors> | ||
<bindings>...</bindings> | ||
</system.serviceModel> | ||
|
||
+ uncomment the start wcf client command line from the project properties | ||
+ check the wcf options start debugging | ||
+ run the wcf service library | ||
|
||
- hosting in console application | ||
+ create a console applicatoin | ||
+ reference the wcf service library | ||
+ add reference to | ||
System.ServiceModel | ||
System.Runtime.Serialization | ||
|
||
+ for the configuration, copy and past the connection string and service model sections | ||
+ host the code | ||
ServiceHost host = new ServiceHost(typeof(ZzaServie)); | ||
host.Open(); | ||
|
||
Console.ReadKey(); | ||
host.Close(); // graceful shutdown | ||
|
||
both open and close may throw exceptions, sole need add them into try catch scope | ||
|
||
- hosting-in iis | ||
+ create a asp.net web project | ||
+ reference wcf service library | ||
+ copy and past the connection string and service model section | ||
remote the basic address section and address property for the service element | ||
<system.serviceModel> | ||
<services> | ||
<!--removed | ||
<host> | ||
<baseAddresses> | ||
//with base address could direct use relative address references | ||
<add baseAddress="http://localhost:2112"/> | ||
</baseAddresses> | ||
</host>--> | ||
<service name="Zza.Services.ZzaService"> | ||
<endpoint binding="basicHttpBinding" contract="Zza.Services.IZzaSerivce"/> | ||
|
||
... | ||
</service> | ||
</services> | ||
+ add a serviceName.svc file with content | ||
<%@ ServiceHost Service="TheServiceImplementationClass"%> | ||
|
||
+ if you don't do the WAS hosting, then remove the other endpoint except basicHttpBinding | ||
|
||
|
||
# Implementing client | ||
- introduction | ||
- wcf client overview | ||
+ to consume wcf services from .net clients you need a client proxy | ||
+ proxies can be code generated by visual studio based on metadata exposed by the service | ||
* service must enable metadata either through wsdl or ws-metadataexchange endpoint | ||
|
||
+ proxies can be hand-coded for more control over the code in proxy | ||
* encapsulate repeating patterns of usage, leverage WCF extensibility features | ||
+ make calls through proxy instance methods | ||
* opens connection to the server | ||
* dispatches the call through SOAP messages | ||
* gets a response message back - completes method | ||
|
||
- code generation | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.