Feeds:
Posts
Comments

Posts Tagged ‘WCF’

Finally, my last day of notes from VS Live Las Vegas….

This session was one of the highlights of the convention for me. This was one of the topics I came to learn the most from. I have not used WCF and I need to in a project for work that I am about to start…hence my excitement to attend VSLive to get to learn something that I will actually get to use sooner rather than later.

And Miguel did not disappoint!

I am not about to walk you through the entire day. If you want this type of training, you should attend the next VSLive event or find a way to see Miguel in another venue. He does have a 5 day training class that he teaches…and boy did he try his best to squeeze that training into a single day at VSLive Las Vegas!

My goal is to give enough information that might help you along your way, and this is my way of not forgetting what I learned. Although it has been a couple of weeks since the conference and my mind is getting a bit hazy on the details.

Workshop: Programming with WCF in One Day (Miguel Castro)

The day started with a quick discussion on SOA (Service Oriented Architecture). First off…SOA is not a product and it is not a replacement for OOP. SOA is an architecture and design paradigm, and it’s used to separate applications into loosely coupled services. Clients communicate only with the service layer and only through proxies.

Programming Paradigms

  • Procedural Programming – continuously call functions that house various things
    • Problem: no code reuse outside application, no analogy to real-world
  • Object Oriented Programming – work with various entities that resembled the real-world
    • Problem: no code reuse outside application, lots of plumbing for managing ancillary functionality like transactions and security
  • Component Oriented Programming – encapsulate objects that are managed by COM and housed in DLLs
    • Problem: needs external service management for ancillary functionality
  • Service Oriented Programming – objects managed and encapsulated behind autonomous services with individual responsibilities
    • Problem: cumbersome plumbing for ancillary functionality, requires technology that accommodates interoperability

WCF helps solve the problems encountered in past programming paradigms. WCF provides a unified programming model enforcing an SOA approach for MSMQ, Remoting, Web Services, Enterprise Services, and Sockets.

The WCF architecture is interception-based. Clients talk to proxies, and the proxies serialize the messages before sending them down channels. The channels have a chain structure that depends on binding and behaviors.

The client-side channel will encode and encrypt the message, validate the security credentials, and propagate the transaction. The last channel on a client and the first channel on the service host are the transport channels. Once the service host has the message, the message is passed through the server-side channel chain to get decrypted and then passed on to the last channel on the service host to be dispatched to the actual service object.

WCF is all in one assembly – System.ServiceModel. WCF Essentials: contracts, services, hosting, proxies, and clients.

Visual Studio provides many templates for getting started with WCF. However, Miguel does not like them because they add everything we may need and lots of things we won’t need.

Questions to consider when designing our services:

  • What will our service provide? Service and Operation Contracts
  • What data is needed and what data is provided by our service? Data Contracts
  • What components are used by our service (not the client)? Down-level Layers (Business, ORM, DAL, etc.)
  • How will our service provide data? Service Classes and Service Design
  • How will our service provide listeners? Service Hosting and Endpoint Configuration(s)
  • What objects will expose our service to clients? Proxies
  • Who are our clients? Endpoint Configuration(s)

If we expose our service to HTTP endpoint, our service can be accessed by any other system. If we expose our service with TCP, our service can only be accessed by Windows systems.

When we decorate with data contract, we must add an attribute to serialize. It will be translated into XML and sent as SOAP. SOAP is heavy. BAD IDEA to use DataSets (not interoperable).

Well, at this point I think I have hit a wall in my memory of what my notes mean. It’s been too long and I don’t remember the context around some of my notes. Hopefully these bullet notes will be helpful to some…

  • Contracts –> Services –> Host
  • Services <– Business Engine –> Host
  • In service name – fully qualified name of service with at least 1 endpoint for every contract.
  • Binding defines how we transport.
    • Intranet – netTCPBinding
    • netIPCBinding – named pipe (fastest)
    • Can’t cross machine boundaries client/service on same machine
  • Address – depends on how hosting (full address clients will ping)
  • Client references contracts and System.ServiceModel
  • Put proxies in the Contracts project
    • 1 proxy class per service contract
  • To expose metadata on service so clients can use, set ServiceMetadata httpGetEnabled=”true”
  • Client will add service reference for URL given
  • Restrictions on data size
  • Binding configurations applied to endpoint
    • Within each binding config we can have multiple attributes
    • (Y) Reliability – packet retry – set true
    • (N) Message ordering – rides with reliability (must be same)
    • (UP arrow) Message size (default 64K) limit LONG typically 1M
    • (D) Timeouts – sent/receive (00:01:00) default 30secs
    • Streaming – TCP/IPC only feature (TransferMode=Streamed)
    • Security
    • We can programmatically add endpoints prior to host.open
      • Host.description.endpoint.add()
  • By default error messages never go to client but we can turn this on with IncludeExceptionDetailsInFaults.
  • Instancing controls how a service is newed up.
    • How long object lives and which instance is served up
    • 3 modes – start with per call and only do others if needed
      • Per-call – new instance for every call
      • Per-session – (default) same instance for every call in same proxy until disposed
      • Singleton – when host open, instance is immediately instantiated. Class variables are shared by all calls.
  • Concurrency Mode
    • Single – (default) WCF puts a lock on the service until complete and other calls go on hold
    • Multiple – all bets are off and we have to deal with locks
    • Reentrant – use in call back situation, allows back into single lock
    • Montitor.Enter(Lock)
    • Lock(this)
  • WebHost
    • The .svc file allows call to service
    • <%@ ServiceHost Service=”Essentials.Services.RegionalService” %>
    • In web.config address is blank
    • In Client app.config address URL to service
    • Binding = basicHttpBinding in both configs
  • In WCF 4.0 we can virtualize the existence of svc files and we don’t have to create a file for each svc.
    • In web.config –> ServiceHosting Environment –> ServiceActivation –> add Service = “Essentials.Services.RegionalService” RelativeAddress=”RegServ.svc”
  • Service Host Factory
    • Create a class that inherits from ServiceHostFactoryBase, and override so we can inject into the host.
    • Additional attribute in ServiceActivations for factory
  • We can have multiple endpoints for the same contract. Same service, different protocals. Or we can have 1 public service and 1 admin service.
  • Client needs the name of endpoints.
  • Callbacks – all done in service contract. Client inherits from DuplexClientBase.
  • Security
    • Primary Identity – if ASP.NET is not the user but the process calling it can trick it to be the user
    • Windows Identity – if using custom this will be NULL
    • NetTCPBinding – default Transport/Encrypted and signed
    • Intranet typically uses windows
    • Set up role based auth, don’t use impersonation
    • Internet and wsHttpBinding – default message
    • Security Certificates
      • Export certificate and give client copy
      • Import into Trusted People
      • Change My to Root and will be in trusted
      • Root store and client does not need certificate but does need public key

Well, hopefully some of my notes will have helped someone with something they didn’t know before. The conference was great and I look forward to attending the one in Orlando in Dec 2012. Hopefully by then I will have put some of this information to use.

Please feel free to leave comments on anything you see on my notes from this conference (especially if I have something wrong). Next time I will try not to procrastinate so long before getting my notes typed up. Thanks for reading!

Recommended Reading:

  • Learning WCF by O’Reiley – Michele Lereux-Bustamante (IDesign)
  • Programming WCF by O’Reiley – Juval Lowi (IDesign)
  • Pro WCF by Apress – Chris Peiris & Dennis Mulder
  • Essential WCF by Addison-Wesley – Chris Bowen, et. al.

Related Sites:

Read Full Post »

%d bloggers like this: