Chad Michel cmichelnebraskaglobal com SOA SERVICE HELPERS ME
Chad Michel cmichel@nebraskaglobal. com SOA – SERVICE HELPERS
ME � Engineering background � Software Architect � Multiple solutions � Accounting � C++, / Billing MFC � Ecommerce �. NET / ASP. NET � CDN �. NET � Asset / ASP. NET / MVC Management �. NET / WCF / WPF 2
NEBRASKA GLOBAL / DON’T PANIC LABS � Starting � Lots 10 companies in 5 years of engineers � Need for consistent pattern � Service Orientation � Each Catalog Manager service encapsulates volatility � Design to a contract � Repeatable, Maintainable, TESTable � WCF ideal plumbing for service orientation Product Accessor
SERVICE ORIENTATION - OVERVIEW Overview Design Implementation Today Foundation
REVIEW � Service Orientation Overview � Application is built from Services � WCF is the plumbing
WCF CAN SUCK � Calling � It WCF Service doesn’t have to suck
THE PROMISE AND POWER OF WCF � Ideal plumbing for service orientation In-Process Call to an Azure-hosted web service � Both calls Provide: Security Transactions Reliable messages Timeouts Throttling Threading Error Masking
WCF COMES WITH A LOT OF OVERHEAD � Programmers have to know a lot to use WCF effectively. � Create Service (and dispose) � Create Proxy (close or abort) � Cache proxy?
WHAT IS A WCF SERVICE? � What do I mean by a “service”? Catalog Manager Product Accessor � Endpoint � Lives behind a contract � Accessed thru a proxy 9
DEFINING A WCF SERVICE My. Service
CREATING A WCF SERVICE My. Service
CREATING A WCF PROXY Client. Prox y Proxy My. Service
WHAT ABOUT CLOSING THE PROXY? � Don’t use a using statement � Call abort if you receive an exception (not close) � What if close throws an exception?
YUCK
CAN’T WE MAKE THIS BETTER? � How about a single method that abstracts the entire close / abort process for us. � We don’t want a bunch of try/catches in our code.
DPL SERVICE HELPERS � Easy to use library that simplifies working with WCF � http: //dplservicehelpers. codeplex. com/
FIRST SIMPLIFICATION � Makes calling a WCF service super easy � When calling a WCF service we must 1. 2. 3. Create Proxy (potentially use cached) Call WCF service Close or Abort Proxy (handle errors)
LAMBDA � Anonymous function � Allows for passing around functions � Delegates work too � We will pass code to proxy factory � Proxy factory will � Create Proxy � Execute lambda � Close / Abort Proxy
LAMBDA Caller Proxy Factory Proxy Service
PROXYFACTORY. CALL Caller Lambda Proxy Factory Proxy Service
LETS LOOK AT SOME CODE
CAN WE DO BETTER? � YES � Lambda exists because we must control access to the proxy � Can we manage access to the proxy in another way?
LAMBDA EXISTS BECAUSE… � We can’t run directly against the proxy � Proxy requires custom close / abort � We 1. 2. 3. 4. could Use Lambda (its ok) Use code gen (T 4 Template) Hand-write proxies (yuck) …
CODE GEN � Code generation is best option � More code � Requires re-gen of code when something changes � Potentially a lot of code � Static � We code gen isn’t ideal want a generated proxy, but we don’t want to have to use static code gen
THINK DYNAMICALLY � As . NET developers we tend to think only about static solutions. � A dynamic solution is perfect in the situation � We can basically do code gen dynamically against the contract
DYNAMIC OPTIONS � Write out C# and then compile a true assembly � Dynamic. Object � Reflection. Emit requires writing IL directly, but gives us the ultimate flexibility.
REFLECTION. EMIT – IT CAN BE YOUR FRIEND � If we generate a proxy using Reflection. Emit we can still present out a simple to use proxy, but still force access thru Factory. Call (lambda). Magic Caller Generated Proxy Factory Real Proxy Service
THIN PROXY � What does our proxy need to do? � Implement every method of the contract (interface) � Call a method on the Proxy. Factory � Method in proxy factory will use existing Call (lambda method)
THIN PROXY GEN We want to create a proxy for this contract Each method will “proxy” the call thru Proxy. Factory Call. Method.
DYNAMIC PROXY
HOW TO CREATE DYNAMIC PROXY 1. 2. 3. Create dynamic assembly Create class Define methods for each method in the interface 1. Method body must be written using IL
TIPS ON WRITING IL DIRECTLY � Just. Decompile � Shows � Write IL for existing implementation proxy in c# � Look at IL � Understand how IL works � Rewrite � Lets in a logical flow (IL will be odd) look at some code
NOW… � With dynamic proxy, calling a WCF service now looks like this…
WHAT DO WE END UP WITH
SERVICE ORIENTATION � Everything is a service � We don’t want to use the Service. Model section for each Service � YUCK � We also want to support many default behaviors � In process (ability to disable WCF for these calls) � External
PROXY CONFIGURATION � We could rely upon the Service. Model configuration section � YUCK
SERVICE MODEL SECTION SHOULD ALWAYS RULE � We can provide other ways to change the behavior, but the Service. Model section should rule all � If client endpoint is defined in section then it should be used
SIMPLIFIED CONFIGURATION � For in process � Use an attribute if possible � Use custom configuration section � For external WCF call � Use custom configuration section
INPROC ATTRIBUTE Use In process hosted WCF service.
CUSTOM CONFIGURATION SECTION
INPROC CONFIGURATION � Attribute � Custom configuration section � Disable in process if necessary
EXTERNAL CONFIGURATION � Service. Model � Use section works custom configuration section to simplify
OTHER THINGS WE CAN IMPROVE 1. 2. 3. 4. Logging Proxy Caching Service hosting Security / bindings
LOGGING � Because we intercept every call it is easy to log every call � Log to enterprise library as default � Can change
PROXY CACHING � Improves performance if proxy is already open � Somewhat dangerous � Need to know when to enable � Will default to off � Not currently in our Service. Helpers
SERVICE HOSTING � Simplify the creation of services � Less configuration, less pain � More of scenario based � Business to Business � Internal PC to Internal PC � Same Computer � Not currently in library
SECURITY � Again, � Not scenario based currently in Library
WCF - MANAGEABLE � WCF can be made very simple � Take a look at the code if you are doing some WCF, it could help � � http: //dplservicehelpers. codeplex. com/ http: //nuget. org/List/Packages/DPLService. Helpers
QUESTIONS / COMMENTS � cmichel@nebraskaglobal. com
- Slides: 49